【发布时间】:2015-11-27 02:11:31
【问题描述】:
我刚刚在服务器上安装了新的 CentOS 操作系统并恢复了所有数据库。 我试图使用 Python 脚本从 Windows 远程获取数据。 它给我返回了这个错误
PS C:\Python27\practice> python .\server_test_script.py
Traceback (most recent call last):
File ".\server_test_script.py", line 9, in <module>
db="transactions") # name of the data base
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.XXX' (10060)")
我已经用 bind-address=0.0.0.0 更改了我的 /etc/my.cnf 文件。还有一件事,当我使用 yum 命令下载 mysql 时,它安装了 mariadb 服务器。是这个问题吗,但我不认为两者都使用相同的包提供相同的服务。我已经将自己创建为具有所有权限的用户,但它仍然没有工作。
这是我的 Python 脚本
import MySQLdb
con = MySQLdb.connect(host="192.168.1.xxx", # your host, usually localhost
user="shubham", # your username
passwd="xxxxx", # your password
db="transactions") # name of the data base
cur = con.cursor()
def getTransactioncount():
cur.execute("select count(tid) from transaction")
row=cur.fetchall()
count=row[0][0]
return count
print getTransactioncount()
谁能帮我找出我做错的地方 谢谢
【问题讨论】:
-
您确定 MySQL 服务器实际上在您的 centos 系统上运行,它正在侦听标准端口(因为您没有在客户端代码中指定端口,我假设它使用默认端口(3306?)你确定没有防火墙阻止从你的客户端到你的服务器的网络连接吗?你的问题看起来像一个基本的 server-not-running-on-expected-port 或 server-not-contactable 类型的问题.
-
感谢您的回复。我刚刚检查过,它在默认端口 3306 上运行,我还尝试在关闭客户端 PC 上的防火墙后运行脚本,但仍然没有运气。
-
CentOs 中的防火墙怎么样,是让 3306 通过吗?能否在服务器本地成功运行脚本?从客户端,您可以 telnet(可能使用 putty 吗?)到服务器端口 3306 - 只需连接即可确认网络路径畅通。
-
SO 终于成功了,就在尝试禁用客户端上的防火墙之后,我尝试关闭服务器上的防火墙并且它成功了。谢谢
标签: python mysql network-programming centos mariadb