MySQL: Can’t connect to MySQL server on (111 “Connection refused”)
1. Mysql连接问题
远程访问mysql或者通过docker访问宿主机mysql经常会碰到下面的问题:
Can’t connect to MySQL server on (111 “Connection refused”)
解决
找到自己MySQL数据库配置文件的位置,编辑
/etc/mysql/mysql.conf.d# vi mysqld.cnf
将 bind_address 127.0.0.1
注释掉
2. 开放远程连接后,会出现第二个问题:
"Host 'x.x.x.x' is not allowed to connect to this MySQL server"
解决办法
root 进入数据库
$ mysql -uroot -p
执行下方两行命令:
grant all privileges on *.* to 'root'@'%' identified by '12345678' with grant option;
开放所有权限给root,当root以12345678密码从任意IP登入的时候,允许其操作所有数据库下的所有表
flush privileges;
刷新,使上述命令生效
3. 重启数据库
$ /etc/init.d/mysql stop
$ /etc/init.d/mysql start
再从docker或者远程主机访问数据库的时候就可以了
$ telnet 172.17.0.1 3306
Trying 172.17.0.1...
Connected to 172.17.0.1.
Escape character is '^]'.
注意: grant all privileges on *.* to 'root'@'%' identified by '12345678' with grant option;
给root用户(mysql用户里面的root,不是主机)
所有数据库的最高权限,可能会有安全隐患。可以根据具体需求限制对具体的数据库,甚至是表格进行授权