1. 程式人生 > >MySQL遠程連接失敗(錯誤碼:2003)

MySQL遠程連接失敗(錯誤碼:2003)

ofo 權限 sele padding ng- order key nac rtu

註:本文來源於《 MySQL遠程連接失敗(錯誤碼:2003)》


  • (1) Xshell遠程登陸服務器,用“ps aux|grep mysql”命令查看,MySQL服務已啟動:

  • (2) 用"vim my.cnf"命令查看my.cnf文件(在MySQL安裝目錄下,我的位置是/usr/local/mysql/my.cnf),修改其對應的值並重啟MySQL。對應內容如下:
[mysqld]
bind-address = 0.0.0.0  # 表示允許任何主機登陸MySQL
port=3306               # 表示MySQL運行端口為3306
  • (3)用“mysql -u admin -p”命令,回車後輸入密碼“adminpsw”能正常登陸服務器MySQL
mysql> show global variables like ‘port‘;  # 查看MySQL運行的實際端口
+---------------+-------
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)
  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> select
host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | admin | | % | root | | localhost | mysql.session | | localhost | mysql.sys | +-----------+---------------+ 5 rows in set (0.00 sec)
  # 如果上述查詢結果,admin用戶對應的host不為%,則修改用戶權限
  # 此處需註意的是,修改權限時要帶上密碼(IDENTIFIED BY ‘adminpwd‘),雖然不知道具體原理,但是沒加密碼之前客戶端還是不能遠程訪問MySQL。   
  mysql> GRANT ALL PRIVILEGES ON *.* TO ‘admin‘@‘%‘ IDENTIFIED BY ‘adminpwd‘ WITH GRANT OPTION; 
  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> FLUSH PRIVILEGES;
  Query OK, 0 rows affected (0.00 sec)
  • (4) 至此,我的本地Navicat客戶端已經能都正常訪問服務器端,但是運行上述python連接MySQL代碼還是報一樣的錯誤。

  • (5)查網上資料說可能是防火墻屏蔽了3306端口,本地cmd"ping 192.168.1.166"能夠Ping通,再用”telnet 192.168.1.166 3306“命令檢查端口是否被屏蔽,結果為"正在連接192.168.1.166...無法打開到主機的連接。 在端口 3306: 連接失敗",說明是防火墻的問題
    (如果Win10 telnet不是內部或外部命令,決解方法參考連接:https://jingyan.baidu.com/article/1e5468f9033a71484961b7d7.html)

  • (6) 起初我以為是指我本地防火墻的問題,於是把本地防火墻關了,結果問題並沒有解決。
  • (7) 其實應該是遠程服務器的防火墻問題。遠程登陸服務器(我用root用戶登錄的),檢查防火墻狀態
systemctl start firewalld # 開啟防火墻
systemctl stop firewalld  # 關閉防火墻
systemctl status firewalld  #檢查防火墻狀態
  • (8) 關閉遠端服務器防火墻後,運行本地運行本地python連接MySQL代碼,MySQL連接成功
[root@db sysconfig]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 三 2018-02-28 17:18:10 CST; 7s ago
     Docs: man:firewalld(1)
 Main PID: 5452 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─5452 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
[root@db sysconfig]# systemctl stop firewalld
[root@db sysconfig]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
  • (9)如果你覺得關閉防火墻不安全,可打開遠端服務器的iptables(安裝或升級命令“yum install iptables”)(我用root用戶登錄的),並用“vi /etc/sysconfig/iptables”檢查3306端口是否打開,如沒有,在文件中加入“-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT”(如下所示),保存文件並用“service iptables restart”命令重啟iptables
# Generated by iptables-save v1.4.21 on Wed Feb 28 12:19:33 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [34:3136]
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT    ## 加上此行
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibite
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Wed Feb 28 12:19:33 2018
~                                                                                                  
~                                                                                                       
"/etc/sysconfig/iptables" 17L, 654C
  • (10)運行本地python連接MySQL代碼,結果如下:
datebase version : 5.7.20-enterprise-commercial-advanced

MySQL遠程連接失敗(錯誤碼:2003)