Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database specificity in sql dump file generated using Ansible mysql_db module

Tags:

mysql

ansible

I'm using the Ansible mysql_db module to create a sql dump file of a database on a remote server. I'm then downloading this dump file to import into a local database that has a different name.

My issue is that the dump file that the mysql_db module creates is prefixed with the CREATE DATABASE and USE commands. E.g.:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `remote_db_name` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `remote_db_name`;

This is causing issues as, when I try to import this dump into my local database (that has a different name), it's creating a whole new database locally and importing into that instead.

Are there any options you can pass to the Ansible mysql_db module to prevent it adding the CREATE DATABASE and USE commands to the dump file?

Here is the line in my Ansible playbook that creates the sql dump file:

mysql_db: state=dump name={{ db_name }} login_user={{ db_user }} login_password={{ db_password }} target=/var/www/{{ domain_name }}/db_backups/{{ db_name }}_{{ dumpdate }}.sql

Remote server is running Ubuntu 18.04 with MySQL Ver 14.14 Distrib 5.7.28 if that makes any difference.

like image 614
Paul Goodfield Avatar asked Feb 12 '26 17:02

Paul Goodfield


1 Answers

Transforming comment as answer upon OP's request


There is an option you can pass to mysqldump (i.e. --no-create-db) but the ansible module does not seem to support it.

So at time of writing this answer (6th of December 2019) you either need to:

  1. vote for an evolution ticket if it already exists or create one on the github project
  2. create a pull request yourself on the github project to add the option to the module
  3. create the dump exactly as you need it by calling mysqldump yourself through command or shell
like image 130
Zeitounator Avatar answered Feb 14 '26 21:02

Zeitounator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!