[转载]ac mysql 无法远程连接

Mac mysql 无法远程连接

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/feixiang2039/article/details/81164136

现象:在 Mac 系统上,mysql 不允许远程连接。

首先按照常规的方法操作: 
进入 mysql: $ mysql -u root -p

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
  • 1
  • 2

再次尝试连接,还是不行。

后来发现,即使在本地使用 IP 也无法连接。那估计就是 mysql 服务绑定的 IP 有问题,要找到 mysql 的配置文件看看。

当时用 Homebrew 安装的 mysql,查看 brew info mysql ,没有找到 mysql 配置文件的位置,却有这样一句话:

MySQL is configured to only allow connections from localhost by default
  • 1

查看 msyql --help ,mysql 提示会按照下面的顺序查找配置文件。

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
  • 1
  • 2

最终发现,使用 Homebrew 安装 mysql,默认配置在 /usr/local/etc/my.cnf,内容是:

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
  • 1
  • 2
  • 3
  • 4

顾名思义,bind-addres的配置绑定了本地IP,所以远程无法连接。再看看 mysql 官方文档, bind-address 的配置是这样描述的:

If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.

If the server was started with —bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections.

于是修改配置为: bind-address = 0.0.0.0 
重启 mysql:brew services restart mysql

再次尝试远程连接,终于通了。

posted on 2019-08-14 11:04  yf清凉  阅读(223)  评论(0编辑  收藏  举报