MySQL 如何使用 mysqldump 仅导出 CREATE TABLE 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1842076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use mysqldump to export only the CREATE TABLE commands?
提问by FMc
I'm trying to use mysqldump
to export only the DB schema -- no data, no additional SQL comments, just the CREATE TABLE
commands. Here's what I've got so far:
我试图mysqldump
仅用于导出 DB 模式——没有数据,没有额外的 SQL 注释,只有CREATE TABLE
命令。这是我到目前为止所得到的:
mysqldump -h localhost -u root -p --no-data --compact some_db
It almost achieves what I want, but I'd like to eliminate the "character set" lines (those like the first 3 lines in the example output below). Is there a mysqldump
option to do that?
它几乎实现了我想要的,但我想消除“字符集”行(类似于下面示例输出中的前 3 行)。有没有mysqldump
办法做到这一点?
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bar_id` int(11) DEFAULT NULL,
`bazz` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=369348 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bar` (
...etc.
Here's my version info, in case that matters:
这是我的版本信息,以防万一:
mysqldump Ver 10.13 Distrib 5.1.34, for Win32 (ia32)
mysql Ver 14.14 Distrib 5.1.34, for Win32 (ia32)
采纳答案by AJ.
This uses grep as well, but it seems to work:
这也使用 grep ,但它似乎有效:
mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
I'm using:
我正在使用:
Ver 10.11 Distrib 5.0.51a, for debian-linux-gnu (x86_64)
Ver 10.11 Distrib 5.0.51a,用于 debian-linux-gnu (x86_64)
回答by Xavier John
Here is the command to dump the schema without the character set and AUTO_INCREMENT.
这是在没有字符集和 AUTO_INCREMENT 的情况下转储模式的命令。
mysqldump -h localhost -u root -p --no-data YOUR_DATABASE_HERE |egrep -v "(^SET|^/\*\!)" | sed 's/ AUTO_INCREMENT=[0-9]*\b//'
Here is the command to dump the schema without the character set, AUTO_INCREMENT and the comments
这是在没有字符集、AUTO_INCREMENT 和注释的情况下转储模式的命令
mysqldump -h localhost -u root -p --no-data --compact YOUR_DATABASE_HERE |egrep -v "(^SET|^/\*\!)" | sed 's/ AUTO_INCREMENT=[0-9]*\b//'
回答by Mathdoy
mysql> SHOW CREATE TABLE mytablename;
回答by AskApache Webmaster
mysqldump --compact --no-set-names --skip-opt --no-data DB | sed "/ SET /d"
回答by Liz Albin
Did you try the --skip-comments option mentioned in the manual? Does it help?
您是否尝试过手册中提到的 --skip-comments 选项?它有帮助吗?
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments
回答by nexneo
Use --skip-set-charset
option.
使用--skip-set-charset
选项。
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_set-charset
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_set-charset