MySQL 忘记root密码的两种处理方法
【背景】
由于各个原因,我遇到过不只一次我服务的客户忘记了MySQL的root密码;如果是普通用户还好,我们可以用root用户去改它的密码,要命
的是把root给丢了!
对于MySQL来说如果你忘记了root密码,但是你又想通过改密码的方式把root密码找回来的话,你就要作好重启的准备了。
【方法一: skip_grant_tables + skip-networking 两次重启】
1): 第一步把MySQL给关掉
ps -ef | grep mysql
mysql : ? :: /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf kill -
2): 以skip-grant-tables skip-networking 模式启动 mysqld
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &
3): 进入mysql
如果是mysql-5.7的话密码的hash值保存在了authentication_string这个列里面,5.7之前的版本保存在password列里面
select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *91B73478B18B04D13F6926FAB5A6178250EAB697 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| monitor | 127.0.0.1 | *DAD735712BB263A8DA12A091AABC625FE99DD344 |
| root | 127.0.0.1 | *91B73478B18B04D13F6926FAB5A6178250EAB697 |
| backup | 127.0.0.1 | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| backup | localhost | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| appuser | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
8 rows in set (0.01 sec)
采用直接更新密码hash值的方式来更新密码
update mysql.user set authentication_string = password('MTls0352') where user='root'; -- 更新密码为MTls0352
Query OK, 2 rows affected, 1 warning (1.01 sec)
Rows matched: 2 Changed: 2 Warnings: 1
select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *597B32612905C92ABC495354FC276D24D0A541C1 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| monitor | 127.0.0.1 | *DAD735712BB263A8DA12A091AABC625FE99DD344 |
| root | 127.0.0.1 | *597B32612905C92ABC495354FC276D24D0A541C1 |
| backup | 127.0.0.1 | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| backup | localhost | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| appuser | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
8 rows in set (0.00 sec)
4): 重启mysqld
pkill mysql /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &
5): 用新的密码进入MySQL
mysql -uroot -pMTls0352
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 2
Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.01 sec)
【方法二:init-file + 一次重启】
1): 创建用于修改root密码的sql文件
touch /tmp/change_password.sql
内容如下
alter user root@'127.0.0.1' identified by 'mtls0352';
alter user root@'localhost' identified by 'mtls0352';
2): 关闭mysql服务
pkill mysqld # 我的主机上只有一个mysql服务所以用pkill mysqld 没有问题,如果你是单机多实例请用 kill $MYSQLPID
3): 代入修改密码的init-file来启动MySQL服务
/usr/local/mysql/bin/mysqld --init-file=/tmp/change_password.sql &
4): 用新密码登录MySQL
mysql -uroot -pmtls0352
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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
rows in set (0.00 sec)
5): 删除修改密码的sql文件
rm /tmp/change_password.sql
【我的评介】
a: 第一种方法比较传统,第二种方法“角度刁钻” 但是两个都能解决问题;并且第二种方法看起来步骤又少一些,但是这并不只是问题的全部
通常一个线上的MySQL实例并不是以“/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &” 这样直接下命令的方式启动的,它们
通常和service ,systemctl 一起用的;所以这种情况下针对“方法二”还是要用service 或 systemctl把数据拉起来的,不然就不能用它们来管理
了,所以“方法二”最终也是要两次重启的。
b: 方法二能使用“alter user”语法,dba就可以不用care密码到底保存在mysql.user表中的那个列。
两种方法各有好处,所以重点还是要把一个方法搞的出神入画!
【学习交流】
-----------------------------http://www.sqlpy.com-------------------------------------------------


-----------------------------http://www.sqlpy.com-------------------------------------------------
MySQL 忘记root密码的两种处理方法的更多相关文章
- MySQL忘记root密码不重启mysqld的方法
MySQL忘记root密码不重启mysqld的方法 1.首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改zabbix数据库)或者其他相同版 ...
- mysql忘记root密码解决办法
最近项目中的数据库我放在了服务器上,但是今天突然不能用了,进入服务器查看,果然是数据库不能进去了,所以今天来分享一个mysql忘记root密码的解决方案: 1.让mysql不载入权限表,命令:mysq ...
- MySQL 5.7 Command Line Client输入密码后闪退和windows下mysql忘记root密码的解决办法
MySQL 5.7 Command Line Client输入密码后闪退的问题: 问题分析: 1.查看mysql command line client默认执行的一些参数.方法:开始->所有程序 ...
- Ubuntu下MySQL忘记root密码重置
MySQL忘记root密码肿么办?-_-||| 这种情况虽然不是很常见,但是有时长时间没有登录系统,还真会忘记密码.这时候,如果您能以系统管理员权限登陆密码,那还是有救的.放大招,将其重置即可. ...
- linux下mysql忘记root密码怎么办
Linux下MySQL忘记root密码怎么办? Linux下MySQL忘记root密码怎么办? 1. 修改MySQL配置文件 默认MySQL的配置文件为/etc/my.cnf,在[mysqld]下面添 ...
- 附录:MySQL忘记root密码
中小型规模网站集群架构:MySQL忘记root密码 : 矮哥linux运维群:93324526 前言 你忘记系统root密码的时候,你怎么解决的? 不就是single用户进行修改密码吗?这里原理是类似 ...
- windows下mysql忘记root密码的解决办法
今天早上 一朋友说自己的mysql 忘记root密码了 让我帮忙给看看,因为没有接触过mysql 所以从网上找了一下信息经我亲身实践 已经成功!mysql版本是5.1以下是从网上找的信息: 1. 首 ...
- mysql忘记root密码的解决方法
Windows下mysql忘记root密码的解决方法 1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令:net stop mysql 或者在windows任 ...
- skip-grant-tables 修改linux的mysql忘记root密码
skip-grant-tables 修改linux的mysql忘记root密码 今天修改mysql中的admin用户权限,在执行update user set host =' %' where use ...
随机推荐
- SpringMVC Controller配置方法有哪几种
第一种 URL对应Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 表示将请求的URL和Bean名字映射--> <bean class="org.s ...
- Java多线程中的内存模型
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6536131.html 一:现代计算机的高速缓存 在计算机组成原理中讲到,现代计算机为了匹配 计算机存储设备的 ...
- An Objective-C Error
Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'long *') from 'int' 主要是因 ...
- 【超精简JS模版库/前端模板库】原理简析 和 XSS防范
使用jsp.php.asp或者后来的struts等等的朋友,不一定知道什么是模版,但一定很清楚这样的开发方式: <div class="m-carousel"> < ...
- Spring DataSource>DBCP & C3P0
Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...
- Android插件化开发之解决OpenAtlas组件在宿主的注冊问题
OpenAtlas有一个问题,就是四大组件必须在Manifest文件里进行注冊,那么就必定带来一个问题,插件中的组件都要反复在宿主中注冊.像Service,ContentProvider等组件眼下没有 ...
- django之创建第7-2个项目-url配置分离
1.urls.PY分离 # -*- coding: UTF-8 -*- from django.conf.urls import patterns, include, url # Uncomment ...
- JVM常见面试题
1. 内存模型以及分区,需要详细到每个区放什么. 栈区: 栈分为java虚拟机栈和本地方法栈 重点是Java虚拟机栈,它是线程私有的,生命周期与线程相同. 每个方法执行都会创建一个栈帧,用于存放局部变 ...
- Android 本地播放器
发布时间:2018-09-06 技术:Glide+pinyin4j+SwipeDelMenuLayout 概述 这是一款Android 端的本地音乐播放器,界面风格有模仿网易云音乐.bilib ...
- masonry瀑布流的使用
今天在使用masonry.pkgd.min.js瀑布流的时候遇到一个很奇怪的问题,官网显示正常,而我的就是显示不正确,然后我又查看一遍,原来要加这段代码就ok了,记录一下,怕以后还会遇到这个问题 *, ...