MySQL 远程连接配置和排
这几天都要和MySQL数据库打交道,而且还是尽量的使用Python MySQLdb来连接管理。yeah...
之前一直都在CentOS下,对MySQL也没深入一直都没出现过数据库不能远程连接的问题。后来转到Ubuntu14.04 之后,还真是有问题了。
先看看CentOS 6.6 下yum安装的MySQL版本:
[root@s1 ~]# mysql --version mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
Ubuntu 14.04 apt-get 安装的MySQL版本:
gea@ub1:~$ mysql --version mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (x86_64) using readline 6.3
一、在CentOS 6.6中对MySQL的test1用户赋予所有权限:并且从远程登录:
mysql> grant all privileges on *.* to test1@'%' identified by '123456' with grant option; Query OK, 0 rows affected (0.00 sec)
从远程登录到本机(192.168.10.10)
gea@ub1:~$ mysql -h 192.168.10.10 -u test1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g.
可以看到,登录是没有问题的。
二、在Ubuntu 14.04中对MySQL的test1用户赋予所有权限:并且从远程登录:
mysql> grant all privileges on *.* to test1@'%' identified by '123456' with grant option; Query OK, 0 rows affected (0.00 sec)
从远程登录到本机(192.168.10.15) [root@s1 ~]# mysql -h 192.168.10.15 -u test1 -p Enter password: ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.15' (111)
结果却出现了不能连接的结果。
排除错误:
1)首先从localhost中确定test1能在本机登录:
gea@ub1:~$ mysql -u test1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 36 可以登录,没有问题
2)确认3306端口是否已经打开,并且为远程开放:
1. gea@ub1:~$ netstat -ntlp (No info could be read for "-p": geteuid()=1000 but you should be root.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN - tcp6 0 0 :::80 :::* LISTEN - tcp6 0 0 :::22 :::* LISTEN - 2. 从远程主机telnet 3306端口 C:\Users\Administrator>telnet 192.168.10.15 3306 正在连接192.168.10.15...无法打开到主机的连接。 在端口 3306: 连接失败
OK。找到原因了。远程端口未开放。
只需要修改MySQL的配置文件,设置3306端口开放远程连接。
gea@ub1:~$ sudo vim /etc/mysql/my.cnf 找到: bind-address = 127.0.0.1 把这句注释掉。 重启MySQL服务。。。