现象:在 Mac 系统上,mysql 不容许远程链接。mysql
首先按照常规的方法操做:
进入 mysql: $ mysql -u root -p
sql
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION; mysql> FLUSH PRIVILEGES;
再次尝试链接,仍是不行。markdown
后来发现,即便在本地使用 IP 也没法链接。那估计就是 mysql 服务绑定的 IP 有问题,要找到 mysql 的配置文件看看。app
当时用 Homebrew 安装的 mysql,查看 brew info mysql
,没有找到 mysql 配置文件的位置,却有这样一句话:oop
MySQL is configured to only allow connections from localhost by default
查看 msyql --help
,mysql 提示会按照下面的顺序查找配置文件。ui
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
,内容是:spa
# Default Homebrew MySQL server config [mysqld] # Only allow connections from localhost bind-address = 127.0.0.1
顾名思义,bind-addres
的配置绑定了本地IP,因此远程没法链接。再看看 mysql 官方文档, bind-address
的配置是这样描述的:.net
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.rest
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.code
因而修改配置为: bind-address = 0.0.0.0
重启 mysql:brew services restart mysql
再次尝试远程链接,终于通了。