The library provides connection pool as well as plain Connection objects.

The basic usage is:

import asyncio
import aiomysql

loop = asyncio.get_event_loop()

async def go():
    pool = await aiomysql.create_pool(host='', port=3306,
                                      user='root', password='',
                                      db='mysql', loop=loop, autocommit=False)

    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 10")
            # print(cur.description)
            (r,) = await cur.fetchone()
            assert r == 10
    await pool.wait_closed()

create_pool(minsize=1, maxsize=10, loop=None, **kwargs)

A coroutine that creates a pool of connections to MySQL database.

  • minsize (int) – minimum sizes of the pool.

  • maxsize (int) – maximum sizes of the pool.

  • loop – is an optional event loop instance, asyncio.get_event_loop() is used if loop is not specified.

  • echo (bool) – – executed log SQL queryes (False by default).

  • kwargs – The function accepts all parameters that aiomysql.connect() does plus optional keyword-only parameters loop, minsize, maxsize.

  • pool_recycle (float) – number of seconds after which connection is recycled, helps to deal with stale connections in pool, default value is -1, means recycling logic is disabled.


Pool instance.

class Pool

A connection pool.

After creation pool has minsize free connections and can grow up to maxsize ones.

If minsize is 0 the pool doesn’t creates any connection on startup.

If maxsize is 0 than size of pool is unlimited (but it recycles used connections of course).

The most important way to use it is getting connection in with statement:

async with pool.acquire() as conn:
    async with conn.cursor() as cur:

See also Pool.acquire() and Pool.release() for acquiring Connection without with statement.


Return echo mode status. Log all executed queries to logger named aiomysql if True


A minimal size of the pool (read-only), 1 by default.


A maximal size of the pool (read-only), 10 by default.


A current size of the pool (readonly). Includes used and free connections.


A count of free connections in the pool (readonly).


A coroutine that closes all free connections in the pool. At next connection acquiring at least minsize of them will be recreated.


Close pool.

Mark all pool connections to be closed on getting back to pool. Closed pool doesn’t allow to acquire new connections.

If you want to wait for actual closing of acquired connection please call wait_closed() after close().


The method is not a coroutine.


Terminate pool.

Close pool with instantly closing all acquired connections also.

wait_closed() should be called after terminate() for waiting for actual finishing.


The method is not a coroutine.


A coroutine that waits for releasing and closing all acquired connections.

Should be called after close() for waiting for actual pool closing.


A coroutine that acquires a connection from free pool. Creates new connection if needed and size of pool is less than maxsize.

Returns a Connection instance.


Reverts connection conn to free pool for future recycling.


The method is not a coroutine.