> 数据库 > MySQL >

【ubuntu】登录mysql(5.7)或忘记密码解决方法

使用debian-sys-maint登录系统,修改密码

查看debian-sys密码
it@it.net.cn:/etc/mysql$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = uLz3xiuwdnpAmWt4
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = uLz3xiuwdnpAmWt4
socket   = /var/run/mysqld/mysqld.sock

记住上面user和password

使用debian-sys-maint登录并修改密码
it@it.net.cn:/etc/mysql$ mysql -u debian-sys-maint -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)


成功登陆mysql

mysql> update mysql.user set authentication_string=password('root') where user='root' and Host ='localhost';
mysql> update user set plugin="mysql_native_password";
mysql> flush privileges;
1
2
3
刷新权限后,quit退出会话。

使用新设置的root密码登录root
it@it.net.cn:/etc/mysql$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)


(责任编辑:IT)