1、下载 mysql-rpm 报
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
2、安装MySQL源
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
3、安装MySQL社区服务器
yum install -y mysql-community-server
如果出现这样的错误:
Total size: 220 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-libs-5.7.44-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
The GPG keys listed for the "MySQL 5.7 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-libs-5.7.44-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
执行:
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
命令重新导入 mysql 的GPG密钥试试看。
不行尝试,清理 yum 缓存并重新安装:
sudo yum clean all
sudo yum install mysql-community-client mysql-community-libs mysql-community-libs-compat mysql-community-server
还不行尝试,下载新的GPG密钥文件“
sudo curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
重新安装MySQL包:
sudo yum clean all
sudo yum install mysql-community-client mysql-community-libs mysql-community-libs-compat mysql-community-server
暴力办法:临时禁用GPG检查(不推荐,仅应急)
sudo yum install --nogpgcheck mysql-community-client mysql-community-libs mysql-community-libs-compat mysql-community-server
验证仓库配置
确保MySQL仓库文件(如/etc/yum.repos.d/mysql-community.repo)中的gpgkey指向正确URL:
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
4、启动 mysql
systemctl start mysqld.service
5、检查是否启动成功
systemctl status mysqld.service
6、获取临时密码,MySQL5.7为root用户随机生成了一个密码
grep 'temporary password' /var/log/mysqld.log
7、通过临时密码登录MySQL,进行修改密码操作
mysql -uroot -p
7.5、如果需要启用 MySQL 增强安全,即:“按提示设置密码强度、删除匿名用户、禁止远程 root 登录等安全配置”。
sudo mysql_secure_installation
8、执行以下两个命令,确保可以随意设置我们想要的密码。
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
9、修改 root 的登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
10、授权其它IP地址的机器访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
11、设置开机启动
systemctl enable mysqld
systemctl daemon-reload
12、设置MySQL字符集为UTF-8,用于支持中文
vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysql]
default-character-set=utf8
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
13、重启 MySQL
service mysqld restart
14、如果正在启用 firewall 防火墙,则需要开放3306的端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
但个人建议用 iptables 来管理防火墙规则,效率更好一些。