mysql部署-gpt
centos7安装 6.0以下mysql ?
在 CentOS 7 上安装 MySQL 5.7 或更低版本(如 MySQL 5.6) 的步骤如下。由于 MySQL 官方已停止维护旧版本,推荐使用 MariaDB(MySQL 分支)作为替代。若仍需安装原生 MySQL 5.6/5.7,需通过官方历史仓库或手动编译安装。
方法 1:安装 MySQL 5.7(推荐)
MySQL 5.7 是较旧的稳定版本,仍被部分老项目使用。以下是安装步骤:
1. 添加 MySQL 5.7 Yum 仓库
bash
sudo rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
2. 安装 MySQL 5.7
bash
sudo yum install mysql-community-server -y
#用下面这个
sudo yum install mysql-community-server -y --nogpgcheck
3. 启动 MySQL 并设置开机自启
bash
sudo systemctl start mysqld
sudo systemctl enable mysqld
4. 获取临时密码
MySQL 5.7 首次安装会生成临时密码:
bash
sudo grep 'temporary password' /var/log/mysqld.log
输出类似:
2023-01-01T00:00:00.000000Z 1 [Note] A temporary password is generated for root@localhost: Abc123!@#
5. 安全配置 MySQL
运行安全脚本:
bash
sudo mysql_secure_installation
按提示操作:
- 输入临时密码。
- 设置新密码(需符合复杂度要求,如
MyNewPass@123
)。 - 移除匿名用户、禁止远程 root 登录、删除测试数据库等。
6. 验证安装
bash
mysql -u root -p
输入密码后进入 MySQL Shell,运行:
sql
SELECT VERSION();
应输出 5.7.x
。
方法 2:安装 MySQL 5.6(不推荐,仅限特殊需求)
MySQL 5.6 已停止支持,需手动启用旧版仓库:
1. 禁用默认 MySQL 8.0 仓库
bash
sudo yum-config-manager --disable mysql80-community
2. 启用 MySQL 5.6 仓库
bash
sudo yum-config-manager --enable mysql56-community
3. 安装 MySQL 5.6
bash
sudo yum install mysql-community-server -y
后续步骤与 MySQL 5.7 相同(启动服务、安全配置等)。
方法 3:安装 MariaDB 5.5(兼容 MySQL 5.6)
如果项目兼容 MariaDB,推荐使用它(更安全且持续更新):
1. 安装 MariaDB 5.5
bash
sudo yum install mariadb-server mariadb -y
2. 启动并配置
bash
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
3. 验证版本
bash
mysql -V
输出类似:
mysql Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64)
常见问题
1. 如何卸载旧版本 MySQL?
bash
sudo yum remove mysql-server mysql-community-server
sudo rm -rf /var/lib/mysql
2. 如何修改 MySQL 密码策略?
编辑配置文件:
bash
sudo vi /etc/my.cnf
添加:
ini
[mysqld]
validate_password_policy=LOW
validate_password_length=4
重启 MySQL:
bash
sudo systemctl restart mysqld
3. 如何允许远程访问?
登录 MySQL 后执行:
sql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
FLUSH PRIVILEGES;
并开放防火墙:
bash
sudo firewall-cmd --add-service=mysql --permanent
sudo firewall-cmd --reload
版本对比
版本 | 状态 | 推荐场景 |
---|---|---|
MySQL 5.6 | 已停止支持 | 仅限遗留系统 |
MySQL 5.7 | 维护至 2023-10 | 旧项目过渡 |
MariaDB 5.5 | 持续维护 | 兼容 MySQL 5.6 |
总结
- 推荐方案:优先使用 MariaDB 或 MySQL 5.7。
- 强制需求:若必须用 MySQL 5.6,需手动启用旧仓库。
- 安全提醒:旧版本存在漏洞,建议升级或使用替代方案。
如果有特殊需求(如特定补丁版本),可考虑手动编译安装,但复杂度较高。
步骤
bash
[root@localhost conf.d]#sudo grep 'temporary password' /var/log/mysqld.log
2025-06-08T00:42:17.640714Z 1 [Note] A temporary password is generated for root@localhost: 1;,L(nNjJ9&Z
[root@localhost conf.d]#sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : yes
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
Success.
All done!
[root@localhost conf.d]#