mariadbmariadb-10.3

Force mysqldump to use CREATE DATABASE IF NOT EXISTS


I've been going through the mysqldump documentation for MariaDB.

How do I force mysqldump to output CREATE DATABASE IF NOT EXISTS?

The best I've managed is to add the --databases flag:

mysqldump -u root -p --skip-set-charset --databases --default-character-set=latin1 database_name > /home/database_name.sql

However that outputs the following:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database_name` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci */;

I can't have the IF NOT EXISTS commented out. What am I missing?

Also: because someone is going to need to make a comment about it: the latin1 character set is a fix for the No mapping for the Unicode character exists in the target multi-byte code page error message that can be solved by this answer if you also follow through with the two terminal commands.


Solution

  • The comment /*!32312 IF NOT EXISTS */ is only interpreted as a comment, if you use MySQL Version < 3.23.12 (which was released in the previous millennium).

    For more details please check the "Comment syntax" in the MariaDB Knowledge Base.