连接MySQL数据库时常见故障问题的分析与解决
初学的mysql网友好象经常会碰到mysql无法连接的错误。特开贴收集这样问题的现象和原因。
先自己扔块砖头出来。
归纳如下:
故障现象 : 无法连接 mysql
=============================================================================
C:\Documents and Settings\Administrator>mysql -uroot -h 192.168.8.88 -proot //带-p参数,并指明密码
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
C:\Documents and Settings\Administrator>mysql -uroot -h 192.168.8.88 -p //带-p参数,在下一步进行密码输入
Enter password: //有字符串输入
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
C:\Documents and Settings\Administrator>mysql -uroot -h 192.168.8.88 //不带-p参数
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: NO)
C:\Documents and Settings\Administrator>mysql -uroot -h 192.168.8.88 -p //带-p参数,在下一步进行密码输入
Enter password: //无字符串输入
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: NO)
上面的对比可总结出一点,使用mysql命令进行登录时,若有密码输入行为并输入了字符,则验证出错后,则返回的错误提示中,对于 (using password: ?)中?的关键字,则返回的是YES,若没有密码输入行为,或无密码字符输入,则返回的是NO。
除上面的实验对比,还进行了如下的登录对比操作,并记录了他们所返回错误提示类型,对上面的总结进行验证:
1.使用存在的用户,不输入密码
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: NO)
2.使用不存在的用户,不输入密码
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: NO)
3.使用存在的用户,且输入密码正确
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
4.使用存在的用户,但输入密码不正确
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
5.使用不存在的用户,但输入数据库中存在的某一个密码
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
6.使用不存在的用户,且输入数据库中不存在的一个密码
ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' (using password: YES)
总结:对于 ERROR 1045 (28000): Access denied for user 'root'@'192.168.8.123' 此类错误返回时, (using password: ?)中?的
关键字是YES还是NO,关键不在于用户是否存在,密码是否正确,它的结果取决于登录时,用户对于密码有没有字符串的输入,如果没有,MySQL数据库验证后,若出错返回此类信息,则应是 (using password: NO),若用户对密码有字符串的输入,返回的则是
(using password: YES)。
下面分析这类 ERROR 1045 (28000): Access denied for user'usera'@'localhost' 错误出现的原因:
原因1 : 客户端远程访问的用户账号并未创建
检查 :
以管理员ROOT登录后,show grants for 'user'@’IP‘; 或者 select user from mysql.user; 确认用户账号是否存在。
ERROR 1141 (42000): There is no such grant defined for user 'jtsec' on host '192.168.8.123'
mysql>
返回信息:ERROR 1141 (42000): There is no such grant defined for user 'jtsec' on host '192.168.8.123'
+-------+---------------+
| user | host |
+-------+---------------+
| root | localhost |
+-------+---------------+
1 rows in set (0.00 sec)
mysql>
关于user记录数只有一条,是root,并没有jtsec相关的记录,说明没有数据库中没有jtsec这个帐号。
处理 :创建用户账号。
mysql> grant all privileges on *.* to 'jtsec'@'192.168.8.123' identified by 'jtsec' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'jtsec'@'192.168.8.123';
+---------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for jtsec@192.168.8.123 |
+---------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jtsec'@'192.168.8.123' IDENTIFIED BY PASSWORD '*0B4AB716B6BE11F89101577836F3016D8EEAA217' WITH GRANT OPTION |
+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select user,host from mysql.user;
+-------+---------------+
| user | host |
+-------+---------------+
| jtsec | 192.168.8.157 |
| root | localhost |
+-------+---------------+
2 rows in set (0.00 sec)
mysql>
ERROR 1141 (42000): There is no such grant defined for user 'root' on host '192.168.8.123'
mysql>
返回信息:ERROR 1141 (42000): There is no such grant defined for user 'root' on host '192.168.8.123'
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------+---------------+
| user | host |
+-------+---------------+
| root | localhost |
+-------+---------------+
1 rows in set (0.00 sec)
mysql>
关于user值为root的记录数只有一条,且host值为localhost,说明root用户只能在本地访问数据库。
处理 :进行root用户的远程访问授权,可以授权到指定的客户端IP,也可以授权为所有IP都可访问(host值为%)。
授权为所有IP都使用用户root,密码root,来远程访问数据库
mysql> GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
再次进行授权的查询
+--------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@% |
+--------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------+---------------+-------------------------------------------+
| user | host | password |
+-------+---------------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | % | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------+---------------+-------------------------------------------+
2 rows in set (0.00 sec)
+-------+---------------+-------------------------------------------+
| user | host | password |
+-------+---------------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | % | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| jtsec | 192.168.8.123 | *0B4AB716B6BE11F89101577836F3016D8EEAA217 |
+-------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
根据查询结果可以看出来,root账户的本地访问和远程访问密码一样。
处理 :使用正确的访问密码进行访问即可。
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
或尝试关掉防火墙
chkconfig ip6tables off
chkconfig iptables off
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.8.88' (10065)
错误信息 :ERROR 2003 (HY000): Can't connect to MySQL server on 'hostxxxxx' (10061)
原因 : mysqld数据库服务没有启动。
检查 :在windows 的任务管理器,或者 unix/linux 下 ps -aux | grep mysql 看一下。确认服务已经启动。
处理 :启动mysqld 服务
===================================================================================
错误信息 :ERROR 1130: Host xx.xx.xx.xx is not allowed to connect to this MySQL server
原因 : mysql服务器没有赋予此客户端远程连接的权限。
检查 :在mysql服务器本地查询mysql库里user表对应的host是否包含客户端机器的IP(%为不限制IP允许远程连接)。
处理 :修改mysql库下的user表:update user set host = '%' where user ='XXX';flush privileges;
===================================================================================
错误信息 :ERROR 1045 (28000): Access denied for user 'usera'@'localhost' (using password:NO)
原因 : 用户账号并未创建,远程登录时登录指令未直接包含密码项
处理 :创建用户账号。
===================================================================================
错误信息 :ERROR 2003 (HY000): Can't connect to MySQL server on 'hostxxxxx' (10061)
原因 : mysqld 客户端与服务端端口不一致。
检查 :在my.ini 设置下服务端口 这种情况特别是客户都与远程服务器端口不一致 很容易出现的问题
处理 :启动mysqld 服务
===================================================================================
错误信息 :ERROR 2002 (HY000): Can't connect to local MySQL server server through socket '/var/lib/mysql/mysql.sock'(111)
原因 : mysqld的mysql.sock没在相应的位置。
处理 :
2.改权限 chown -R mysql:msyql /var/lib/mysql
3.修改/etc/my.cnf (注意:先停数据库)
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[client]
socker=/var/lib/mysql/mysql.sock
4.启动数据库
===================================================================================
常用检查步骤。
1. PING hostname 或 PING 189.xx.xx.xx 确认服务器IP层通信没有问题。如果PING通则继续(2),PING不通则找网络方面专家协助。
2. TELNET hostname 3306 确认服务器TCP层通信没有问题。(你的端口号可能不是3306),如通则继续,如不通,请检查mysqld 是否已经在运行,防火墙屏蔽了端口。
3. 检查用户权限, show grants ... 或 select user,host,password from mysql.user;
故障现象 : 本地无法 mysql -u root -p 用原来的密码登录,但可以用无密码,远程必须用原密码登录
错误信息 :ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
原因 : 未知,不知道是否mysql的新密码控制策略
检查 :mysql -u root 密码空登录无问,远程用navica mysql无密码有问题,有密码无问题
处理 :暂时先把本地登录的密码去掉
版本环境:mysql Ver 14.14 Distrib 5.1.56, for debian-linux-gnu (i486) using readline 6.2
+-------+---------------+-------------------------------------------+
| user | host | password |
+-------+---------------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
2 rows in set (0.00 sec)
+-------+---------------+-------------------------------------------+
| user | host | password |
+-------+---------------+-------------------------------------------+
| root | localhost | |
| root | % | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
2 rows in set (0.00 sec)
==================================================================================
故障现象 : 无法连接 mysql windows
错误信息 :ERROR 2003 (HY000): Can't connect to MySQL server on 'hostxxxxx' (10061)
原因 : 同时开启大量的tcp端口和而仅有一小部分短命端口时,当他们处于time_wait时期,mysql可能会跑错端口
检查 :在windows 默认会开启5000个临时端口供调用,而他们的生命仅仅是120s,意思是在关闭端口的时候会有120秒的延时
处理 :减少不不必要的tcp端口。
修改注册表
==================================================================================
求助,不知道大家有一样的情况没有?
按照网上说的方法仍没解决,楼主是否碰见过?
==================================================================================
Lost connection to MySQL server at 'reading initial communication packet', system error: 0
什么问题??
Author: lioncode
CMP: JTSEC-RD
Date: 2012-08-28 18:14
Project: jtsec_one-way_dbsync
Version: v1.0.0
转载 http://blog.csdn.net/lioncode/article/details/7917310