1、可能密码不对

[root@slave162 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'@'slave162posa' (using password: YES)
错误原因:可能是用户名或密码不对,或者权限不够

解决方法:检查用户名和密码以及权限,然后再连接

2、本地连接,密码权限都对,但是无法连接

[root@slave162 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参数让客户端使用这个文件,或者是在myf中的[client]下面添加socket配置使其和实际的一样

[root@slave162 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/slave162posa.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/slave162posa.err --pid-file=/usr/local/mysql3311/data/slave162posa.pid --socket=/tmp/mysql3311.sock --port=3311
root     21658 16517  0 11:25 pts/1    00:00:00 grep mysqld
[root@slave162 mysql3311]# 
[root@slave162 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/myf,在[client]下添加socket=/tmp/mysql3311.sock

[root@slave162 mysql3311]# vim /etc/myf
[root@slave162 mysql3311]# 
[root@slave162 mysql3311]# 
[root@slave162 mysql3311]# 
[root@slave162 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、防火墙可能导致下面错误

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

错误原因:远程连接的时候,可能会因为防火墙,导致无法连接数据库。

解决方法:要么配置防火墙,要么关闭防火墙。













更多推荐

无法连接MySQL数据库错误总结