MySQL (MariaDB) [WinError 10053] 已建立的连接被主机中的软件中止
Posted
技术标签:
【中文标题】MySQL (MariaDB) [WinError 10053] 已建立的连接被主机中的软件中止【英文标题】:MySQL (MariaDB) [WinError 10053] An established connection was aborted by the software in your host machine 【发布时间】:2021-11-12 17:27:30 【问题描述】:所以,我在 mysql 上苦苦挣扎
我尝试使用mysql.connector
,但事实证明,当我通过sshtunnel
连接时它并不想合作
于是我转到pymysql
,这是我能写的最基本的代码:
import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(('192.168.0.x', 22), ssh_username='pi', ssh_password='*********', remote_bind_address=('localhost', 3306)) as tunnel:
tunnel.start()
mydb = pymysql.connect(host="localhost",
user='Mashu',
passwd='******',
port=tunnel.local_bind_port,
db='Special_Channels')
print(mydb)
query = "SELECT * FROM Daily"
cur = mydb.cursor()
data = cur.execute(query)
print(data)
虽然
cur = mydb.cursor()
它引发了一个错误:
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
在更高的层次上是:
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10053] An established connection was aborted by the software in your host machine)')
我确定数据库和表存在,并且这个 mysql 帐户是可以访问的,因为我已经打开它并在其他软件中对其进行了更改(如果有人想知道的话,DataGrip)
【问题讨论】:
【参考方案1】:我建议 mariadb 代替 pymysql 来解决您的问题
pip install mariadb
【讨论】:
【参考方案2】:解决方案非常简单。
它应该都在with
语句内,因为隧道连接(因此数据库访问)在语句外关闭,所以它应该如下所示:
import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(('192.168.0.x', 22), ssh_username='pi', ssh_password='*********', remote_bind_address=('localhost', 3306)) as tunnel:
tunnel.start()
mydb = pymysql.connect(host="localhost",
user='Mashu',
passwd='******',
port=tunnel.local_bind_port,
db='Special_Channels')
print(mydb)
query = "SELECT * FROM Daily"
cur = mydb.cursor()
data = cur.execute(query)
print(data)
【讨论】:
以上是关于MySQL (MariaDB) [WinError 10053] 已建立的连接被主机中的软件中止的主要内容,如果未能解决你的问题,请参考以下文章