0

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.

John
  • 1
  • 13
  • 98
  • 177

1 Answers1

2

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.

Georg Richter
  • 5,970
  • 2
  • 9
  • 15
  • I really had to read that - so in short MariaDB and MySQL support syntax *inside of comments*. That is akin to Internet Explorer's conditional comments from IE 5.5 to IE10. So what is happening here is that code tells MariaDB to not run that snippet if it doesn't match that version of greater which is odd because it lacks the periods and thus is wildly open to interpretation. So the code will run as `CREATE DATABASE IF NOT EXISTS` as long as it's greater than version 3.23.12. – John May 13 '21 at 20:58