aiomysql — API Reference

Connection

The library provides a way to connect to MySQL database with simple factory function aiomysql.connnect(). Use this function if you want just one connection to the database, consider connection pool for multiple connections.

Example:

import asyncio
import aiomysql


@asyncio.coroutine
def go():
    conn = yield from aiomysql.connect(database='aiomysql',
                                    user='root',
                                    password='secret',
                                    host='127.0.0.1')
    cur = yield from conn.cursor()
    yield from cur.execute("SELECT * FROM tbl")
    ret = yield from cur.fetchall()
connect(host="localhost", user=None, password="",
db=None, port=3306, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=decoders, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=None, read_default_group=None,
no_delay=False, autocommit=False, echo=False, loop=None)

A coroutine that connects to MySQL.

The function accepts all parameters that pymysql.connect() does plus optional keyword-only loop and timeout parameters.

Parameters:
  • host (str) – host where the database server is located, default: localhost.
  • user (str) – username to log in as.
  • password (str) – password to use.
  • db (str) – database to use, None to not use a particular one.
  • port (int) – MySQL port to use, default is usually OK.
  • unix_socket (str) – optionally, you can use a unix socket rather than TCP/IP.
  • charset (str) – charset you want to use, for example ‘utf8’.
  • sql_mode – default sql-mode to use, like ‘NO_BACKSLASH_ESCAPES’
  • read_default_file – specifies my.cnf file to read these parameters from under the [client] section.
  • conv – decoders dictionary to use instead of the default one. This is used to provide custom marshalling of types. See pymysql.converters.
  • use_unicode – whether or not to default to unicode strings.
  • client_flag – custom flags to send to MySQL. Find potential values in pymysql.constants.CLIENT.
  • cursorclass – custom cursor class to use.
  • init_command (str) – initial SQL statement to run when connection is established.
  • connect_timeout – Timeout before throwing an exception when connecting.
  • read_default_group (str) – Group to read from in the configuration file.
  • no_delay (bool) – disable Nagle’s algorithm on the socket
  • autocommit – Autocommit mode. None means use server default. (default: False)
  • loop – asyncio event loop instance or None for default one.
Returns:

Connection instance.