如何创建一个mysql连接池或更好的方式来初始化多个数据库? [英] How to create a mysql connection pool or any better way to initialize the multiple databases?

查看:370
本文介绍了如何创建一个mysql连接池或更好的方式来初始化多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我打开了两个mysql连接,并使用HTTP请求将数据插入到数据库中

  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 /< db>)
def insert(db):
将数据插入到mysql1数据库中的代码
#code for insert into mysql2 database

我正在发送HTTP请求来选择数据库。

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

它运行良好,数据正在插入选定的数据库。
但是我正在考虑为这两个连接创建一个连接池,然后使用这个池。



问题:


  1. 如何实现mysql连接池?

  2. 是否有其他更好的连接初始化方法。当前的连接会在每个请求中被打开。

  3. 使用ORM框架进行制作 事情更容易,下面是一个基本的和一般的方法,我们创建一个没有任何ORM框架的连接池。



    1. mysql.connector.pooling 模块实现共享。

    2. 一个池打开一些连接并处理线程安全当
      提供与请求者的连接时。

    3. 连接池的大小可以在池创建时配置。
      此后无法调整大小。

    创建您自己的池在连接池的参数中命名 myPool ,还可以声明池大小= 5(这是数据库连接的数量)。



    请参阅下文以获取更多信息:

      dbconfig = {
    database:test,
    user:joe
    }

    cnx = mysql.connector.connect(pool_name =mypool,
    pool_size = 3,
    ** dbconfig)

    dbconfig,您每次更改数据库时都会提供所有配置的详细信息。实际上你可以有多个数据库,如果你想的话。

    请看这个 MySQL文档

    我们可以看到更多关于如何声明这个参数:

      MySQLConnectionPool(pool_name = None,
    pool_size = 5,
    pool_reset_session = True,
    ** kwargs)

    这个构造函数实例化一个对象管理一个连接池。



    详细的参数:

      1 。 pool_name:池名称。如果没有给出这个参数,Connector / Python会自动生成名称,这个名称是由kwargs中给出的主机,端口,用户和数据库连接参数中的任何一个组成的。 

    多个池具有相同的名称并不是错误。必须通过
    ** pool_name **属性区分池的应用程序应该使用不同的名称创建每个池。

    2. pool_size:池大小。如果没有给出这个参数,默认是5.

    你应该看到这个很好的文档这里



    为了让你的连接池多线程化,stackoverflw上的这个帖子可能真的有帮助。请参阅此文章


    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/<db>')  
    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:

    1. How to implement the mysql connection pooling?

    2. 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.

    1. The mysql.connector.pooling module implements pooling.

    2. A pool opens a number of connections and handles thread safety when providing connections to requesters.

    3. 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.

    Please see this MySQL documentation here

    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

    这篇关于如何创建一个mysql连接池或更好的方式来初始化多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆