简体   繁体   English

MySql转储输出条件注释? 执行dump as query以恢复数据库

[英]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 */; 当我使用mysqldump二进制文件转储mysql数据库时,我得到一个包含(以及其他行)的文件: 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. 我搜索了Google和MySql参考,但我找不到那些/ *!32312的意思,以及它们是如何工作的。 I can only guess that they are conditional comments. 我只能猜测它们是有条件的评论。 Like eg if build > 32312, execute "IF NOT EXIST"? 例如,如果build> 32312,执行“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. 我想知道这个的原因是因为我无法在C#客户端中使用转储内容作为1查询执行恢复。 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? 我是否真的必须启动mysql作为一个使用命令行参数来恢复转储的进程? 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... 因为我真的想在C#客户端中使用它作为SQL查询来完成转储的恢复,因为它比控制mysql二进制文件输出和退出代码等更方便...

They are indeed version-specific comments, as explained in Comment syntax . 它们确实是特定于版本的注释,如Comment语法中所述

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. 表示只有MySQL服务器版本为4.01.00或更高版本时才会执行此部分。

As stated by the original poster of the question : 正如问题的原始海报所述:


Using Query = Regex.Replace(QueryOrFilePath, @"(?:\\/\\*!\\d+\\s)|(?:\\*\\/)", @"", RegexOptions.Multiline); 使用Query = Regex.Replace(QueryOrFilePath, @"(?:\\/\\*!\\d+\\s)|(?:\\*\\/)", @"", RegexOptions.Multiline); Will remove all comment-and-version-a-like notations and only keeps the query inside the version specific comment. 将删除所有与注释和版本类似的注释,并仅将查询保留在特定于版本的注释中。

Useful if you are sure you are executing the query on a version compatible database. 如果您确定在版本兼容的数据库上执行查询,则非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM