[轉載]ac mysql 沒法遠程鏈接

Mac mysql 沒法遠程鏈接

版權聲明:本文爲博主原創文章,遵循 CC 4.0 by-sa 版權協議,轉載請附上原文出處連接和本聲明。
本文連接: http://www.javashuo.com/article/p-rlepbcyh-ch.html

現象:在 Mac 系統上,mysql 不容許遠程鏈接。mysql

首先按照常規的方法操做: 
進入 mysql: $ mysql -u root -psql

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

再次嘗試鏈接,仍是不行。markdown

後來發現,即便在本地使用 IP 也沒法鏈接。那估計就是 mysql 服務綁定的 IP 有問題,要找到 mysql 的配置文件看看。app

當時用 Homebrew 安裝的 mysql,查看 brew info mysql ,沒有找到 mysql 配置文件的位置,卻有這樣一句話:oop

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

查看 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
  • 1
  • 2

最終發現,使用 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
  • 1
  • 2
  • 3
  • 4

顧名思義,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

再次嘗試遠程鏈接,終於通了。

相關文章
相關標籤/搜索