MySQL 8.X 版本安装及用户管理
离线安装
软件包下载地址:http://mirrors.sohu.com/mysql/MySQL-8.0/
这里使用8.0.18
版本,对应各软件包下载链接如下:
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-client-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-common-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-devel-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-embedded-compat-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-libs-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-libs-compat-8.0.18-1.el7.x86_64.rpm
http://mirrors.sohu.com/mysql/MySQL-8.0/mysql-community-server-8.0.18-1.el7.x86_64.rpm
📝 安装步骤
✏️ 将软件包上传至服务器
# ll
total 514508
-rw-r--r--. 1 root root 40104640 Mar 17 17:08 mysql-community-client-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 611436 Mar 17 17:07 mysql-community-common-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 6915400 Mar 17 17:25 mysql-community-devel-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 23683600 Mar 17 17:26 mysql-community-embedded-compat-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 3877664 Mar 17 17:07 mysql-community-libs-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 1363968 Mar 21 00:23 mysql-community-libs-compat-8.0.18-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 450282440 Mar 17 17:20 mysql-community-server-8.0.18-1.el7.x86_64.rpm
✏️ 安装
# yum localinstall ./mysql-community-*
...
Complete!
✏️ 启动
# systemctl start mysqld
✏️ 查看初始密码并修改密码
# grep password /var/log/mysqld.log
2020-03-20T15:11:35.241552Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: W7.j*e<lfU_9
# mysql -u root -p
...
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY "NewPass#123";
Query OK, 0 rows affected (0.02 sec)
在线安装
📝 安装步骤
✏️ 安装MySQL yum源
# wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
# yum -y localinstall mysql80-community-release-el7-1.noarch.rpm
✏️ 更新yum源
# yum clean all
# yum makecache
✏️ 安装
# yum install mysql-community-server -y
✏️ 启动
# systemctl start mysqld
✏️ 查看初始密码并修改密码
# grep password /var/log/mysqld.log
2020-03-20T15:11:35.241552Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: W7.j*e<lfU_9
# mysql -u root -p
...
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY "NewPass#123";
Query OK, 0 rows affected (0.02 sec)
账户密码管理
MySQL8 账户密码默认策略采用的是
caching_sha2_password
,导致很多时候我们创建了用户和密码,在客户端连接时一直提示密码错误。修改认证方式可以通过配置文件修改,也可以修改单独用户的认证策略。
📝 示例不修改加密方式创建用户
✏️ 创建用户
这里我创建一个zabbix库进行创建用户和授权示例
mysql> create database zabbix character set utf8mb4;
Query OK, 1 row affected (0.01 sec)
mysql> create user 'zabbix'@'localhost' identified by 'Zabbix@123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,plugin from mysql.user where user='zabbix';
+--------+-----------+-----------------------+
| user | host | plugin |
+--------+-----------+-----------------------+
| zabbix | localhost | caching_sha2_password |
+--------+-----------+-----------------------+
1 row in set (0.00 sec)
✏️ 修改单个用户的加密方式
#这条命令可以查询用户所使用的密码插件模式
mysql> select user,host,plugin from mysql.user;
mysql> select user,host,plugin from mysql.user where user='zabbix';
mysql> alter user 'zabbix'@'localhost' identified with mysql_native_password by "Zabbix@123";
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,plugin from mysql.user where user='zabbix';
+--------+-----------+-----------------------+
| user | host | plugin |
+--------+-----------+-----------------------+
| zabbix | localhost | mysql_native_password |
+--------+-----------+-----------------------+
1 row in set (0.00 sec)
📝 示例,修改配置文件更改加密方式
✏️ 修改配置文件
# vim /etc/my.cnf
[mysqld]
......
default-authentication-plugin=mysql_native_password
# systemctl restart mysqld
✏️ 创建用户查看
mysql> create user 'zabbix_test'@'localhost' identified by "Zabbix@321";
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,plugin from mysql.user where user='zabbix_test';
+-------------+-----------+-----------------------+
| user | host | plugin |
+-------------+-----------+-----------------------+
| zabbix_test | localhost | mysql_native_password |
+-------------+-----------+-----------------------+
1 row in set (0.00 sec)
通过上面可以查看,修改配置文件后,现在创建的新用户默认加密方式即为mysql_native_password
模式
人生是条无名的河,是浅是深都要过;
人生是杯无色的茶,是苦是甜都要喝;
人生是首无畏的歌,是高是低都要唱。