https://www.bilibili.com/video/BV1Sv411r7vd?p=89
1.新建文件夹/opt/mysql,并cd进去
mkdir /opt/mysql
cd /opt/mysql/
2. 运行 wget http://dev.mysql.com/get/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar ,下载mysql安装包
PS:centos7.6自带的类mysql数据库是mariadb,会跟mysql冲突,要先删除。
3. 运行 tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
4. 运行 rpm -qa|grep mari ,查询 mariadb 相关安装包
5. 运行 rpm -e --nodeps mariadb-libs 和 rpm -e --nodeps marisa ,卸载
[root@cjbCentos01 mysql]# rpm -qa|grep mari
mariadb-libs-5.5.68-1.el7.x86_64
marisa-0.2.4-4.el7.x86_64
[root@cjbCentos01 mysql]# rpm -e --nodeps mariadb-libs
[root@cjbCentos01 mysql]# rpm -qa|grep mari
marisa-0.2.4-4.el7.x86_64
[root@cjbCentos01 mysql]# rpm -e --nodeps marisa
再次查看没有 mariadb 的相关安装包
6. 然后开始真正安装mysql,依次运行以下几条
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
案例:
[root@cjbCentos01 mysql]# rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-5.7.26-1.e################################# [100%]
[root@cjbCentos01 mysql]# rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-5.7.26-1.el7################################# [100%]
[root@cjbCentos01 mysql]# rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-5.7.26-1.e################################# [100%]
[root@cjbCentos01 mysql]#
[root@cjbCentos01 mysql]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-5.7.26-1.e################################# [100%]
[root@cjbCentos01 mysql]#
7.运行 systemctl start mysqld.service ,启动mysql
8. 然后开始设置root用户密码
Mysql自动给root用户设置随机密码,运行 grep “password” /var/log/mysqld.log 可看到当前密码
案例:
[root@cjbCentos01 mysql]# grep "password" /var/log/mysqld.log
2021-10-01T10:09:39.951331Z 1 [Note] A temporary password is generated for root@localhost: b+IOpn<r5dk/
[root@cjbCentos01 mysql]#
9.运行 mysql -u root -p ,用root用户登录,提示输入密码可用上述的,可以成功登陆进入mysql命令行
10. 设置root密码,对于个人开发环境,要设比较简单的密码(生产环境服务器要设复杂密码),可以运行
set global validate_password_policy=0; 提示密码设置策略
(validate_password_policy默认值1,)
查看数据库当前的密码策略: show VARIABLES like “%password%”;
mysql> show VARIABLES like "%password%";
+---------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------+-------+
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM|
| validate_password_special_char_count | 1 |
+---------------------------------------+-------+
14 rows in set (0.00 sec)
set global validate_password_length=0; 更改密码长度
案例:
mysql> set global validate_password_policy=0; # 更改密码策略为LOW,只要求长度(默认8位)
Query OK, 0 rows affected (0.00 sec)
mysql> set password for 'root'@'localhost' =password('123456');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_length=0; # 更改密码长度
Query OK, 0 rows affected (0.00 sec)
11 设置root密码 set password for ‘root’@‘localhost’ =password(‘123456’);
12. 运行 flush privileges; 使密码设置生效
13. 退出MySQL命令行(可使用\q、QUIT或EXIT),使用新密码登录