PyMySQL==0.7.11, MySQL server has gone away
PyMySQL==0.7.11, MySQL server has gone away
我在 Python Tornado 框架上编写了一个应用程序。
我写了一个脚本,通过隧道侦听消息,当我的条件满足时,脚本连接到 MySQL 并进行插入或更新,但很长一段时间后我收到此错误 MySQL 已断开连接。
听隧道我用鼠兔模块。所以当一切发生时我得到这个
Pika: Could not connect to host 127.0.0.1 on port 5671
MySQL: (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))")
我的代码如下:
import pika
def main():
credentials = pika.PlainCredentials('user', 'password')
try:
cp = pika.ConnectionParameters(
host='127.0.0.1',
port=5671,
credentials=credentials,
ssl=False,
)
connection = pika.BlockingConnection(cp)
channel = connection.channel()
def callback(ch, method, properties, body):
if 'messageType' in properties.headers:
message_type = properties.headers['messageType']
if message_type in allowed_message_types:
result = proto_file._reflection.ParseMessage(descriptors[message_type], body)
if result:
result = protobuf_to_dict(result)
if message_type == '1':
Model.message_event(data=result)
else:
print('Message type not in allowed list = ' + str(message_type))
print('continue listening...')
channel.basic_consume(callback, queue='queue', no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
except Exception as e:
print('Could not connect to host 127.0.0.1 on port 5671')
print(str(e))
这是连接到 MySQL
的 message_event 模型
import pymysql
import pymysql.cursors
class Model(object):
def __init__(self, conf):
self.conf = conf
conv = pymysql.converters.conversions.copy()
conv[246] = Decimal
conv[10] = str
self.mysql = {pymysql.connect(
host=self.conf['host'],
user=self.conf['user'],
password=self.conf['password'],
db=self.conf['db'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
autocommit=self.conf['autocommit'],
conv=conv)}
def message_event(self, data):
with self.mysql.cursor() as cursor:
// here doing following operations
// select, insert or update
cursor.close()
// and after sometime i get this
Pika: Could not connect to host 127.0.0.1 on port 5671
MySQL: (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))")
主点一旦连接到隧道,必须无限监听消息并使用里面的消息进行操作MySQL,但一段时间后连接断开。
如果可能的话,请在不更改 MySQL 服务器配置的情况下解决此问题的任何想法。
我想 post 我的答案有效,只有这个解决方案对我有效
在连接到mysql之前检查连接是否打开,如果没有重新连接
if not self.mysql.open:
self.mysql.ping(reconnect=True)
我在 Python Tornado 框架上编写了一个应用程序。
我写了一个脚本,通过隧道侦听消息,当我的条件满足时,脚本连接到 MySQL 并进行插入或更新,但很长一段时间后我收到此错误 MySQL 已断开连接。
听隧道我用鼠兔模块。所以当一切发生时我得到这个
Pika: Could not connect to host 127.0.0.1 on port 5671
MySQL: (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))")
我的代码如下:
import pika
def main():
credentials = pika.PlainCredentials('user', 'password')
try:
cp = pika.ConnectionParameters(
host='127.0.0.1',
port=5671,
credentials=credentials,
ssl=False,
)
connection = pika.BlockingConnection(cp)
channel = connection.channel()
def callback(ch, method, properties, body):
if 'messageType' in properties.headers:
message_type = properties.headers['messageType']
if message_type in allowed_message_types:
result = proto_file._reflection.ParseMessage(descriptors[message_type], body)
if result:
result = protobuf_to_dict(result)
if message_type == '1':
Model.message_event(data=result)
else:
print('Message type not in allowed list = ' + str(message_type))
print('continue listening...')
channel.basic_consume(callback, queue='queue', no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
except Exception as e:
print('Could not connect to host 127.0.0.1 on port 5671')
print(str(e))
这是连接到 MySQL
的 message_event 模型import pymysql
import pymysql.cursors
class Model(object):
def __init__(self, conf):
self.conf = conf
conv = pymysql.converters.conversions.copy()
conv[246] = Decimal
conv[10] = str
self.mysql = {pymysql.connect(
host=self.conf['host'],
user=self.conf['user'],
password=self.conf['password'],
db=self.conf['db'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
autocommit=self.conf['autocommit'],
conv=conv)}
def message_event(self, data):
with self.mysql.cursor() as cursor:
// here doing following operations
// select, insert or update
cursor.close()
// and after sometime i get this
Pika: Could not connect to host 127.0.0.1 on port 5671
MySQL: (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))")
主点一旦连接到隧道,必须无限监听消息并使用里面的消息进行操作MySQL,但一段时间后连接断开。
如果可能的话,请在不更改 MySQL 服务器配置的情况下解决此问题的任何想法。
我想 post 我的答案有效,只有这个解决方案对我有效
在连接到mysql之前检查连接是否打开,如果没有重新连接
if not self.mysql.open: self.mysql.ping(reconnect=True)