1. 程式人生 > >ubuntu安裝mysql 5.7未提示輸入密碼無法登陸的情況

ubuntu安裝mysql 5.7未提示輸入密碼無法登陸的情況

在Ubuntu18.04通過apt-get安裝mysql服務

[email protected]:~$ sudo apt-get install mysql-client-core-5.7

[email protected]:~$ sudo apt-get install mysql-server

安裝完成,中間過程並沒有提示設定mysql的預設密碼。不管了,先用root嘗試登陸mysql試試。

[email protected]:~$ mysql
ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO)

[email protected]
:~$ mysql -u root ERROR 1698 (28000): Access denied for user 'root'@'localhost' [email protected]:~$ mysql -uub64 ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO) [email protected]:~$ mysql -uub64 -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: YES)

提示報錯了,不管是root還是本機使用者名稱,都無法登陸。測試一下重啟服務吧,還是不行。

[email protected]:~$ service mysql start
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'mysql.service'.
Authenticating as: ub64-1804-1 (ub64)
Password:
==== AUTHENTICATION COMPLETE ===

[email protected]
:~$ mysql ERROR 1045 (28000): Access denied for user 'ub64'@'localhost' (using password: NO) [email protected]:~$ ps -ef | grep mysql mysql 14285 1 0 05:14 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

程序服務執行正常,但是還是登陸不上。祭出百度神器,找到同樣的問題。

好了,開啟conf配置檔案,檢視一下預設設定的賬號密碼

[email protected]:~$ sudo view /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = i1OVIwwgMWs8MAKj
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = i1OVIwwgMWs8MAKj
socket   = /var/run/mysqld/mysqld.sock

原來安裝完成後,預設使用的不再是以前的root了,而是使用debian-sys-maint使用者和這一大串隨機密碼,順利登陸。查詢一下現有的使用者情況。


mysql> select host, user, plugin, authentication_string from mysql.user;
+-----------+------------------+-----------------------+-------------------------------------------+
| host      | user             | plugin                | authentication_string                     |
+-----------+------------------+-----------------------+-------------------------------------------+
| localhost | root             |                       |                                         |
| localhost | mysql.session    | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys        | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | mysql_native_password | *2D3B703CBCBE5E317141B9EC7FA780ABB991F93E |
+-----------+------------------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)

接下來為了使用方便,那就是修改一下預設的root賬號的登陸設定了。由於mysql5.7的密碼儲存在authentication_string欄位中,password()方法設定登陸密碼123456。好吧,雖然不知道什麼意思,但是和debian-sys-maint一樣,修改預設的認證方式為"mysql_native_password"。

mysql> update mysql.user set authentication_string=password('123456') where user='root'and Host = 'localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> update mysql.user set plugin="mysql_native_password" where user='root'and Host = 'localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

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

測試使用root使用者登入完成,ok。