1. 程式人生 > >首次登入mysql(5.7)或忘記密碼解決方法【ubuntu】

首次登入mysql(5.7)或忘記密碼解決方法【ubuntu】

使用debian-sys-maint登入系統,修改密碼

檢視debian-sys密碼

[email protected]:/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登入並修改密碼

[email protected]:/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;

重新整理許可權後,quit退出會話。

使用新設定的root密碼登入root

[email protected]:/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)

成功修改root密碼並登入