MySQL远程连接失败(错误码:2003)python第2版课后题答案
MySQL远程连接失败(错误码:2003)
更新于2018年3⽉12⽇
⼀环境信息
服务器系统:Oracle Linux 7.3
服务器MySQL版本:MySQL 5.7.20
本地系统:win10
本地客户端:Navicat for MySQL 10.1.7
本地开发环境:python(3.6.3),PyMySQL(0.8.0)
假设:登录⽤户名为admin,密码为adminpwd ,MySQL连接端⼝3306
⼆问题描述
本地客户端及代码连接均失败: 2003, "Can't connect to MySQL server on '192.168.1.166' (10061)"
python连接MySQL代码
import pymysql
conn= t(
host='192.168.1.166',
port = 3306,
user='admin',
passwd='adminpwd',
db ='test',
charset='utf8'
)
# 使⽤cursor⽅法获取操作游标
cur = conn.cursor()
# 使⽤execute ⽅法执⾏sql语句
# 使⽤fetchone()⽅法获取⼀条数据库
data = cur.fetchone()
print("datebase version : %s"%data)
# 关闭数据库连接mysql无法连接到服务器
conn.close()
python连接MySQL报错
Traceback (most recent call last):
File "D:/JetBrains/test/study_test/mysql_test1.py", line 15, in <module>
charset='utf8'
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\connections.py", line 699, in __init__
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\connections.py", line 967, in connect
raise exc
三官⽅⽂档描述
"The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the netwo ⾦⼭译⽂:错误(2003年)⽆法连接到“server”(10061)上的mysql服务器,表⽰⽹络连接已被拒绝。您应该检查是否有⼀个mysql服务器正在运⾏,它是否启⽤了⽹络连接,并且您指定的⽹络端⼝是在服务器上配置的。
四解决过程
(1) Xshell远程登陆服务器,⽤“ps aux|grep mysql”命令查看,MySQL服务已启动:
(2)⽤"vim myf"命令查看myf⽂件(在MySQL安装⽬录下,我的位置是/usr/local/mysql/myf),修改其对应的值并重启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
python面向对象
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;
telelift是什么意思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.⽆法打开到主机的连接。在端⼝ 3306: 连接失败",说明是防⽕墙的问题
(如果Win10 telnet不是内部或外部命令,决解⽅法参考连接:
(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”(如下所⽰),保存⽂件并⽤“ser
php成绩判定
vice 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
optionvalue值怎么取(10)运⾏本地python连接MySQL代码,结果如下:
datebase version : 5.7.20-enterprise-commercial-advanced

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。