hugo-teek is loading...

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 仓库

1sudo rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

2. 安装 MySQL 5.7

1sudo yum install mysql-community-server -y
2
3#用下面这个
4sudo yum install mysql-community-server -y --nogpgcheck

3. 启动 MySQL 并设置开机自启

1sudo systemctl start mysqld
2sudo systemctl enable mysqld

4. 获取临时密码

MySQL 5.7 首次安装会生成临时密码:

1sudo grep 'temporary password' /var/log/mysqld.log

输出类似:

12023-01-01T00:00:00.000000Z 1 [Note] A temporary password is generated for root@localhost: Abc123!@#

5. 安全配置 MySQL

运行安全脚本:

1sudo mysql_secure_installation

按提示操作:

  1. 输入临时密码。
  2. 设置新密码(需符合复杂度要求,如 MyNewPass@123)。
  3. 移除匿名用户、禁止远程 root 登录、删除测试数据库等。

6. 验证安装

1mysql -u root -p

输入密码后进入 MySQL Shell,运行:

1SELECT VERSION();

应输出 5.7.x


方法 2:安装 MySQL 5.6(不推荐,仅限特殊需求)

MySQL 5.6 已停止支持,需手动启用旧版仓库:

1. 禁用默认 MySQL 8.0 仓库

1sudo yum-config-manager --disable mysql80-community

2. 启用 MySQL 5.6 仓库

1sudo yum-config-manager --enable mysql56-community

3. 安装 MySQL 5.6

1sudo yum install mysql-community-server -y

后续步骤与 MySQL 5.7 相同(启动服务、安全配置等)。


方法 3:安装 MariaDB 5.5(兼容 MySQL 5.6)

如果项目兼容 MariaDB,推荐使用它(更安全且持续更新):

1. 安装 MariaDB 5.5

1sudo yum install mariadb-server mariadb -y

2. 启动并配置

1sudo systemctl start mariadb
2sudo systemctl enable mariadb
3sudo mysql_secure_installation

3. 验证版本

1mysql -V

输出类似:

1mysql  Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64)

常见问题

1. 如何卸载旧版本 MySQL?

1sudo yum remove mysql-server mysql-community-server
2sudo rm -rf /var/lib/mysql

2. 如何修改 MySQL 密码策略?

编辑配置文件:

1sudo vi /etc/my.cnf

添加:

1[mysqld]
2validate_password_policy=LOW
3validate_password_length=4

重启 MySQL:

1sudo systemctl restart mysqld

3. 如何允许远程访问?

登录 MySQL 后执行:

1GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;
2FLUSH PRIVILEGES;

并开放防火墙:

1sudo firewall-cmd --add-service=mysql --permanent
2sudo firewall-cmd --reload

版本对比

版本状态推荐场景
MySQL 5.6已停止支持仅限遗留系统
MySQL 5.7维护至 2023-10旧项目过渡
MariaDB 5.5持续维护兼容 MySQL 5.6

总结

  • 推荐方案:优先使用 MariaDBMySQL 5.7
  • 强制需求:若必须用 MySQL 5.6,需手动启用旧仓库。
  • 安全提醒:旧版本存在漏洞,建议升级或使用替代方案。

如果有特殊需求(如特定补丁版本),可考虑手动编译安装,但复杂度较高。

步骤

 1[root@localhost conf.d]#sudo grep 'temporary password' /var/log/mysqld.log
 22025-06-08T00:42:17.640714Z 1 [Note] A temporary password is generated for root@localhost: 1;,L(nNjJ9&Z
 3[root@localhost conf.d]#sudo mysql_secure_installation
 4
 5Securing the MySQL server deployment.
 6
 7Enter password for user root: 
 8
 9The existing password for the user account root has expired. Please set a new password.
10
11New password: 
12
13Re-enter new password: 
14The 'validate_password' plugin is installed on the server.
15The subsequent steps will run with the existing configuration
16of the plugin.
17Using existing password for root.
18
19Estimated strength of the password: 100 
20Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
21
22 ... skipping.
23By default, a MySQL installation has an anonymous user,
24allowing anyone to log into MySQL without having to have
25a user account created for them. This is intended only for
26testing, and to make the installation go a bit smoother.
27You should remove them before moving into a production
28environment.
29
30Remove anonymous users? (Press y|Y for Yes, any other key for No) : yes
31Success.
32
33
34Normally, root should only be allowed to connect from
35'localhost'. This ensures that someone cannot guess at
36the root password from the network.
37
38Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no
39
40 ... skipping.
41By default, MySQL comes with a database named 'test' that
42anyone can access. This is also intended only for testing,
43and should be removed before moving into a production
44environment.
45
46
47Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes
48 - Dropping test database...
49Success.
50
51 - Removing privileges on test database...
52Success.
53
54Reloading the privilege tables will ensure that all changes
55made so far will take effect immediately.
56
57Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
58Success.
59
60All done! 
61[root@localhost conf.d]#
推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航