安装完成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>
结束。