[连接远程主机] : ERROR 1045 (28000): Access denied for user 'user'@'host' (using password: YES)
[复制中的IO] : [ERROR] Slave I/O: error connecting to master 'user@IP Address:3306' - retry-time: xx retries: x, Error_code: 2003
产生的原因无外乎以下几种:
目标主机的防火墙开启
目标主机中不允许该用户连接(权限表中没有改用户)
用户名的密码不对
剩下的还没想好
测试环境 两台主机
发起主机:dbm01 192.168.146.101
目标主机:dbm02 192.168.146.102
在目标主机上建立两个测试用户
test01 指定了IP地址来源
test02 指定了主机名
mysql> CREATE USER 'test01'@'192.168.146.%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'test01'@'192.168.146.%'; Query OK, 0 rows affected (0.00 sec) mysql> CREATE USER 'test02'@'dbm01' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'test02'@'dbm01'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
dbm02>select host,user from mysql.user order by user; +---------------+--------+ | host | user | +---------------+--------+ | dbm01 | | | localhost | | | dbm02 | replmm | | % | root | | localhost | root | | ::1 | root | | 127.0.0.1 | root | | dbm01 | root | | 192.168.146.% | test01 | | dbm01 | test02 | +---------------+--------+ 10 rows in set (0.00 sec)
要想解决问题要先弄清权限表原理
当建立一个类似以下的一个远程连接时
mysql -u test01 -p123456 -h dbm02
发起连接的主机会先在本地主机/etc/hosts文件中查找目标主机对应的IP地址
[root@dbm01 ~]# vi /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.146.101 dbm01 192.168.146.109 dbs09 <strong>192.168.146.102 dbm02</strong>
如果找不到主机别名会报如下类似的错误:未知的主机名
ERROR 2005 (HY000): Unknown MySQL server host 'dbm02' (0)
找到以后会转换成相应的IP地址,直接写IP地址的话省略此步骤
所以 排错的话 先应该ping目标主机IP 确保能与目标主机联通 这是远程连接基础。
再找了目标主机地址之后。再去访问目标主机mysql中的权限表 检查用户的有效性
如果用user:test01去连dbm02的时候 应该确保dbm02目标主机中的权限表中有该用户
<pre name="code" class="plain">dbm02>select host,user from mysql.user order by user; +---------------+--------+ | host | user | +---------------+--------+ | dbm01 | | | localhost | | | dbm02 | replmm | | % | root | | localhost | root | | ::1 | root | | 127.0.0.1 | root | | dbm01 | root | | 192.168.146.% | test01 | | dbm01 | test02 | +---------------+--------+ 10 rows in set (0.00 sec)
使用别名连接时 应该保证skip_name_resolve参数关闭,否则不能解析
dbm01>show variables like 'skip_name_resolve'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | skip_name_resolve | OFF | +-------------------+-------+ 1 row in set (0.00 sec)
第一次连接时 skip_name_resolve关闭
<span style="font-family: Arial, Helvetica, sans-serif;">[root@dbm01 ~]# mysql -utest01 -p123456 -h dbm02</span>
Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'test01'@'dbm01' (using password: YES) [root@dbm01 ~]# mysql -utest02 -p123456 -h dbm02 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g.test01没有连接上
test02连接成功
将test01的IP地址解析成了主机名所以 test01没有连接上
*不过建议开启该参数,因为开后禁止内网IP做反向解析,只有等超时失败后,mysql才会响应客户端。会有一个较为长时间的解析过程,严重影响效率。
因为该参数是静态变量所以只能修改配置参数
vi /etc/my.cnf追加
skip_name_resolve
再重启mysql服务
[root@dbm01 ~]# service mysql restart Shutting down MySQL............ SUCCESS! Starting MySQL.. SUCCESS!使用root用户登录后查看状态 已经开启了
mysql> show variables like 'skip_name_resolve'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | skip_name_resolve | ON | +-------------------+-------+ 1 row in set (0.00 sec)
第二次连接时 skip_name_resolve开启
[root@dbm01 ~]# mysql -utest01 -p123456 -h dbm02 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g.
[root@dbm01 ~]# mysql -utest02 -p123456 -h dbm02 Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'test02'@'192.168.146.101' (using password: YES)
test01连接成功
test02没有连接上
虽然test02用户不能登录了 但host为IP地址的用户test01可以连接。
总结:
要想进行远程连接,需要在远程连接的参数文件中添加skip_name_resolve开启该功能。并且在目标主机的权限表中HOST是连接主机的地址。