ubuntu系统mysql破解root密码

安装完成mysql后,安装修改的root密码竟然忘记了。

root@yaoyafei:# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

输了N次,是真的记不住了。又不想重新安装,挺麻烦的,只能破解密码
下面是修改密码的教程
首先查看mysql的状态,并关闭服务

root@yaoyafei:# systemctl status mysql
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-09-06 22:43:31 CST; 8min ago
  Process: 30118 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)
  Process: 30109 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 30120 (mysqld)
    Tasks: 28 (limit: 2212)
   CGroup: /system.slice/mysql.service
           └─30120 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
           
Sep 06 22:43:30 yaoyafei systemd[1]: Starting MySQL Community Server...
Sep 06 22:43:31 yaoyafei systemd[1]: Started MySQL Community Server.

关闭mysql服务

root@yaoyafei:# systemctl stop mysql 
root@yaoyafei:# 

修改mysql的配置文件my.cnf ubuntu系统默认在/etc/mysql/mysql.conf.d/mysqld.cnf
在配置文件中的[ mysqld ]下面写入 skip-grant-tables 作用是登录mysql跳过密码验证

[mysqld]
skip-grant-tables
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock

保存并重启mysql , 查看进程存在

root@yaoyafei:~# systemctl start mysql
root@yaoyafei:~# ps aux | grep mysql
mysql    30248  2.6  9.1 1356768 177704 ?      Sl   22:58   0:00 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
root     30277  0.0  0.0  14436  1052 pts/1    S+   22:59   0:00 grep --color=auto mysql

输入命令mysql -u root 进入mysql

root@yaoyafei:~# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

然后进行修改密码 update mysql.user set authentication_string=password(‘新密码’) where user=‘root’;

mysql> update mysql.user set authentication_string=password('新密码') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

打开配置文件删除添加的代码skip-grant-tables,并重启mysql验证新密码。

root@yaoyafei:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf 
root@yaoyafei:~# systemctl restart mysql
root@yaoyafei:~# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值