ansible.builtin.mysql_db (v2.9.24) — module

Add or remove MySQL databases from a remote host.

| "added in version" 0.6 of ansible.builtin"

Authors: Ansible Core Team

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.24

Description

Add or remove MySQL databases from a remote host.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a new database with name 'bobdata'
  mysql_db:
    name: bobdata
    state: present
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create new databases with names 'foo' and 'bar'
  mysql_db:
    name:
      - foo
      - bar
    state: present
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Copy database dump file to remote host and restore it to database 'my_db'
- name: Copy database dump file
  copy:
    src: dump.sql.bz2
    dest: /tmp
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Restore database
  mysql_db:
    name: my_db
    state: import
    target: /tmp/dump.sql.bz2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Dump multiple databases
  mysql_db:
    state: dump
    name: db_1,db_2
    target: /tmp/dump.sql
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Dump multiple databases
  mysql_db:
    state: dump
    name:
      - db_1
      - db_2
    target: /tmp/dump.sql
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Dump all databases to hostname.sql
  mysql_db:
    state: dump
    name: all
    target: /tmp/dump.sql
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Import file.sql similar to mysql -u <username> -p <password> < hostname.sql
  mysql_db:
    state: import
    name: all
    target: /tmp/dump.sql
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Delete database with name 'bobdata'
  mysql_db:
    name: bobdata
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Make sure there is neither a database with name 'foo', nor one with name 'bar'
  mysql_db:
    name:
      - foo
      - bar
    state: absent

Inputs

    
name:
    aliases:
    - db
    description:
    - name of the database to add or remove.
    - I(name=all) May only be provided if I(state) is C(dump) or C(import).
    - List of databases is provided with I(state=dump), I(state=present) and I(state=absent).
    - if name=all Works like --all-databases option for mysqldump (Added in 2.0).
    elements: str
    required: true
    type: list

quick:
    default: 'yes'
    description:
    - Option used for dumping large tables
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

state:
    choices:
    - present
    - absent
    - dump
    - import
    default: present
    description:
    - The database state

target:
    description:
    - Location, on the remote host, of the dump file to read from or write to. Uncompressed
      SQL files (C(.sql)) as well as bzip2 (C(.bz2)), gzip (C(.gz)) and xz (Added in 2.0)
      compressed files are supported.

ca_cert:
    aliases:
    - ssl_ca
    description:
    - The path to a Certificate Authority (CA) certificate. This option, if used, must
      specify the same certificate as used by the server.
    type: path

encoding:
    description:
    - Encoding mode to use, examples include C(utf8) or C(latin1_swedish_ci)

collation:
    description:
    - Collation mode (sorting). This only applies to new table/databases and does not
      update existing ones, this is a limitation of MySQL.

client_key:
    aliases:
    - ssl_key
    description:
    - The path to the client private key.
    type: path

login_host:
    default: localhost
    description:
    - Host running the database.
    - In some cases for local connections the I(login_unix_socket=/path/to/mysqld/socket),
      that is usually C(/var/run/mysqld/mysqld.sock), needs to be used instead of I(login_host=localhost).
    type: str

login_port:
    default: 3306
    description:
    - Port of the MySQL server. Requires I(login_host) be defined as other than localhost
      if login_port is used.
    type: int

login_user:
    description:
    - The username used to authenticate with.
    type: str

client_cert:
    aliases:
    - ssl_cert
    description:
    - The path to a client public key certificate.
    type: path

config_file:
    default: ~/.my.cnf
    description:
    - Specify a config file from which user and password are to be read.
    type: path

ignore_tables:
    default: []
    description:
    - A list of table names that will be ignored in the dump of the form database_name.table_name
    required: false
    version_added: '2.7'
    version_added_collection: ansible.builtin

check_hostname:
    description:
    - Whether to validate the server host name when an SSL connection is required. Corresponds
      to MySQL CLIs C(--ssl) switch.
    - Setting this to C(false) disables hostname verification. Use with caution.
    - Requires pymysql >= 0.7.11.
    - This option has no effect on MySQLdb.
    type: bool
    version_added: 1.1.0
    version_added_collection: community.mysql

login_password:
    description:
    - The password used to authenticate with.
    type: str

connect_timeout:
    default: 30
    description:
    - The connection timeout when connecting to the MySQL server.
    type: int

login_unix_socket:
    description:
    - The path to a Unix domain socket for local connections.
    type: str

single_transaction:
    default: 'no'
    description:
    - Execute the dump in a single transaction
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

Outputs

db:
  description: Database names in string format delimited by white space.
  returned: always
  sample: foo bar
  type: str
db_list:
  description: List of database names.
  returned: always
  sample:
  - foo
  - bar
  type: list
  version_added: '2.9'
  version_added_collection: ansible.builtin