Python下mysql数据库连接池

文章目录

        • 前言
        • 使用模块
        • 创建连接池
        • 取出连接
        • 连接放回池子

前言

参考我的文章 Python操作mysql
连接池能复用mysql连接,节约资源

使用模块

import mysql.connector.pooling

创建连接池

    #  mysql.connector.pooling.MySQLConnectionPool()
    #  pool_size 池子连接数
    config = {
        "host": "47.98.164.90",
        "port": "3306",
        "user": "sun_riches_test",
        "password": "sun_riches_test@test",
        "database": "sun_riches_test",
        "autocommit": True,
        "pool_size": 20
    }
    pool = mysql.connector.pooling.MySQLConnectionPool(**config)

取出连接

    # con 连接
    # 同python操作mysql的用法一致
    con = pool.get_connection()

连接放回池子

    con.close()

你可能感兴趣的:(python,python,mysql)