python使用连接池连接MYSQL


import mysql.connector.pooling

MYSQL_HOST = 'localhost'

MYSQL_PORT = 3306

MYSQL_USER = 'root'

MYSQL_PWD = '_pass999'

MYSQL_DB_NAME = 'TEST'

mysql_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="mypool", pool_size=2, host=MYSQL_HOST, port=MYSQL_PORT,database=MYSQL_DB_NAME, user=MYSQL_USER, password=MYSQL_PWD,pool_reset_session=True)

try:

    conn = mysql_pool.get_connection() #get connection from connect pool

    cursor = conn.cursor()

    sql = 'select id FROM test ORDER BY id DESC LIMIT 1'

    cursor.execute(sql)

    data = cursor.fetchall()

print data

except Exception as e:

print e

finally: # must close cursor and conn!!

    cursor.close()

    conn.close()

你可能感兴趣的:(python使用连接池连接MYSQL)