mysql - Python 3 pymysql 数据库访问 - 连接到数据库出现以下错误

标签 mysql database python-3.x pymysql

在通过 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, in create_connection raise err File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 703, in create_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 Connect return 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 connect raise 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/

相关文章:

mysql - 具有大量连接的 Web 服务

functional-programming - 函数式编程更好地操作数据库数据列表?

python - 将 Pandas 数据框转换为字典,其中键是索引,值是列值列表

python - 如何在 matplotlib 中获取轴的主要刻度线的长度?

python - 有没有人有 Python 3 备忘单

php - MySQL 日期在 Highstock 中显示为年份

php - 我将 PHP 文件上传到在线文件管理器,但当我打开我的域时,我看不到它们

php - MySql SELECT 从多条记录中获取1条记录

mysql - 在 mysql 存储过程中执行保证和失败回滚

javascript - firebase.database() 不是函数 - 新的 Firebase 项目