1. 程式人生 > >無法連線MySQL資料庫錯誤總結

無法連線MySQL資料庫錯誤總結

1、可能密碼不對

[[email protected] mysql3311]# mysql -uroot -P3311 -h192.168.60.162 -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'slave162.netposa' (using password: YES)
錯誤原因:可能是使用者名稱或密碼不對,或者許可權不夠

解決方法:檢查使用者名稱和密碼以及許可權,然後再連線

2、本地連線,密碼許可權都對,但是無法連線

[[email protected] mysql3311]# mysql -uroot -P3311  -p123456
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 '/tmp/mysql.sock' (111)
錯誤原因:資料庫本地連線預設使用的是/tmp/mysql.sock去連線資料庫的,如果資料庫的sock檔案不是/tmp/mysql.sock,客戶端找不到這個檔案就報錯

解決方法:找到資料庫用的sock檔案,然後使用-S引數讓客戶端使用這個檔案,或者是在my.cnf中的[client]下面新增socket配置使其和實際的一樣

[[email protected] mysql3311]# ps -ef|grep mysqld
root     21096     1  0 11:10 pts/1    00:00:00 /bin/sh /usr/local/mysql3311/bin/mysqld_safe --datadir=/usr/local/mysql3311/data --pid-file=/usr/local/mysql3311/data/slave162.netposa.pid
mysql    21353 21096  0 11:10 pts/1    00:00:00 /usr/local/mysql3311/bin/mysqld --basedir=/usr/local/mysql3311 --datadir=/usr/local/mysql3311/data --plugin-dir=/usr/local/mysql3311/lib/plugin --user=mysql --log-error=/usr/local/mysql3311/data/slave162.netposa.err --pid-file=/usr/local/mysql3311/data/slave162.netposa.pid --socket=/tmp/mysql3311.sock --port=3311
root     21658 16517  0 11:25 pts/1    00:00:00 grep mysqld
[
[email protected]
mysql3311]# [[email protected] mysql3311]# mysql -uroot -p123456 -S /tmp/mysql3311.sock mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.14-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> mysql>

或者修改 /etc/my.cnf,在[client]下新增socket=/tmp/mysql3311.sock
[[email protected] mysql3311]# vim /etc/my.cnf
[[email protected] mysql3311]# 
[[email protected] mysql3311]# 
[[email protected] mysql3311]# 
[[email protected] mysql3311]# mysql -uroot -P3311  -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.14-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 
mysql> 

3、防火牆可能導致下面錯誤

[[email protected] ~]#  mysql -uroot -P3311 -h192.168.60.162 -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.60.162' (113)

錯誤原因:遠端連線的時候,可能會因為防火牆,導致無法連線資料庫。

解決方法:要麼配置防火牆,要麼關閉防火牆。