创建多个mysql连接池,如何创建一个mysql连接池或更好的方式来初始化多个数据库?...

In my code I am opening two mysql connections and using HTTP requests to insert data into database

g.db = mysql.connector.connect(user=a ,password=password, host=localhost,database=mysq1)

g.db1 = mysql.connector.connect(user=b,password=password, host=localhost,database=mysql2)

@app.route('/user/')

def insert(db):

#code for inserting data into mysql1 database

#code for inserting data into mysql2 database

I am making HTTP requests to select the databases.

curl -i 'localhost:5000/user/mysql1' #

It is working well, data is getting inserted into the selected database.

But I was thinking of creating a connection pool for the two connections and then used that pool.

Questions:

How to implement the mysql connection pooling?

Is there other better way of initializing connections.Currently connection get opened at each request.

解决方案

Use ORM frameworks for making things easier, below is a basic and a general way we create a connection pool with out any ORM frameworks.

The mysql.connector.pooling module implements pooling.

A pool opens a number of connections and handles thread safety when

providing connections to requesters.

The size of a connection pool is configurable at pool creation time.

It cannot be resized thereafter.

Create your own pool and name it, myPool in the arguments of connection pooling, you can also declare the pool size = 5 (which is the number of database connections).

Please see below for more information:

dbconfig = {

"database": "test",

"user": "joe"

}

cnx = mysql.connector.connect(pool_name = "mypool",

pool_size = 3,

**dbconfig)

dbconfig, database configuration is where you give all the configuration details, everytime you change your databse. In fact you can have multiple databases, if you want to.

We can see more about how this arguments can be declared:

MySQLConnectionPool(pool_name=None,

pool_size=5,

pool_reset_session=True,

**kwargs)

This constructor instantiates an object that manages a connection pool.

Arguments in detail:

1. pool_name: The pool name. If this argument is not given, Connector/Python automatically generates the name, composed from whichever of the host, port, user, and database connection arguments are given in kwargs, in that order.

It is not an error for multiple pools to have the same name. An application that must distinguish pools by their

**pool_name** property should create each pool with a distinct name.

2. pool_size: The pool size. If this argument is not given, the default is 5.

You should see this some nice documentation here

For making your connection pool multithreaded, this post on stackoverflw might really help. Please see this post

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值