安装
#使用wget命令下载MySQL 5.7的安装包rpm文件。
[root@slave1 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@slave1 ~]# ls
anaconda-ks.cfg mysql57-community-release-el7-11.noarch.rpm
#使用rpm命令安装MySQL 5.7的rpm包。
[root@slave1 ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#使用yum命令安装MySQL服务器,--nogpgcheck选项表示跳过GPG检查。
[root@slave1 ~]# yum -y install mysql-server --nogpgcheck
#查看已安装的MySQL版本。
[root@slave1 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using EditLine wrapper
[root@slave1 ~]# systemctl start mysqld
[root@slave1 ~]# systemctl status mysqld
修改密码
#查看MySQL日志文件中生成的临时密码。
[root@slave1 ~]# cat /var/log/mysqld.log| grep password
2023-09-01T06:24:20.556937Z 1 [Note] A temporary password is generated for root@localhost: m3<S2tk(jgxp
[root@slave1 ~]# mysql -uroot -pm3<S2tk(jgxp
#即可登录成功。
#修改密码
mysql> ALTER USER "root"@"localhost" IDENTIFIED BY "123456";
mysql> FLUSH PRIVILEGES;
忘记密码并修改密码
#将"skip-grant-tables"设置添加到MySQL配置文件(/etc/my.cnf),该设置允许在未输入密码的情况下登录到MySQL。
#[root@slave1 ~]# echo skip-grant-tables >> /etc/my.cnf
[root@slave1 ~]# vim /etc/my.cnf
#validate_password_length=0
#validate_password_policy=0
skip-grant-tables
[root@slave1 ~]# systemctl restart mysqld
#使用root用户登录MySQL shell。
[root@slave1 ~]# mysql
#修改root用户的密码为123456。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#注意:如果在执行该步骤的时候出现 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 错误。则执行下 flush privileges; 命令,再执行该命令即可。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit;
Bye
[root@slave1 ~]# mysql -uroot -p123456
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.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit;
Bye
[root@slave1 ~]#
优化
删掉 /etc/my.cnf 里的 skip-grant-tables
[root@slave1 ~]# sed -i -e '/skip-grant-tables/d' /etc/my.cnf
[root@slave1 ~]# tail /etc/my.cnf
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[root@slave1 ~]# systemctl restart mysqld