美文网首页
python使用连接池连接MYSQL

python使用连接池连接MYSQL

作者: vcancy | 来源:发表于2016-10-31 20:29 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/hzvputtx.html