Search code examples
mysqlcommentsrestoredump

MySql Dump output conditional comments? Execute dump as query to restore database


Note: Actually two questions...

When I dump a mysql database using the mysqldump binary I get a file that contains (among other lines) this: CREATE DATABASE /*!32312 IF NOT EXISTS*/MyDatabase/*!40100 DEFAULT CHARACTER SET utf8 */;

I have searched on Google and MySql reference but I cant find what those /*!32312 mean, and how they work. I can only guess that they are conditional comments. Like e.g. if build > 32312, execute "IF NOT EXIST"?

Can anyone shed some light on this?

The reason my I want to know this, is because I am failing to execute a restore using the contents of the dump as 1 query in a C# client. Using this code:

            MySqlConnection msc = default(MySqlConnection);
            MySqlCommand cmd = default(MySqlCommand);
            MySqlTransaction mst = default(MySqlTransaction);
            try
            {
                //Create a connection to the database
                msc = new MySqlConnection(ConnectionString);
                msc.Open();

                //Creata a MySql Transaction
                mst = msc.BeginTransaction();

                cmd = msc.CreateCommand();
                cmd.Transaction = mst;
                cmd.CommandText = ContentsOfMySqlDumpSql;

                cmd.ExecuteNonQuery();

                mst.Commit();
            }

Do I really have to start mysql as a process with commandline arguments to restore the dump? As I really want to accomplish the restore of the dump using it as a SQL Query in the C# client, as its more convenient than controlling the mysql binaries output and exit code and such...


Solution

  • They are indeed version-specific comments, as explained in Comment syntax.

    These comments allow to execute optional parts of statements only if the server supports them.

    /*!40100 DEFAULT CHARACTER SET utf8 */;
    

    means this part will be executed only if the version of the MySQL server is 4.01.00 or higher.