1.管理員用戶密碼忘記?PS: 一般不會忘記(>﹏<)
[root@centos7-db01 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
2.關閉數據庫
[root@centos7-db01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@centos7-db01 ~]# systemctl stop mysqld
[root@centos7-db01 ~]# systemctl is-active mysqld
inactive
PS:使用sys-v方式也可以管理數據庫服務。
3.啟用數據庫維護模式
[root@centos7-db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
[1] 2268
[root@centos7-db01 ~]# ps -ef|grep [m]ysqld_safe
root 2268 1429 0 17:39 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking
PS:參數查看方式 mysqld --verbose --help|egrep 'skip-grant-tables|skip-networking'。
說明:
--skip-grant-tables :跳過授權表
--skip-networking :跳過遠程登錄
- 只允許本地登錄,防止在重置密碼期間,用戶通過遠程登錄數據庫,篡改數據記錄、修改授權表信息等操作。
4.登錄並修改密碼
方式1:
[root@centos7-db01 ~]# mysql
mysql> alter user root@'localhost' identified by '123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
PS: flush privileges; 加載授權表,使其管理員擁有更改用戶密碼的權限。
方式2:
[root@centos7-db01 ~]# mysql
mysql> update mysql.user set authentication_string=password('123') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
PS: MySQL5.7 版本使用authentication_string字段保存用戶密碼。
5.關閉數據庫,正常啟動驗證
[root@centos7-db01 ~]# mysqladmin shutdown
[root@centos7-db01 ~]# systemctl start mysqld
#忽略授權表方式登錄
[root@centos7-db01 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#原密碼登錄
[root@centos7-db01 ~]# mysql -uroot -p123456
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
#新密碼登錄
[root@centos7-db01 ~]# mysql -uroot -p123 -e 'select version();'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| version() |
+-----------+
| 5.7.22 |
+-----------+
