MySQL运维实战(2.2)忘记密码如何处理

俊达3年前技术文章1194

如果忘记了一个普通用户的密码,可以使用管理员账号登录,修改其他用户的密码。

但是如果所有管理员账号的密码都忘记了,应该怎么处理呢?


如果忘记root密码,可以使用skip-grant-tables参数启动数据库,重新设置root密码。

使用skip-grant-tables启动实例后,所有人都可以免密码登陆数据库,并且拥有所有权限。

使用时可以配合bind-address或skip-networking参数一起使用,只允许从本机登陆数据库。



具体操作步骤:

1、停止数据库实例。

停止mysql实例。可以使用多种方法停止实例。这里使用serivce停止mysql

service mysqld stop


2、配置文件中增加skip-grant-tables和skip-networking参数

# /etc/my.cnf
skip-grant-tables
skip-networking


3、重新启动实例

2021-04-06T15:53:17.250207Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.32'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community Server (GPL)


4、登陆数据库

由于加了skip-networking参数,只能通过socket登陆数据库

[root@box1 ~]# mysql -h 127.0.0.1
ERROR 2003 (HY000): Cant connect to MySQL server on '127.0.0.1' (111)

[root@box1 ~]# mysql  -S /var/lib/mysql/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>


5、重新加载权限

实例使用skip-grant-tables参数启动,无法直接修改账号密码。需要执行flush privileges后才能修改密码。

使用alter user或set password命名修改密码。

mysql> alter user 'root'@'localhost' identified by 'helloww';
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)


mysql> alter user 'root'@'localhost' identified by 'helloww';
Query OK, 0 rows affected (0.00 sec)


mysql> set password for 'root'@'localhost' = 'helloww';
Query OK, 0 rows affected (0.00 sec)


mysql> set password for 'root'@'localhost' = password('helloww');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                                 |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SET PASSWORD FOR <user> = PASSWORD('<plaintext_password>')' is deprecated and will be removed in a future release. Please use SET PASSWORD FOR <user> = '<plaintext_password>' instead |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


6、修改参数,重新启动数据库

[root@box1 ~]# sed -i '/skip-networking/d' /etc/my.cnf
[root@box1 ~]# sed -i '/skip-grant-tables/d' /etc/my.cnf
[root@box1 ~]# grep skip /etc/my.cnf
[root@box1 ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service


[root@box1 ~]# tail -2 /var/log/mysqld.log
2021-04-06T16:02:20.527113Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.32'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)


7、验证数据库恢复正常

重启后需要使用密码才能登陆数据。

[root@box1 ~]# mysql -uroot -h127.0.0.1
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@box1 ~]# mysql -uroot -h127.0.0.1 -phelloww
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>




相关文章

MySQL运维实战之ProxySQL(9.5)proxysql和MySQL Group Replication配合使用

如果后端MySQL使用了Group Replication,可通过配置mysql_group_replication_hostgroups表来实现高可用mysql_group_replication_...

MySQL运维实战之备份和恢复(8.1)xtrabackup全量备份

xtrabackup是percona开源的mysql物理备份工具。xtrabackup 8.0支持mysql 8.0版本的备份和恢复。xtrabackup 2.4支持mysql 5.7及以下版本的备份...

MySQL运维实战(5.1) 字符和编码的基本概念

MySQL运维实战(5.1) 字符和编码的基本概念

字符和编码字符字符是符号,是人们用于交流的各类符号,如26个英文字母、汉字、标点符号、数学运算符、其他语言的字母和符号。编码编码是计算机中以二进制方式存储字符的方式。字符集字符集是字符和编码的映射表。...

MySQL运维实战之ProxySQL(9.4)proxysql和后端MySQL自动切换

MySQL运维实战之ProxySQL(9.4)proxysql和后端MySQL自动切换

如上图架构,当后端MySQL主库出现问题,发生主备切换后,如何自动将ProxySQL的读写切换到新的主库上?可以通过mysql_replication_hostgroups表配置实现:insert&n...

 MySQL优化器特性(九)行数评估

MySQL优化器特性(九)行数评估

查询的行数在成本计算中起了很重要的作用:1、row_evaluate_cost和行数直接相关2、需要访问多少索引页面,和行数直接相关。根据页面大小和平均索引条目长度计算每个索引页面的记录数,根据记录数...

MySQL优化器特性(七)成本估算常数

成本估算常数表示执行一些MySQL基础操作时的成本,如读取一个页面,创建一个临时表,比较一条记录,解析一行记录等操作。mysql.engine_cost和mysql.server_cost表分别记录存...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。