在CentOS 7上通过RPM安装MySQL 5.7
首先,访问MySQL官方网站下载所需版本的RPM包。
例如,MySQL 5.7的RPM包可以从这里获取:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
下载完成后将其上传到CentOS系统中。
[root@server ~]# mkdir mysql_dir //创建一个专门的MySQL目录
[root@server ~]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar -C mysql_dir/ //解压文件到mysql_dir目录
[root@server ~]# yum install -y make gcc-c++ cmake bison-devel ncurses-devel libaio libaio-devel net-tools //安装必要的依赖包
由于CentOS 7默认安装了MariaDB,因此需要先卸载系统中的MariaDB组件才能安装MySQL。
[root@server ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@server ~]# yum remove -y mariadb-libs
现在可以开始安装MySQL。因为存在依赖关系,所以安装顺序是固定的。
[root@server ~]# rpm -ivh mysql_dir/mysql-community-common-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-libs-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-client-5.7.26-1.el7.x86_64.rpm
[root@server ~]# rpm -ivh mysql_dir/mysql-community-server-5.7.26-1.el7.x86_64.rpm //完成MySQL服务器的安装
下一步,启动MySQL服务并设置开机自启。
[root@server ~]# systemctl start mysqld
[root@server ~]# systemctl enable mysqld
[root@server ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-09-01 10:00:00 CST; 1min 30s ago
Main PID: 12345 (mysqld)
CGroup: /system.slice/mysqld.service
└─12345 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
9月 01 10:00:00 server systemd[1]: Starting MySQL Server...
9月 01 10:00:00 server systemd[1]: Started MySQL Server.
第四步,获取MySQL临时密码,并为root用户设置新密码。
[root@server ~]# grep "temporary password" /var/log/mysqld.log //查找日志中的临时密码
2023-09-01T02:00:00.123456Z 1 [Note] A temporary password is generated for root@localhost: abc!@#123
[root@server ~]# mysql -uroot -p"abc!@#123"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
//尝试使用简单密码会失败
mysql> alter user 'root'@'localhost' identified by 'simple';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by 'ComplexPass123!';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
除了使用"alter user 'root'@'localhost' identified by 'ComplexPass123!';",还可以用"set password for root@localhost = password('ComplexPass123!');"。
最后一步,测试连接。
如果密码包含特殊字符,必须用引号包裹。
[root@server ~]# mysql -u root -p'ComplexPass123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
扩展说明:
若想设置简单的密码,可以通过以下两种方法之一来实现:修改MySQL命令或直接编辑配置文件。
使用MySQL命令的方法如下:
[root@server ~]# mysql -uroot -p'ComplexPass123!'
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.02 sec)
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@localhost=password('easy');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
直接修改配置文件的方法:
[root@server ~]# vi /etc/my.cnf
# 在[mysqld]部分添加validate-password=OFF以禁用密码验证插件
[root@server ~]# systemctl restart mysqld
[root@server ~]# mysql -uroot -peasy
mysql> set password for root@localhost=password('veryeasy');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye