Mac mysql 无法远程连接

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

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

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

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

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

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

MySQL is configured to only allow connections from localhost by default

查看 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

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

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1

顾名思义,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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值