1. 程式人生 > >mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000):

mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000):

使用Ubuntu16.04出現的問題:mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2),原因是因為丟失mysqld.sock檔案。

[email protected]:~$ mysql -uroot -pmysql
mysql: [Warning] Using a password on the command
line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

終端輸入mysql -uroot -h 127.0.0.1 -p

[email protected]:~$ mysql -uroot -h 127.0.0.1 -p 
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

顯然,也沒有用。
進入,

cd /etc/mysql/mysql.conf.d

[email protected]:/etc/mysql/mysql.conf.d$ ls
mysqld.cnf  mysqld_safe_syslog.cnf
[email protected]:/etc/mysql/mysql.conf.d$ vim mysqld.cnf 

進入mysql.cnf檔案,如下:
在這裡插入圖片描述

然後儲存退出,並將這個目錄許可權修改一下:

/etc/mysql/mysql.conf.d$ sudo chmod 777 mysqld.cnf 

然後就啟動mysql 和mysqld 服務:

[email protected]:/etc/mysql/mysql.conf.d$ service mysql  restart
[email protected]:/etc/mysql/mysql.conf.d$ service mysqld restart

然而,mysqld啟動又出錯了,啟動失敗,憤怒啊!!!不用管它!

[email protected]:~$ service mysqld restart
Failed to restart mysqld.service: Unit mysqld.service not found.

進入目錄並檢視:

[email protected]:/var/run$ cd mysqld/
[email protected]:/var/run/mysqld$ ls
mysqld.sock  mysqld.sock.lock

然後使用當前的 mysqld.sock 測試一下:

[email protected]:/var/run/mysqld$ mysql -uroot -p -S /var/run/mysqld/mysqld.sock

此時,終於見到光明瞭。可以正常進入mysql資料庫。

[email protected]:/var/run/mysqld$ mysql -uroot -p -S /var/run/mysqld/mysqld.sock 
mysql: [Warning] World-writable config file '/etc/mysql/mysql.conf.d/mysqld.cnf' is ignored.
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

到此還不算完成!
建立軟連結:

[email protected]:~$ ln -s /var/run/mysqld/mysqld.sock /var/lib/mysql/mysql.sock

進入如下目錄:

[email protected]:~$ cd /var//run/mysqld/
[email protected]:/var/run/mysqld$ ls
mysqld.sock  mysqld.sock.lock

然後再將原來修改的 mysql.cnf 檔案改回來,
在這裡插入圖片描述
至此,算是完成了。
重新進入終端測試一下:
在這裡插入圖片描述
此時,成功搞定!今日什麼也沒有做,花了近一天的時間,才完成這個大bug的!!!