默认情况下远程访问会出现 Can’t connect to MySQL server on ‘192.168.10.18′ (10061) 错误
是因为,mysql的默认配置为了增强安全性,禁止了非本机的访问,在ubuntu中是这样,debian中也是。禁止的方式以前是在my.cnf中有一句
是因为,mysql的默认配置为了增强安全性,禁止了非本机的访问,在ubuntu中是这样,debian中也是。禁止的方式以前是在my.cnf中有一句
skip-network
现在则变成了:
# Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure.
现在则变成了:
# Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1 是只监听本机地址。所以,如果要开放局域网内的访问,需要注释掉这一句,或者改成: bind-address = 0.0.0.0
1)修改 bind-address = 127.0.0.1 为 bind-address = 0.0.0.0
# vim /etc/mysql/my.cnf2)修改完成后重启mysql服务
# sudo /etc/init.d/mysql restart这样改完3306端口就开始监听了,但是mysql的权限还没有打开root远程登陆。mysql默认是不允许远程连接的,因为有很大的安全隐患。
需要手动增加可以远程访问数据库的用户。
3)本地登入mysql,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,将"localhost"改为"%"
#mysql -uroot -ppassword
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;
注:红色部分改为自己的用户名密码
修改完这些就可以远程访问了。
MySQL> update user set host='%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
然后查看了下数据库的host信息如下:
MySQL> select host from user where user = 'root';
+-----------------------+
| host |
+-----------------------+
| % |
| 127.0.0.1 |
| localhost.localdomain |
+-----------------------+
3 rows in set (0.00 sec)
host已经有了%这个值,所以直接运行命令:
复制代码 代码如下:
MySQL>flush privileges;
再用MySQL administrator连接...成功!!
修改完这些就可以远程访问了。
补充:
今天按照这个方法遇到了点问题补充一下MySQL> update user set host='%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
然后查看了下数据库的host信息如下:
MySQL> select host from user where user = 'root';
+-----------------------+
| host |
+-----------------------+
| % |
| 127.0.0.1 |
| localhost.localdomain |
+-----------------------+
3 rows in set (0.00 sec)
host已经有了%这个值,所以直接运行命令:
复制代码 代码如下:
MySQL>flush privileges;
再用MySQL administrator连接...成功!!
来源:https://www.cnblogs.com/haxianhe/p/9271089.html