基于yum命令的mysql8.0安装

安装

# 添加 mysql yum源
yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# 安装 yum 工具 yum-utils
yum install -y yum-utils
# 导入密钥
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
# 安装mysql
yum install mysql-community-server

启动

# 启动mysql
systemctl start mysqld
# 关闭mysql
systemctl stop mysqld
# 查看mysql状态
systemctl status mysqld

修改密码

初次安装mysql后,使用临时密码登录命令行

# 查看mysql安装后的初始登录密码
cat /var/log/mysqld.log | grep 'temporary password'
# 使用临时密码登录mysql
mysql -uroot -p${password}

修改mysql密码

# 由于安全策略的限制,可以先设置一个复杂的密码
alter user 'root'@'localhost' identified by '${password}' ;
# 刷新权限
flush privileges;

设置简单密码(可选)

# 查看密码校验策略
show variables like 'validate_password%';
# 设置密码校验策略为LOW
set global validate_password.policy=0;
# 设置密码长度限制最低6位
set global validate_password.length=6;
# 修改root密码为简单密码
alter user 'root'@'localhost' identified by '123456';
# 刷新权限
flush privileges;

错误排查

当前系统已经安装其他版本的mysql

# 查寻当前系统是否存在mysql安装包
rpm -qa | grep mysql
# 如果存在mysql安装包,先卸载
yum remove ${mysql-community*}

root用户无法远程连接

# 登录mysql,创建用户
create user 'root'@'%' identified by '123456';
# 授权数据库给新创建的用户
grant all privileges on ${db_name}.* to 'root'@'%';
# 或以下一行命令
alter user 'root'@'%' identified with mysql_native_password by '123456';

1227 Access Denied

# you need the PROCESS(SYSTEM_USER) privilege(s) 错误
grant process on *.* to ${user}@'%' ;