为什么会出现此错误?
Mysql.connector.errors.DatabaseError:2003(HY000):无法连接到MySQL服务器n'3.XX.XXX.89'(110)
我正在从MQTT代理收集数据并将其存储在MySQL数据库中。要订阅MQTT代理上的主题,我正在使用Paho MQTT客户端,并且要连接到MySQL数据库服务器,我正在使用MySQL python连接器。我在后台在Amazon EC2 Ubuntu实例(虚拟服务器)上连续运行此python脚本:
import mysql.connector
import paho.mqtt.client as mqtt
#Mysql Settings
db = mysql.connector.connect(host='localhost',
user='user_name',
password='password',
db='db_name')
if db.is_connected():
db_Info = db.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = db.cursor()
#MQTT Settings
MQTT_Broker = "3.XX.XXX.89"
MQTT_Port = 1883
Keep_Alive_Interval = 60
MQTT_Topic = "my/publish/#"
#Subscribe
def on_connect(client, userdata, flags, rc):
mqttc.subscribe(MQTT_Topic, 0)
print("subscribed")
#Save data into DB Table
def on_message(mosq, obj, msg):
print("Insert from MQTT")
print("wait")
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
sql = """INSERT INTO gnss (topic, gnssdata) VALUES (%s,%s)"""
tuple = (msg.topic, msg.payload)
cursor.execute(sql, tuple)
db.commit()
print("Record inserted successfully into gnss table")
def on_subscribe(mosq, obj, mid, granted_qos):
pass
mqttc = mqtt.Client()
#Assign Event Callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
#Connect
mqttc.connect(MQTT_Broker, int(MQTT_Port), int(Keep_Alive_Interval))
#Continue the network loop and close db connection
mqttc.loop_forever()
db.close()
print("MySQL connection is closed")
该虚拟服务器上安装了Moqit和mysql服务器的Mosquitto。首先,当我运行此脚本时,将收集来自MQTT Broker的所有数据并将其存储到mysql数据库中。它在一整天内正常工作。第二天,没有做任何更改,我将数据发送到了MQTT经纪人。数据是由paho mqtt客户端收集的,但无法将其存储在mysql数据库中。错误和输出如下:
('Connected to MySQL Server version ', '5.7.30-0ubuntu0.18.04.1')
subscribed
Insert from MQTT
wait
my/publish/topic 0 acc, 10245, tyu
Record inserted successfully into gnss table
Insert from MQTT
wait
my/publish/topic 0 hello world
Record inserted successfully into gnss table
Insert from MQTT
wait
my/publish/topic 0 hello world
Record inserted successfully into gnss table
Insert from MQTT
wait
my/publish/topic 0 new test
Record inserted successfully into gnss table
Insert from MQTT
wait
my/publish/topic 0 19/05/2020
Insert from MQTT
wait
my/publish/topic 0 19/05/2020
Insert from MQTT
wait
my/publish/topic 0 Tuesday
Insert from MQTT
wait
my/publish/topic 0 Tuesday
Traceback (most recent call last):
File "mqtt.py", line 8, in <module>
db='nrf91')
File "/home/ubuntu/.local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 264, in connect
return CMySQLConnection(*args, **kwargs)
File "/home/ubuntu/.local/lib/python2.7/site-packages/mysql/connector/connection_cext.py", line 80, in __init__
self.connect(**kwargs)
File "/home/ubuntu/.local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 960, in connect
self._open_connection()
File "/home/ubuntu/.local/lib/python2.7/site-packages/mysql/connector/connection_cext.py", line 219, in _open_connection
sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '3.XX.XXX.89' (110)
我收到此mysql连接器数据库错误。如何解决这个错误?最初有与mysql服务器的连接,但第二天它就消失了。那么如何在不引起此错误的情况下始终保持与mysql服务器的连接呢?
客户端(例如python程序)与MySQL之间的连接在不使用时不会永远保持打开状态。当MySQL的wait_timeout
或interactive_timeout
过期时,MySQL将断开连接。
听起来您的程序整天都能工作,整夜都无法入睡(就像Monty Python schtick中的伐木工人一样)。当它唤醒时,它没有与数据库的连接,因为MySQL关闭了它。
您能对此做什么?按照我的偏好:
在您的程序处理每个排队的项目之前,请执行的无操作操作SELECT 1;
。如果该操作引发异常,请重新打开数据库连接。这是一个很好的解决方案,因为如果必须重新启动MySQL数据库服务器,它将使您的应用程序更具弹性。有时,云提供商(AWS,Azure,Digital Ocean等)必须使主机停止服务。当他们这样做时,他们会跳动客户虚拟机,以便他们可以在新的主机上启动。如果您很幸运,他们会先警告您。
更新程序,使其在工作队列为空时关闭其数据库连接,并在有工作要做时重新打开它。
每隔一分钟(一分钟?)执行一次无操作SELECT 1;
作为保持活动状态,因此MySQL不会关闭连接。
将wait_timeout
值更改为足够长的时间。这将其设置为一个星期。
SET @@GLOBAL.wait_timeout=604800
使用86400
一天。
知道如何设置超时也很有用,因为它使您无需等待很长时间即可测试此问题的解决方案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句