不重启mysqld更改root密码

Ever found yourself working on a MySQL server where root’s password is unavailable? It has happened to me a few times, always because the person who set up the DB left the place long ago, and this information was not documented anywhere.

If you have root access to the OS, MySQL lets you restart the server bypassing access checks, using the skip-grant-tables option, which requires a service restart.

However, if you need to regain root access and want to minimize service impact, you can take advantage of the way the server responds to SIGHUP signals and the fact that access credentials are stored on a MyISAM table.

MySQL uses a few tables to store credentials and privileges for users (you can find more about this here), but for this procedure, we only need to work with the mysql.user table.

Specifically, we will work with the columns ‘user’, ‘host’ and ‘password’ from this table.

Here’s an example of how this can look on a server:

mysql> select user,host,password from mysql.user;
+-----------+-----------+-------------------------------------------+
| user      | host      | password                                  |
+-----------+-----------+-------------------------------------------+
| root      | localhost | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | mysql     | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | 127.0.0.1 | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | ::1       | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
|           | localhost |                                           |
|           | mysql     |                                           |
| dba       | %         | *4FC8D8270BEC4364C78799065996F5306139B412 |
| readwrite | localhost | *202273E75BD11D06FBE2F057BFA1B1BB2B26549C |
| readonly  | localhost | *FC69E042CE30D92E2952335F690CF2345C812E36 |
+-----------+-----------+-------------------------------------------+
9 rows in set (0.00 sec)

To start, we’ll need to make a copy of this table to a database where we can change it. On this example server, this means the ‘test’ schema, as the ‘readwrite’ user has write privileges on it. Even if root’s password was lost, you can typically get a less privileged MySQL account by checking the applications that connects to this database. If for some reason this is not the case, you can achieve the same results by copying this table to another server, and copying it back after the necessary changes have been made.

The following command happen on the datadir:

[root@mysql mysql]# cp mysql/user.* test/; chown mysql.mysql test/user.*

Please don’t overwrite an existing table when doing this! Rename the copied files as needed instead …

Now you should be able to access (and write) to this table:

[root@mysql mysql]# mysql -ureadwrite -p test
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.6.16 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.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> select user,host,password from user;
+-----------+-----------+-------------------------------------------+
| user      | host      | password                                  |
+-----------+-----------+-------------------------------------------+
| root      | localhost | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | mysql     | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | 127.0.0.1 | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root      | ::1       | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
|           | localhost |                                           |
|           | mysql     |                                           |
| dba       | %         | *4FC8D8270BEC4364C78799065996F5306139B412 |
| readonly  | %         | *FC69E042CE30D92E2952335F690CF2345C812E36 |
| readwrite | %         | *202273E75BD11D06FBE2F057BFA1B1BB2B26549C |
+-----------+-----------+-------------------------------------------+
9 rows in set (0.00 sec)

By now you’ve probably figured out what I’ll do: update test.user, changing the password column for user ‘root’ and host ‘localhost’ to the result of running the PASSWORD() function with some string of my choice, then copying this table back, and then sending SIGHUP to the server.

A couple of caveats:

  • Either make a copy of the original table file, (and?) or write down the original hash for root (the one you will replace)
  • Even if nobody on the customer’s current team knows how to get you MySQL’s root password, that does not mean they don’t have some old app someone has forgotten about that uses the root account to connect. If this is the case, access will break for this app. You can follow the same steps outlined here, but instead of permanently changing root’s password, use your regained access to create a new super user account, and then replace root’s hash with the one you saved (and flush privileges!)

For completion, here’s the rest of the process:

mysql> update test.user set password=password('newpass but this is insecure so dont use') where user = 'root' and host = 'localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select user,host,password from test.user where user='root';
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *0A131BF1166FB756A61317A40F272D6FFDD281E9 |
| root | mysql     | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root | 127.0.0.1 | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
| root | ::1       | *1BD9C328233CF457571A4BB5DB8D32892AB8EDBF |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)mysql>

Time to copy the table back and reload the grant tables:

[root@mysql mysql]# 'cp' test/user.MY* mysql/
[root@mysql mysql]# kill -SIGHUP $(pidof mysqld)

And now you should be able to get back in:

[root@mysql mysql]# mysql -p'newpass but this is insecure so dont use'
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 35
Server version: 5.6.16 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.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> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*0A131BF1166FB756A61317A40F272D6FFDD281E9' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

There you go. We’ve regained root access to MySQL without restarting the service!

I hope you find this useful, and I’ll leave opinions on MySQL’s security as an exercise to the reader …

posted on 2014-09-17 10:54 秦瑞It行程实录 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/ruiy/p/3976632.html

不重启mysqld更改root密码相关推荐

  1. mysql tree 修改_13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令

    更改root密码 ps aux |grep mysql     //先查看一下mysql是否启动了 /etc/init.d/mysqld start    //启动mysql ps aux |grep ...

  2. 设置更改root密码(远程,本地)、连接mysql、mysql常用命令

    设置更改root密码 1.将mysql加入环境变量中 [root@centos7 ~]# grep mysql /etc/profile export PATH=/usr/local/mysql/bi ...

  3. 设置更改root密码 连接mysql mysql常用命令

    一.设置更改root密码 #/etc/init.d/mysqld start #ps aux |grep mysql #mysql -uroot //提示-bash: mysql : 未找到命令 #l ...

  4. 设置更改root密码 ,连接mysql,mysql常用命令

    2019独角兽企业重金招聘Python工程师标准>>> 设置更改root密码 /usr/local/mysql/bin/mysql -uroot 更改环境变量PATH,增加mysql ...

  5. 13.1-13.3 设置更改root密码,连接MySQL,MySQL常用命令

    13.1 设置更改root密码 大纲 准备工作: 1 启动mysql服务 [root@AliKvn ~]# /etc/init.d/mysqld start Starting MySQL.       ...

  6. mysql 更改root密码及 主机_设置更改root密码(远程,本地)、连接mysql、mysql常用命令...

    设置更改root密码 1.将mysql加入环境变量中 [root@centos7 ~]# grep mysql /etc/profile export PATH=/usr/local/mysql/bi ...

  7. mysql root命令_设置更改root密码、连接mysql、mysql常用命令

    目录 一.设置更改root密码 二.连接mysql 三.mysql常用命令 一.设置更改root密码 检查mysql服务是否启动 [root@minglinux-01 ~] ps aux |grep ...

  8. 服务器怎么修改sqlroot密码,mysql设置更改root密码、mysql服务器的连接、mysql常用命令的图解...

    1.设置更改root密码 查看mysql 启动与否,若没启动就运行:/usr/local/mysql56/bin/mysql ps aux |grep mysql 或 netstat -tulnp | ...

  9. 0521MySQL常用操作---设置更改root密码、数据库备份恢复、连接mysql、mysql用户管理...

    2019独角兽企业重金招聘Python工程师标准>>> 13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令 13.4 mysql用户管理 13.5 ...

最新文章

  1. nginx检查配置文件语法是否正常,需要检查主配置文件
  2. linux网络寻址顺序,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  3. 【软考-软件设计师】输入/输出技术
  4. Nature:压榨学生,论资排辈,现行论文作者制度已死
  5. Ext 入门 (05) 打印+gridpanel()方法
  6. Python 数据分析三剑客之 Pandas(十):数据读写
  7. android requestpermissions参数,Android M Permissions:onRequestPermissionsResult()未被调用
  8. redis连接被拒绝
  9. from __future__ import的用法
  10. vlang: 新语言尝试,初生牛犊,未来可期
  11. 查看ip命令 linux centos7,如何centos7查看ip(地址)命令?
  12. 华为交换机 查ip冲突_华为交换机根据ip查端口号
  13. IDEA的快捷键与qq的冲突了怎么办
  14. 嘉兴 机器人仓库 菜鸟_双11前菜鸟网络升级智能仓库 浙江嘉兴仓担当大任
  15. 批量解压多个rar压缩包并将解压出来的文件以该压缩包的名称重命名
  16. TCP 与 UDP 的异同
  17. 移动目标定位技术笔记1:WiFi、ZigBee、UWB技术
  18. linux复制与粘贴操作的快捷键
  19. strcmp和==比较
  20. 豆瓣读书top250爬取

热门文章

  1. 2010年年终“飞”的总结
  2. Latex文本文档的排版
  3. 斐波那契数列(递归和非递归实现)
  4. java ftp连接成功 上传失败_ftp自动上传工具,如何设置及配置ftp自动上传工具
  5. react echarts 绘制带有滑块柱图
  6. android nds模拟器窗口,安卓NDS模拟器drastic模拟器使用经验分享
  7. java 计时 timeclock_用 java 写一个clock的类,100毫秒的时钟 求代码。。越简单越好。。最好有注释...
  8. mysql pgsql 语法_PostgreSQL ALIAS语法
  9. ssh整合mysql不能自动生成表_ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表...
  10. android电视手机遥控器,tcl电视遥控器