CentOS 7 下安装 MySQL 5.7
一、环境准备
1、下载 MySQL Yum 仓库包:
wget https://dev.mysql.com/get/mysql57communityreleaseel711.noarch.rpm
2、安装 MySQL Yum 仓库包:
sudo yum localinstall mysql57communityreleaseel711.noarch.rpm
3、查看可用的 MySQL 版本:
yum repolist all | grep mysql
4、禁用 MySQL 8.0 并启用 MySQL 5.7:
sudo yumconfigmanager disable mysql80community sudo yumconfigmanager enable mysql57community
5、安装 MySQL 5.7:
sudo yum install y mysqlcommunityserver
6、启动 MySQL 服务:
sudo systemctl start mysqld
7、查看 MySQL 服务状态:
sudo systemctl status mysqld
二、配置 MySQL
1、获取临时密码:
sudo grep 'temporary password' /var/log/mysqld.log
2、使用临时密码登录 MySQL:
mysql u root p
3、修改 root 用户密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
4、设置远程连接权限(可选):
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '新密码' WITH GRANT OPTION; FLUSH PRIVILEGES;
5、配置防火墙:
firewallcmd zone=public addport=3306/tcp permanent firewallcmd reload
三、常见问题及解决
FAQs:
Q1: 如何更改 MySQL 的默认编码为 utf8?
A1:
编辑/etc/my.cnf
文件,添加或修改以下内容:
[client] defaultcharacterset=utf8 [mysqld] charactersetserver=utf8 collationserver=utf8_general_ci
然后重启 MySQL 服务:
sudo systemctl restart mysqld
Q2: 如果忘记 root 用户密码,如何重置?
A2:
1、停止 MySQL 服务:
sudo systemctl stop mysqld
2、跳过授权表启动 MySQL:
sudo mysqld_safe skipgranttables &
3、登录 MySQL:
mysql u root
4、重置密码:
FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
5、退出 MySQL:
exit;
6、重新启动 MySQL 服务:
sudo systemctl start mysqld