MySQL配置遠程訪問(bind-address)的一大誤區
看到網上很多人說了bind-address=192.168.1.100,並且關閉skip-networking。可以實現在192.168.1.100主機上對mysql的遠程訪問。我想說,我試了,This is bullshit!
一、我本機操作,嘗試了多次都是無法啟動MySQL,報錯如下:
[root@localhost ~]# service mysql restart
ERROR! MySQL server PID file could not be found!
Starting MySQL... ERROR! The server quit without updating PID file (/tmp/mysqld/mysqld.pid).
[root@localhost ~]#
如果說是得用設置的那個主機遠程連接重啟的話,那好我也嘗試了,依舊報錯如上。
首先bind-address的官方解釋如下:
--bind-address=ip_address
#Use specified network interface to connect to MySQL Server(在具有多個網絡接口的計算機上,使用此選項選擇用於連接 MySQL 服務器的接口。)
#注解:假如一個服務器上配置了多個網卡,即存在配置多個ip的情況下,可以用bind-address選擇監聽某個接口(ip)。只有通過被監聽的接口,才能訪問mysql。
總結一下,bind-address的設置有以下三種情況:
- bind-address=127.0.0.1 #只允許本機訪問。
- bind-address=某個網卡的ip #例如bind-address=192.168.1.101,只能通過ip為192.168.1.101的網卡訪問。
- bind-address=0.0.0.0 #此規則是系統默認配置,監聽所有網卡,即允許所有ip訪問。
二、僅允許本機訪問數據庫
如果想設置為本機訪問數據庫,那么/etc/my.cnf里可以這么設置:
bind-address=127.0.0.1
skip-networking=1
skip-networking的默認值是0(關閉狀態)。如果想查看skip-networking的值,可以通過mysql>show variables like 'skip_networking';
來查看,前提是登錄連接上了mysql。
三、遠程訪問數據庫
1、前提得在防火牆開啟對應端口開放
firewall設置命令如下:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#開放端口
firewall-cmd --reload
#重啟防火牆生效
iptables設置命令如下:
[root@v01-svn-test-server online]# iptables -A INPUT -p tcp -s 192.168.1.100 --dport 3306 -j ACCEPT
[root@v01-svn-test-server online]# service iptables status
2、關閉skip_networking
編輯/etc/my.cnf:
skip-networking=0
3、設置user表中對應賬戶的host值
mysql>grant all privileges on *.* to root@'192.168.1.100' identified by '123456';
mysql>flush privileges;
#說明:@'192.168.1.100'只是允許這台主機進行訪問,如果設置為'%'則是所有主機可以登錄訪問,設置為'localhost'則是只允許本機訪問。
附加說明:在mysql的配置文件my.cnf中,'-'有時候同'_'均可以生效。例如:“skip-networking”,在數據表中是“skip_netwroking”,而在my.cnf中使用減號'-'和下划線'_'都生效。