MySQL常见问题解决和自动化安装脚本


常见问题

MySQL密码正确但无法登录的情况

这种情况一般都是因为缓存,使用mysql -u root -p123456直到成功登陆为止,并且进入之后重新修改密码,多次重复修改密码的命令并且再一次清除缓存后退出。

sql 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

MySQL安装的GPG密钥问题

前言

在MySQL安装的过程中,我们出现了不能通过YUM安装的MySQL的问题,YUM工具警告说安装包的GPG密钥无效或未安装,这意味着不能验证该包的真实性和完整性。系统尝试从指定的本地路径检索密钥,但没有找到有效的密钥与包匹配。

具体错误
shell 复制代码
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-8.0.36-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID a8d3785c: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


The GPG keys listed for the "MySQL 8.0 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.


 Failing package is: mysql-community-client-8.0.36-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决方式
shell 复制代码
yum install net-tools -y # 检查和安装网络工具包
yum list installed | grep mysql # 检查已安装的MySQL安装包
rpm -e --nodeps mysql80-community-release-el7-8.noarch # 强制删除指定的MySQL仓库包,不检查依赖。
rm -rf /etc/my.cnf # 删除MySQL的主配置文件。
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 # 导入MySQL的GPG公钥,用于验证软件包。
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm# 安装新的MySQL仓库配置包。
yum -y install mysql-community-server# 安装MySQL服务器及其依赖。

此处出现了GPG的错误,我们需要通过绕过GPG的检查来实现安装

shell 复制代码
yum -y install mysql-community-server  --nogpgcheck # 忽略GPG密钥检查,重新安装
systemctl start mysqld && systemctl enable mysqld # 启动MySQL服务并设置为开机启动。
systemctl status mysqld # 检查MySQL服务状态,确认服务已经运行
grep 'temporary password' /var/log/mysqld.log # 获取随机密码并登录
sql 复制代码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
set global validate_password.policy=0;
ALTER USER USER() IDENTIFIED BY 'wingkin45@A.';
set global validate_password.policy=0;
set global validate_password.length=4;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

MySQL自动化安装脚本

shell 复制代码
echo "INFO : BEGIN INSTALLING MYSQL8 ..."
#检查mysql服务版本,若存在Mysql8,直接退出
me=`ps -ef|grep mysqld|wc -l`
if [ $me -eq 2 ];then
        me=`systemctl status mysqld|grep Docs|awk -F: '{print $NF}'`
        if [ $? -eq 0 -a "$me" == "mysql(8)" ];then
                echo "INFO : MYSQL8 FOUND "
                exit 0
        fi

        #若存在旧版本的mysql,且服务处于启动状态
        systemctl status mysqld|grep "active (running)"
        if [ $? -eq 0 ];then
                systemctl stop mysqld
                echo "INFO : OLD MYSQL SERVICE FOUND BUT STOPPED"
        fi
fi
#若存在旧版本的残留,则直接卸载
names=`rpm -qa|grep mysql`
for name in $names;do
        yum -y remove $name 1>/dev/null 2>&1
        echo "INFO : OLD MYSQL RESOURCE [ $name ] FOUND AND REMOVED"
done

#rpm 安装mysql8
src=$1
if [[ ! -e $src || ! -f $src ]];then
        echo "ERROR : PLEASE PROVIDE MYSQL8 RPM FILE "
        exit 1
fi
if [[ ! "$src" =~ mysql8.*\.rpm$ ]];then
        echo "ERROR : RPM PACKAGE OR NOT MYSQL8"
        exit 1
fi

rpm -ivh $src 1>/dev/null 2>&1
if [ $? -eq 0 ];then
        echo "INFO : SUCCESS TO RPM INSTALL MYSQL8"
else
        echo "ERROR : FAIL TO RPM INSTALL MYSQL8"
        exit 2
fi

# yum 安装mysql8服务
yum -y install mysql-community-server --nogpgcheck 1>/dev/null 2>&1
if [ $? -eq 0 ];then
        echo "INFO : SUCCESS TO YUM MYSQL8 SERVICE"
else
        echo "ERROR : FAIL TO YUM MYSQL8 SERVICE"
        exit 3
fi

#检查服务是否已启动,若未启动则启动
me=`systemctl status mysqld|grep "active (running)"`
if [ $? -ne 0 ];then
        systemctl start mysqld
fi
echo "INFO : MYSQL8 SERVICE STARTED"

echo "INFO : FINISH INSTALL MYSQL8"

unset me
unset names
unset src
相关推荐
瓜牛_gn3 小时前
mysql特性
数据库·mysql
Yaml48 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
追风林8 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
Hsu_kk10 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境10 小时前
第02章 MySQL环境搭建
数据库·mysql
knight-n10 小时前
MYSQL库的操作
数据库·mysql
eternal__day12 小时前
MySQL_聚合函数&分组查询
数据库·mysql
咕哧普拉啦12 小时前
乐尚代驾十订单支付seata、rabbitmq异步消息、redisson延迟队列
java·spring boot·mysql·spring·maven·乐尚代驾·java最新项目
春哥的魔法书14 小时前
数据库基础(5) . DCL
数据库·mysql
鬼才血脉14 小时前
docker+mysql配置
mysql·adb·docker