解决MySQL忘记root密码

网上有很多关于忘记MySQL root密码的一些文章,里面都有写怎么去解决,但有时觉得写得太恶心,要么一字不漏的抄别人的,要么就说得不清不楚,好了,不吐槽了,以下是解决的整个过程。

首先我们要知道忘记MySQL root密码后,能否重启mysql,能重启的操作是怎么样的?不能重启的操作又会是怎么样的?

 

情况一:(能重启情况下)

修改my.cnf配置文件,在mysqld栏下添加skip-grant-tables选项,意思是mysqld server启动之后并不使用权限系统(privilege system),也可以理解为跳过授权表。为了安全起见,通常加上skip-networking,mysqld不侦听任何TCP/IP连接请求。

重启mysqld,然后空密码连接:

[root ~]$mysql -uroot -S /data/mysql-5.5/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.40-log 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> 

可以看到已经成功登录了,然后修改root密码:

mysql> update mysql.user set password=password('123456') where user='root';    
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges; 
Query OK, 0 rows affected (0.01 sec)

已经成功修改密码了,但还有事情要做,就是把刚刚添加到my.cnf里的skip-grant-tables和skip-networking删除掉或者注释掉。

 

情况二:(不能重启mysql的情况)

如果不能重启,mysql.user 刚好有权限比较低的用户,如果没有,你请神仙来帮你吧,哈哈

1、为了测试,我自己创建一个用户,可以没什么权限

mysql> create user xuanzhi@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

2、进到数据目录下:

[root mysql-5.5]$ pwd
/data/mysql-5.5
[root mysql-5.5]$ cp mysql/user.* test/
[root mysql-5.5]$ chown mysql.mysql test/user.*

3、用权限比较小的用户登录:

[root mysql-5.5]$mysql -uxuanzhi -p123456 -S /data/mysql-5.5/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.40-log 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> use test
Database changed
mysql> update user set password=password('123123') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

4、把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。

[root mysql-5.5]$ pwd
/data/mysql-5.5
[root mysql-5.5]$ mv mysql/user.MYD mysql/user.MYD.bak
[root mysql-5.5]$ mv mysql/user.MYI mysql/user.MYI.bak
[root mysql-5.5]$ cp test/user.MY* mysql/
[root mysql-5.5]$ chown mysql:mysql  mysql/user.*

5.查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。(有时加载一次不行的时候,再加载多一次@。@)

[root mysql]$ pgrep -n mysql
23166
[root mysql]$ kill -SIGHUP 23166
[root mysql]$ /usr/local/mysql-5.5.40/bin/mysql -uroot -p123123 -S /data/mysql-5.5/mysql.sock 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root mysql]$ kill -SIGHUP 23166
[root mysql]$ /usr/local/mysql-5.5.40/bin/mysql -uroot -p123123 -S /data/mysql-5.5/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.40-log 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> 

 

不重启的第二种方法

1、创建新的数据目录,并给原来user相应的权限,忘记密码对应的实例它的user是mysql,所以把权限给mysql用户

[root data]$ mkdir -pv /dbdata/datadir/
mkdir: 已创建目录 "/dbdata"
mkdir: 已创建目录 "/dbdata/datadir/"
[root data]$ chown  -R mysql:mysql /dbdata/datadir/

2、执行初始化操作:(报错了)

[root scripts]$ pwd
/usr/local/mysql-5.5.40/scripts
[root scripts]$ ./mysql_install_db --datadir=/dbdata/datadir/ --user=mysql2FATAL ERROR: Could not find ./bin/my_print_defaultsIf you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.

解决方法:

[root scripts]$ /usr/local/mysql-5.6.10/scripts/mysql_install_db --datadir=/dbdata/datadir/ --user=mysql --datadir=/dbdata/datadir/ --basedir=/usr/local/mysql-5.6.10/
Installing MySQL system tables...
141210 16:09:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
OK
Filling help tables...
141210 16:09:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
OK

3、启动一个新的进程,这里要注意一下port,sock文件,还有pid文件,这都是新的,user还是忘记密码实例的user,而不是忘记密码对应的那个数据库实例的,这里我们不需要用到InnoDB引擎,设置默认引擎为MyISAM:

[root ~]$  /usr/local/mysql-5.6.10/bin/mysqld_safe --datadir=/dbdata/datadir --plugin-dir=/usr/local/mysql-5.6.10/lib/plugin/  --skip-innodb \
> --default-storage-engine=myisam --socket=/dbdata/datadir/mysql2.sock --user=mysql --port=3305 --log-error=/dbdata/datadir/error2.log --pid-file=/data/mysql-5.6/mysql.pid &
[1] 21204
[root ~]$
141210 16:56:11 mysqld_safe Logging to '/dbdata/datadir/error2.log'. 141210 16:56:11 mysqld_safe Starting mysqld daemon with databases from /dbdata/datadir

4、登录新启动的mysql实例,此时密码为空密码:

root datadir]$ /usr/local/mysql-5.6.10/bin/mysql -S /dbdata/datadir/mysql2.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.10-log Source distributionCopyright (c) 2000, 2013, 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> flush tables;
Query OK, 0 rows affected (0.00 sec)

修改root密码:

mysql> select user, host, password from user where user like 'root';
+------+-----------------------+----------+
| user | host                  | password |
+------+-----------------------+----------+
| root | localhost             |          |
| root | localhost.localdomain |          |
| root | 127.0.0.1             |          |
| root | ::1                   |          |
+------+-----------------------+----------+
4 rows in set (0.02 sec)mysql> update mysql.user set password=password('654321') where user='root'; 
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5、拷备新的user表到忘记密码的实例数据库的mysql目录下

[root mysql]$ pwd
/dbdata/datadir/mysql
[root mysql]$ cp user.* /data/mysql-5.6/mysql/
cp:是否覆盖"/data/mysql-5.6/mysql/user.frm"? y
cp:是否覆盖"/data/mysql-5.6/mysql/user.MYD"? y
cp:是否覆盖"/data/mysql-5.6/mysql/user.MYI"? y
[root mysql]$ chown -R mysql5.6:mysql5.6 /data/mysql-5.6/
[root mysql]$ chmod 660 /data/mysql-5.6/mysql/user.*

6、我们需要到mysqld发送一个sighup信号,MySQL响应这个信号加载授权表,刷新表,日志,线程缓存。
如果是单个MySQL实例,可以用这样的方法去重新加载

[root ~]$ kill -1 $(/sbin/pidof mysqld)

如果是多个MySQL实例在一台服务器上的话,就要注意点了,可以通过这样的方法找到pid,我旧实例的端口是3306:

[root mysql-5.6.10]$ netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      8414/mysqld         
tcp        0      0 0.0.0.0:3308                0.0.0.0:*                   LISTEN      6430/mysqld         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1144/sshd           
      
tcp        0      0 :::3310                     :::*                        LISTEN      17151/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      1144/sshd           
tcp        0      0 ::1:631                     :::*                        LISTEN      1109/cupsd          
tcp        0      0 :::3306                     :::*                        LISTEN      2091/mysqld
[root mysql-5.6.10]$ kill -1 2091

有时kill -1一次不行,再执行一次就可以了:

[root mysql-5.6.10]$ kill -1 2091
[root mysql-5.6.10]$ /usr/local/mysql-5.6.10/bin/mysql -uroot -p654321 -S /data/mysql-5.6/mysql.sock 
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root mysql-5.6.10]$ kill -1 2091
[root mysql-5.6.10]$ /usr/local/mysql-5.6.10/bin/mysql -uroot -p654321 -S /data/mysql-5.6/mysql.sock 
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 13
Server version: 5.6.10-log MySQL Community Server (GPL)Copyright (c) 2000, 2013, 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> 

OK,已经成功登录了,如果有更多好的方法,我们可以再一起讨论下

 

 

总结:

1)第一种方法简单,但需要重启MySQL,重启会影响线上业务,一般不建议重启

2)第二种方法比较好,不用重启MySQL实例,修改密码,只修改root用户的,而且其它保持不变

3)第三种方法也不需要重启,但是新的user表里,只有root一个用户,如果之前服务器还存在别的用户及权限,这就比较麻烦了

 

 

 参考资料:http://www.percona.com/blog/2014/12/10/recover-mysql-root-password-without-restarting-mysql-no-downtime/

PS:本人也是参考别人的博客做的,但我没有照搬别人的东西,太恶心了,希望大家有自己的风格。^.^

 

作者:陆炫志

出处:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111

您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。

转载于:https://www.cnblogs.com/xuanzhi201111/p/4138352.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SpringCloud Sleuth分布式请求链路追踪

概念 1. 为什么需要链路追踪? 在微服务框架中,一个由客户端发起的请求在后端系统中 会经过多个不同的的服务节点调用来协同产生最后的请求结果, 每一个前段请求都会形成一复杂的分布式服务调用链路, 链路中的任何一环出现高延时或错误都会引起整个请求最…

前端学习(1292):文件写入操作

const fs require(fs);fs.writeFile(./demo.txt, 即将要写入的内容, err > {if (err ! null) {console.log(err);return;}console.log(文件内容写入成功); }) 运行结果

Android中如何下载文件并显示下载进度

原文地址:http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1125/2057.html 这里主要讨论三种方式:AsyncTask、Service和使用DownloadManager。 一、使用AsyncTask并在进度对话框中显示下载进度 这种方式的优势是你可以在后台执行下载任务的同时…

前端学习(1293):系统模块path路径操作

//导入path模块 const path require(path); //路径拼接 const finaPath path.join(public, uploads, avater); console.log(finaPath); 运行结果

nacos作为服务注册中心

nacosnacos简介nacos 作为服务注册中心demo基于Nacos的服务提供者基于Nacos的服务消费者nacos切换ap和cp 模式nacos简介 为什么叫nacos 前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service。是什么 一个更易于构建云原生应用的动态服务发现&am…

前端学习(1294):相对路径和绝对路径

const fs require(fs); const path require(path); console.log(__dirname); console.log(path.join(__dirname, ./demo01.js)); fs.readFile(path.join(__dirname, ./demo01.js), utf8, (err, doc) > {console.log(err);console.log(doc); }) 运行结果

layui 数据表格代码

一套增删改查&#xff0c;打完收工。 layui版本&#xff1a;2.4.5 默认请求,分页。 /rest_address?page1&limit1json数据格式要求. 参数说明文档 https://www.layui.com/doc/modules/table.html#cols <!DOCTYPE html> <html lang"en" xmlns:th"…

[BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]

题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态&#xff0c;需要使用优秀的搜索方式和一些优化技巧。 我使用的是 IDA* 搜索&#xff0c;从小到大枚举步数&#xff0c;每次 DFS 验证在当前枚举的步数之内能否到达目标状态。 如果不能到达&#xff0…

nacos服务配置中心演示

config centerNacos作为配置中心-基础配置Nacos作为配置中心-分类配置nacos将配置持久化到mysql新型技术&#xff0c;替代spring config center & bus Nacos作为配置中心-基础配置 ⑴ module cloudalibaba-config-nacos-client3377 (2) pom <dependencies><!--n…

前端学习(1296):第三方模块nodemon

修改保存重新执行 如何断开ctrlc

core java 8~9(GUI AWT事件处理机制)

MODULE 8 GUIs--------------------------------GUI中的包&#xff1a; java.awt.*; javax.swing.*; java.awt.event.*; 要求:1)了解GUI的开发流程&#xff1b;2&#xff09;掌握常用的布局管理器 开发GUI图形界面的步骤-------------------------------1.选择容器 1&#xff0…

note.. redis五大数据类型

redis 五大数据类型使用nosql介绍&#xff0c;由来什么是nosql阿里巴巴的架构nosql 四大分类redis入门概述redis 安装 &#xff08;docker&#xff09;基础的知识redis五大数据类型Redis-KeyStringList (列表)Set &#xff08;集合&#xff09;Hash(哈希)Zset 有序集合nosql介绍…

Arcengine 基本操作(待更新)

/// <summary>/// 删除fieldName属性值为1的弧段/// </summary>/// <param name"fieldName"></param>/// <param name"t"></param>public void DelectPolyline(string fieldName, int t){ILayer pLayer axMapControl…

redis 三种特殊数据类型

三种特性数据类型 geospatial 定位&#xff0c;附近的人&#xff0c;打车距离计算。 redis的geo在redis3.2版本就推出了。可推算地理位置的信息&#xff0c;两地之间的距离&#xff0c;方圆几里的人。 6个命令。 GEOADD GEODIST GEOHASH GEOPOS GEORADIUS GEORADIUSBYMEMB…

前端学习(1298):gulp使用

第一步安装 第二步建立文件夹 第三部 src放源代码 第四步 输入代码 执行

Sentinel 分布式系统的流量防卫兵

sentinelsentinel base服务编写关键名词解释sentinel base 官网&#xff1a; https://github.com/alibaba/Sentinel https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D 是什么&#xff1f; 是一款优秀的限流&#xff0c;降级&#xff0c;熔断的框架。 Sentinel …

php查询mysql返回大量数据结果集导致内存溢出的解决方法

web开发中如果遇到php查询mysql返回大量数据导致内存溢出、或者内存不够用的情况那就需要看下MySQL C API的关联,那么究竟是什么导致php查询mysql返回大量数据时内存不够用情况&#xff1f; 答案是: mysql_query 和 mysql_unbuffered_query 两个函数 首先来分析一个典型的实例:…