为什么我会有这个错误?
Mysql.connector.errors.DatabaseError: 2003 (HY000):无法连接到MySQL服务器n‘3.XX.XXX.89 (110)
我正在从MQTT收集数据,并将其存储在MySQL数据库中。为了订阅MQTT上的主题,我使用Paho MQTT客户端,为了连接到MySQL数据库服务器,我使用MySQL python连接器。我在Amazon实例(Virtual )上连续地在后台运行这个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")用于mqtt和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服务器而不导致此错误呢?
发布于 2020-05-22 11:21:52
客户端(如您的python程序)和MySQL之间的连接在不使用时不会永远保持打开状态。MySQL在其wait_timeout或interactive_timeout到期时删除连接。
听起来你的程序整天都在工作,整个晚上都在睡觉(就像Monty中的伐木工人一样)。当它醒来时,它与数据库没有连接,因为MySQL关闭了它。
你能做些什么?按我的优先次序:
SELECT 1;之类的非操作操作。如果该操作引发异常,则重新打开数据库连接。这是一个很好的解决方案,因为如果必须重新启动MySQL数据库服务器,它将使您的应用程序更具弹性。有时云提供商(AWS、Azure、Digital等)不得不让主机停止服务。当他们这样做,他们反弹客户虚拟机,以便他们可以开始在新的主机。如果你幸运的话,他们会先警告你。SELECT 1;来保持活力,这样MySQL就不会将wait_timeout值关闭到足够长的时间。这将它设置为一周。SET @@GLOBAL.wait_timeout=604800使用86400一天。
了解如何设置超时也很有用,因为它使您无需等待很长时间就可以测试该问题的解决方案。
https://stackoverflow.com/questions/61951269
复制相似问题