Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/pyinfra-dev/pyinfra/llms.txt

Use this file to discover all available pages before exploring further.

Manage MySQL databases, users and privileges. Requires the mysql CLI executable on the target host(s). All operations in this module take four optional arguments:
  • mysql_user: the username to connect to mysql to
  • mysql_password: the password for the connecting user
  • mysql_host: the hostname of the server to connect to
  • mysql_port: the port of the server to connect to

Functions

mysql.sql

Execute arbitrary SQL against MySQL.
mysql.sql(
    sql,
    database=None,
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)

mysql.user

Add/remove/update MySQL users.
mysql.user(
    user,
    present=True,
    user_hostname="localhost",
    password=None,
    privileges=None,
    require=None,
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)
Hostname: this + user makes the username - so changing this will create a new user, rather than update users with the same user.Password: will only be applied if the user does not exist - ie pyinfra cannot detect if the current password doesn’t match the one provided, so won’t attempt to change it.

mysql.database

Add/remove MySQL databases.
mysql.database(
    database,
    present=True,
    collate=None,
    charset=None,
    user=None,
    user_hostname="localhost",
    user_privileges="ALL",
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)

mysql.privileges

Add/remove MySQL privileges for a user, either global, database or table specific.
mysql.privileges(
    user,
    privileges,
    user_hostname="localhost",
    database="*",
    table="*",
    flush=True,
    with_grant_option=False,
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)

mysql.dump

Dump a MySQL database into a .sql file. Requires mysqldump.
mysql.dump(
    dest,
    database=None,
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)

mysql.load

Load .sql file into a database.
mysql.load(
    src,
    database=None,
    mysql_user=None,
    mysql_password=None,
    mysql_host=None,
    mysql_port=None,
)

Examples

from pyinfra.operations import mysql

# Create a MySQL user
mysql.user(
    name="Create the pyinfra@localhost MySQL user",
    user="pyinfra",
    password="somepassword",
)

# Create a database
mysql.database(
    name="Create the pyinfra_stuff database",
    database="pyinfra_stuff",
    user="pyinfra",
    user_privileges=["SELECT", "INSERT"],
    charset="utf8",
)

# Dump a database
mysql.dump(
    name="Dump the pyinfra_stuff database",
    dest="/tmp/pyinfra_stuff.dump",
    database="pyinfra_stuff",
)

Build docs developers (and LLMs) love