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

俊达2年前技术文章1109

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

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


如果忘记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运维实战(1.2)安装部署:使用二进制安装部署

MySQL运维实战(1.2)安装部署:使用二进制安装部署

一般在生产环境,我们会使用二进制安装的方式安装MySQL。使用二进制安装,在处理单机多实例、升级MySQL等场景下更加方便。如果有特殊的需求(比如要打一些patch),我们还可以自己编译二进制。1、下...

MySQL运维实战(4.9) SQL_MODE之NO_UNSIGNED_SUBTRACTION

在mysql数据库中,unsigned表示不存负数,如果unsigned类型的字段作运算,得到的结果为负数,SQL会报错。mysql> create table t...

MySQL运维实战(4.4) SQL_MODE之STRICT_TRANS_TABLES和STRICT_ALL_TABLES

如果设置STRICT模式,则如果数据写入时,如果数据不符合字段定义(字符串超出长度、数值类型数据超出范围、违反not null约束等),SQL会报错。如果不设置STRICT模式,会对异常数据进行截断处...

MySQL运维实战(7.2) MySQL复制server_id相关问题

MySQL运维实战(7.2) MySQL复制server_id相关问题

主库server_id没有设置主库没有设置server_idGot fatal error 1236 from master when&nb...

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

恢复全量备份恢复全量备份大致可以分成以下几步:解压备份文件、prepare备份文件、将数据copy到目标实例相关目录、启动数据库实例。解压文件如果备份时使用了xbstream,需要先解压备份文件。我们...

MySQL运维实战(5.3) MySQL数据乱码的一些情况

MySQL运维实战(5.3) MySQL数据乱码的一些情况

表数据乱码当数据的真实编码和相关参数(常见的包括character_set_client, character_set_result, 字段的编码,终端的编码)不一致时,会产生乱码。测试1 - 表中的...

发表评论    

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