Mysql连接器python - 在本地和生产中呈现不同的行为

时间:2017-08-11 15:34:15

标签: python mysql mysql-connector mysql-connector-python

我有一个运行测试代码的简单容器,它使用MySQL connector / python(2.1.4)连接到MySQL服务器数据库,运行一个简单的查询,计算结果并每隔1秒打印一次。如果发生任何连接错误,则只需打印,忽略它,然后开始下一个循环迭代。

UITableView

它在我的本地机器上运行完美,这是一个示例日志:

import time
import os
import mysql.connector
import datetime

while True:
  time.sleep(1)

  try:
    conn = mysql.connector.connect(host=os.environ['DB_SERVER_NAME'], 
                                port=int(os.environ['DB_PORT']),
                                user=os.environ['DB_USER'], 
                                password=os.environ['DB_PASSWORD'],
                                database=os.environ['DB_NAME'])
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM accounts")
    print('{} - no-peewee-mysql-connector counting accounts: {}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), len(cursor.fetchall())))
    cursor.close()
  except mysql.connector.Error as err:
    if err.errno == mysql.connector.errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == mysql.connector.errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        # print("connection error: {}".format(err.sqlstate))
        print("Error code:", err.errno)        # error number
        print("SQLSTATE value:", err.sqlstate) # SQLSTATE value
        print("Error message:", err.msg)       # error message
        print("Error:", err)                   # errno, sqlstate, msg values
        s = str(err)
        print("Error:", err)                   # errno, sqlstate, msg values
        print(type(err))
  finally: 
    try:
        conn.close()
    except Exception as err:
        print("closing connection error: {}".format(err))

在这种情况下,由我引起的连接错误,启动和停止mysql服务器。

在Amazon ECS上,一夜之间的行为很奇怪。它从晚上11点开始正常工作,直到下午3点,CPU的某些峰值明显从0.3%上升到1%,并不令人担忧但很奇怪。然后在上午10点,CPU消耗平均为0.75%,并且很多连接错误开始发生,它们更频繁,但不是恒定的。

错误消息如下:

2017-08-11 15:20:47 - no-peewee-mysql-connector counting accounts: 0

2017-08-11 15:20:48 - no-peewee-mysql-connector counting accounts: 0

2017-08-11 15:20:49 - no-peewee-mysql-connector counting accounts: 0

2017-08-11 15:20:50 - no-peewee-mysql-connector counting accounts: 0

2017-08-11 15:20:51 - no-peewee-mysql-connector counting accounts: 0

Error code: 2013
SQLSTATE value: None
Error message: Lost connection to MySQL server during query
Error: 2013: Lost connection to MySQL server during query
Error: 2013: Lost connection to MySQL server during query
<class 'mysql.connector.errors.InterfaceError'>

Error code: 2013
SQLSTATE value: None
Error message: Lost connection to MySQL server during query
Error: 2013: Lost connection to MySQL server during query
Error: 2013: Lost connection to MySQL server during query
<class 'mysql.connector.errors.InterfaceError'>

Error code: 2003
SQLSTATE value: None
Error message: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
<class 'mysql.connector.errors.InterfaceError'>

Error code: 2003
SQLSTATE value: None
Error message: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
<class 'mysql.connector.errors.InterfaceError'>

Error code: 2003
SQLSTATE value: None
Error message: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
<class 'mysql.connector.errors.InterfaceError'>

Error code: 2003
SQLSTATE value: None
Error message: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
Error: 2003: Can't connect to MySQL server on '172.17.0.1:4002' (111 Connection refused)
<class 'mysql.connector.errors.InterfaceError'>

2017-08-11 15:21:00 - no-peewee-mysql-connector counting accounts: 0

2017-08-11 15:21:01 - no-peewee-mysql-connector counting accounts: 0

请注意,它与我在本地收到的(111连接拒绝)子代码不同。

我有其他三个实验运行相同的任务,但是使用不同的ORM和mysql客户端lib配置,其他三个都表现良好。

这种行为看起来很可疑,但我找不到一个好的解释。由于整晚都很明显,我无法在本地环境中轻松调试。

任何指导都具有很高的价值。

0 个答案:

没有答案