1、首先要進入Linux服務器,查看3306端口是否開啟。
netstat -an|grep 3306
如果顯示如下,3306為紅色,則表示,端口未開放。

2、修改訪問權限
需要在mysql.conf文件中添加
port = 3306然后保存。具體怎么打開使用vim。
3、登錄數據庫
默認mysql的用戶是沒有遠程訪問的權限的,因此當程序跟數據庫不在同一台服務器上時,我們需要開啟mysql的遠程訪問權限。
主流的有兩種方法,改表法和授權法。
相對而言,改表法比較容易一點,因此,這里只貼出改表法
[root@csxt bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.27 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set host = '%' where user = 'root'; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select host, user from user; +-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.06 sec) mysql>
4、防火牆開放3306端口
打開防火牆配置文件
vi /etc/sysconfig/iptables
增加下面一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
重啟防火牆
service iptables restart
- 注意:增加的開放3306端口的語句一定要在icmp-host-prohibited之前
個人配置:
# 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 -i eth0 -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 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT -A FORWARD -p icmp -j ACCEPT -A FORWARD -i lo -j ACCEPT -A FORWARD -i eth0 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
