1.从官网获取Yum Repository下载链接
https://dev.mysql.com/downloads/repo/yum/
例如:wget https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm
2.安装Yum Repository
yum -y install mysql80-community-release-el7-11.noarch.rpm
3.安装MySQL
yum -y install mysql-community-server
如有All matches were filtered out by modular filtering for argument: mysql-community-server执行:
yum module disable mysql
如有GPG Keys报错,执行:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023(以后年份可能会变成2024、2025)
yum clean all
4.MySQL服务命令
systemctl start mysqld.service 启动
systemctl status mysqld.service 查看状态
systemctl stop mysqld.service 停止
systemctl restart mysqld.service 重启
systemctl enable mysqld.service 开机启动
systemctl disable mysqld.service 停止开机启动
5.获取MySQL的root密码
grep 'temporary password' /var/log/mysqld.log
6.登录MySQL
mysql -u root -p
接着输入获取到的密码回车即可登录
7.密码相关
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Password_1'; 修改root密码
SHOW VARIABLES LIKE 'validate_password%'; 查看密码策略
set global validate_password.policy=LOW; 设置密码策略为低
set global validate_password.length=6; 设置密码长度为6
CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 新增用户
8.允许远程登录
use mysql;
select host, user from user;
update user set host = '%' where user = 'root';
或者
use mysql;
select User,authentication_string,Host from user;
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
修改后需重启MySQL服务
systemctl restart mysqld.service
还需检查防火墙是否开放端口3306
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
9.卸载MySQL,先查询所有MySQL相关的rpm
rpm -qa | grep mysql
然后每个都要卸载,例如
rpm -ev mysql80-community-release-el7-7.noarch
如果报依赖错误,加--nodeps参数,例如
rpm -ev mysql80-community-release-el7-7.noarch --nodeps
查询并删除所有MySQL相关的文件夹
find / -name mysql
例如rm -rf /var/lib/mysql/
还需删除my.cnf rm -rf /etc/my.cnf