mysql 忘记root_解决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 idis 3Server version:5.5.40-log MySQL Community Server (GPL)
Copyright (c)2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracleis 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.00sec)
Rows matched:4 Changed: 4 Warnings: 0mysql> 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 connectionid is 3Server 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 clearthe current input statement.
mysql>use test
Database changed
mysql> update user set password=password('123123') where user='root';
Query OK,4 rows affected (0.00sec)
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 mysql23166[root mysql]$kill -SIGHUP 23166[root mysql]$/usr/local/mysql-5.5.40/bin/mysql -uroot -p123123 -S /data/mysql-5.5/mysql.sock
ERROR1045 (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 connectionid is 5Server 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 clearthe 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=mysql2
FATAL ERROR: Could notfind ./bin/my_print_defaults
If you compiled from source, you need to run'make install'to
copy the software into the correct location readyforoperation.
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 ina future release.
OK
Filling help tables...141210 16:09:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed ina 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
Welcometo the MySQL monitor. Commands end with ; or\g.
Your MySQL connection idis 1Server version:5.6.10-logSource distribution
Copyright (c)2000, 2013, Oracle and/or its affiliates. Allrights reserved.
Oracleis a registered trademark of Oracle Corporation and/orits
affiliates. Other names may be trademarksoftheir respective
owners.
Type'help;' or '\h' for help. Type '\c' to clear the currentinput 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.02sec)
mysql> update mysql.user set password=password('654321') where user='root';
Query OK,4 rows affected (0.03sec)
Rows matched:4 Changed: 4 Warnings: 0mysql> 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"? ycp:是否覆盖"/data/mysql-5.6/mysql/user.MYD"? ycp:是否覆盖"/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
tcp0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 8414/mysqld
tcp0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 6430/mysqld
tcp0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1144/sshdtcp0 0 :::3310 :::* LISTEN 17151/mysqld
tcp0 0 :::22 :::* LISTEN 1144/sshd
tcp0 0 ::1:631 :::* LISTEN 1109/cupsd
tcp0 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.
ERROR1045 (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 connectionid is 13Server 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 clearthe current input statement.
mysql>
OK,已经成功登录了,如果有更多好的方法,我们可以再一起讨论下
总结:
1)第一种方法简单,但需要重启MySQL,重启会影响线上业务,一般不建议重启
2)第二种方法比较好,不用重启MySQL实例,修改密码,只修改root用户的,而且其它保持不变
3)第三种方法也不需要重启,但是新的user表里,只有root一个用户,如果之前服务器还存在别的用户及权限,这就比较麻烦了
PS:本人也是参考别人的博客做的,但我没有照搬别人的东西,太恶心了,希望大家有自己的风格。^.^
作者:陆炫志
您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。
mysql 忘记root_解决MySQL忘记root密码相关推荐
- mysql8.1改密码,mysql 8.0以上版本修改root密码的方法
昨天发布了一篇关于在win系统下安装配置mysql的教程,今天就有同学来咨询 mysql 8.0 以上的版本如何去修改 root 账号的密码.由于 mysql 8.0 以上版本的密码策略得到了加强,网 ...
- mysql.sock作用-解决mysql.sock直接找不到了的问题-重新生成mysql.sock
= = = mysql.sock作用-解决mysql.sock直接找不到了的问题-重新生成mysql.sock mysql.sock作用 mysql的连接方式有两种: 1.通过TCP/IP的连接 ...
- linux系统root改密码忘记了怎么办,linux系统root密码忘记修改办法
linux系统Root密码忘了怎么办?--解决办法(图解请下载附件) Root密码忘了就这么办!这个方法菜鸟实践证明可行! 1.在开机启动的时候能看到这个引导目录(3秒将进入默认),用上下方向键选择你 ...
- mac安装mysql修改密码_Mac下安装MySQL 5.7.28并且修改root密码-Go语言中文社区
0.前言 mysql8安装:https://blog.csdn.net/qq_21383435/article/details/80577038 1.下载 地址:https://www.mysql.c ...
- mysql笔记一——安装和设置root密码
1. mysql 5.6安装包下载. MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提 ...
- linux系统修改用户名密码忘记,Linux下修改/找回root密码
一.Linux下修改一般用户的密码 1.如果没有忘记自己的密码,进行修改: #输入passwd 之后会提示输入新密码 输入之后修改成功! 2.以 root身份修改一般用户的密码:# passwd us ...
- mysql 关闭swap_解决MySQL经常停止运行(swap分区设置) | kTWO-个人博客
背景 自从搭建了这个个人博客,MySQL就经常性的奔溃,前面写过一篇文章<记一次解决MySQL经常停止运行的问题>,在文章中对MySQl的错误日志进行了分析并对错误做出了相应的修改,不错, ...
- mysql报1862_解决mySQL中1862(phpmyadmin)/1820(mysql)错误的方法
发现问题 之前一直运行的好好的,突然mysql就无法工作了.请求命令后报错误:error 1820 (hy000): you must set password before executing th ...
- 试题导入mysql乱码_解决Mysql导入乱码问题
解决Mysql导入乱码问题 方法一: 通过增加参数 –default-character-set = utf8 解决乱码问题 C:\Users\Administrator>mysql -uroo ...
最新文章
- ++i和i++效率谁高
- Linux系统中网络配置详解
- RabbitMQ整合SpringBoot(web)
- 微软觊觎LinkedIn算法
- 自动装箱自动拆箱java,自动装箱?拆箱?==问题?详解java面试常见的一个问题...
- javacc解析json报错
- java中方法体的作用
- ubuntu14.04安装vnc
- Kafka连接SparkStreaming的两种方式
- faster RCNN之RPN详解
- python的def什么意思_「Python基础」def是什么?如何自定义函数def
- 【抢头条】迄今最全的无刷电机工作及控制原理分享-绝对...!
- jesd204b高速ad/da ad9172 AD9689调试记录
- rails 杂记 - erb 中的 form_helper
- php echo eot,(基础篇)php中理解print EOT分界符和echo EOT的用法区别
- 大学四年的生活规划——做一个清醒的奋斗者
- 华为p10测试软件,华为p10内存测试软件
- WIN32 opengl缩放、旋转、移动图形
- 考研380分什么水平计算机,考研380分属于什么水平 算高分吗
- 阿龙的学习笔记---3.26---常用的各种树
热门文章
- 已锁定计算机,计算机锁定怎么解除_计算机已锁定如何解除
- mysql合并表快速去重_MySQL数据表合并去重的简单实现方法
- Java随机数控制范围
- 使用计算机的好处,电脑的好处
- python计算机知识点,最新最全Python基础的知识点复习完整版.pdf
- java window linux_java环境变量配置(Windows Linux)
- 关于点名的简单python编程_如何用python编写一个简易的随机点名软件
- 九十六、双指针和滑动窗口算法模板
- 那年大一在图书馆作死的大学高数笔记 | 导数和微分
- 三十二、VsCode前端的开发工具介绍和使用