在通过 python 3.5 连接数据库时,任何人都可以帮助我解决这个问题。接收错误后。
import pymysql
# Open database connection
db = pymysql.connect("localhost","testuser","test123","TESTDB", )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
db.close()
错误:
RESTART: C:/Program Files (x86)/Python35-32/db_connect.py Traceback (most recent call last): File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 916, in connect
**kwargs
) File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 712, increate_connection
raise err
File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 703, increate_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last): File "C:/Program Files (x86)/Python35-32/db_connect.py", line 4, in
db = pymysql.connect("localhost","testuser","test123","TESTDB", )
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql__init__.py", line 90, in Connectreturn Connection(*args, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 706, in __ init __self.connect()
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pymysql-0.7.11-py3.5.egg\pymysql\connections.py", line 963, in connectraise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] No connection could be made because the target machine actively refused it)")
最佳答案
来自help
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 network port you specified is the one configured on the server.
检查您是否正在运行 mysql 并尝试连接到适当的端口(默认为 3306)
关于mysql - Python 3 pymysql 数据库访问 - 连接到数据库出现以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912664/