linux上新装了mysql5.5,并且已经给远程机器受过可访问的权限,但远程连接mysql的时候报错:
ERROR 2003 (HY000): Can't connect to MySQL server
检查后发现是linux防火墙的3306端口没有开放,linux上打开3306端口后,可以在远程访问mysql。
windows上登录远程的mysql,报错:Can't connect to MySQL server
D:\>mysql -u root -pmysql -h 192.168.0.198
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.198' (10065)
telnet 服务器上的3306端口一样失败
D:\>telnet 192.168.0.198 3306
Connecting To 192.168.0.198...Could not open connection to the host, on port 3306: Connect failed
登录mysql服务器主机,确认mysql使用的是3306端口。
[mysql@DBMYSQL ~]$ netstat -ant|grep 33
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
mysql> show variables like'%port%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_support_xa | ON |
| large_files_support | ON |
| port | 3306 |
| report_host | |
| report_password | |
| report_port | 3306 |
| report_user | |
+---------------------+-------+
7 rows in set (0.00 sec)
检查linux防火墙的情况,发现3306端口没有开放。
[root@DBMYSQL /]# /sbin/service iptables status
Chain RH-Firewall-1-INPUT (2 references)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
3 ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
6 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:631
8 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
10 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
在/etc/sysconfig/iptables中开放3306端口
[root@DBMYSQL /]# vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
重启iptables服务
[root@DBMYSQL /]# /sbin/service iptables restart
输入正确的密码,可以登录mysql
D:\>mysql -u root -pmysql -h 192.198.0.198
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.20-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
如果输错密码,报的错误是Access denied
D:\>mysql -u root -ptest -h 192.168.0.198
ERROR 1045 (28000): Access denied for user 'root'@'192.168.0.10' (using password: YES)