設置 Linux 服務器中 MySQL 允許遠程訪問
開啟 MySQL 遠程訪問權限: 在linux系統上登陸mysql服務。
-- root 是用戶名 [[email protected] ~]# mysql -u root -p Enter password: -- 輸入密碼
創建遠程連接 MySQL 的用戶:
-- 創建用戶、密碼及權限範圍 第一個 roo t為用戶名 @後為適用的主機,‘%’表示所有電腦都可以訪問連接,第二個 root 為密碼 mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘192.168.0.2‘ IDENTIFIED BY‘root‘ WITH GRANT OPTION; Query OK, 0 rows affected (1.57 sec) -- 立即生效 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
查看數據庫用戶:
-- 使用 mysql 庫 mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature toget a quicker startup with -A Database changed -- 查看用戶 mysql> SELECT DISTINCT CONCAT(‘User: [‘, user, ‘‘‘@‘‘‘, host, ‘];‘) AS USER_HOST FROM user; +---------------------------------------+ | USER_HOST | +---------------------------------------+ | User: [[email protected]
創建成功。
查看端口:
mysql> show global variables like ‘port‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.01 sec)
開啟3306端口:
[[email protected] ~]# vim /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT "/etc/sysconfig/iptables" 14L, 543C
註意:一定要加在後兩行的前面。
重啟防火墻:
[[email protected] ~]# service iptables restart iptables:將鏈設置為政策 ACCEPT:filter [確定] iptables:清除防火墻規則: [確定] iptables:正在卸載模塊: [確定] iptables:應用防火墻規則: [確定]
查看服務器ip地址:
[[email protected] ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:16:F4:E6 inet addr:192.168.0.123 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: hjjj:jjji::iii:oooo:oooo:ioio/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2931 errors:0 dropped:0 overruns:0 frame:0 TX packets:1631 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:322681 (315.1 KiB) TX bytes:266043 (259.8 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
連接測試:
設置 Linux 服務器中 MySQL 允許遠程訪問