简体   繁体   English

我怎样才能摆脱 MySQL 转储中的这些评论?

[英]How can I get rid of these comments in a MySQL dump?

I'm trying to create a simple structure only dump of my database.我正在尝试创建一个简单的结构,仅转储我的数据库。 Using mysqldump gives me a result like:使用mysqldump给我这样的结果:

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

DROP TABLE IF EXISTS `foo`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;

No matter what I try, I just can't seem to get rid of those comments.无论我尝试什么,我似乎都无法摆脱这些评论。

I'm currently using: mysqldump -p -d --add-drop-table --skip-tz-utc --skip-set-charset -h 127.0.0.1 -u foo bar --result-file=dumpfile.sql我目前正在使用: mysqldump -p -d --add-drop-table --skip-tz-utc --skip-set-charset -h 127.0.0.1 -u foo bar --result-file=dumpfile.sql

Edit: I do however wish to retain other comments, such as -- MySQL dump 10.13 Distrib 5.1.41, for Win32 (ia32)编辑:但是我确实希望保留其他评论,例如-- MySQL dump 10.13 Distrib 5.1.41, for Win32 (ia32)

WHOA! 哇! These aren't really comments even though they look that way. 这些并不是真正的评论,即使它们看起来那样。 They are conditional-execution tokens. 它们是条件执行令牌。

Take this line: 走这条线:

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;

If the version of mySQL is 4.00.14 or higher , then the MySQL server will run this statement. 如果mySQL的版本是4.00.14 或更高版本 ,则MySQL服务器将运行此语句。

This magic comment syntax is documented in the Comment Syntax section of the manual. 此魔术注释语法记录在本手册的“ 注释语法”部分中。

You probably don't want to get rid of this stuff. 你可能不想摆脱这些东西。

I know this is an ancient question, but here is an answer at least. 我知道这是一个古老的问题,但至少这是一个答案。 I also couldn't find a flag in mysqldump to remove the conditional comments, or indeed a better option to set a minimum mysql version for these comments to appear. 我也找不到mysqldump中的标志来删除条件注释,或者确实是为这些注释设置最小mysql版本的更好选项。 If you just want to nuke them all, you can do so using grep or sed (sed leaves blank lines, grep does not): 如果你只想把它们全部核对,你可以使用grep或sed(sed留空行,grep没有):

mysqldump ... | grep -v '^\/\*![0-9]\{5\}.*\/;$'
mysqldump ... | sed -e 's/^\/\*![0-9]\{5\}.*\/;$//g'

To answer my own wish of conditionally removing comments dependent on mysql version, use one of these (removes any comments for anything < mysql5): 要回答我自己希望有条件地删除依赖于mysql版本的注释,请使用其中一个(删除任何注释<mysql5):

mysqldump ... | grep -v '^\/\*![0-4][0-9]\{4\}.*\/;$'
mysqldump ... | sed -e 's/^\/\*![0-4][0-9]\{4\}.*\/;$//g'

Try --skip-comments ? 试试--skip-comments

Thanks 谢谢

Edit: 编辑:

I see .. Try this 我明白了..试试吧

--skip-add-drop-table --skip-add-locks --skip-disable-keys --skip-set-charset

Play around to remove some of the options till you get the desired result, basically this is same as --compact without --skip-comments 在你得到想要的结果之前,可以去除一些选项,基本上这与--compact没有--skip-comments

--skip-comments removes the comments relating to version and stuff .. --skip-comments删除与版本和内容相关的注释。

Have you tried the shortcut option --compact ? 你试过快捷方式选项--compact吗?

Information here . 这里的信息

Technically the lines you are trying to get rid of are not comments. 从技术上讲,你试图摆脱的线路不是评论。 They temporarily modify some variables at the beginning, and then reset them to the previous value at the end. 它们在开始时临时修改一些变量,然后在结束时将它们重置为先前的值。

They're not very useful (but they're also harmless) in your case, since you're using --no-data, but I thought it worth mentioning that the lines do serve a purpose, and are not just comments. 在你的情况下,它们不是很有用(但它们也是无害的),因为你正在使用--no-data,但我认为值得一提的是这些行确实有用,而不仅仅是注释。

Those are not comments, the execution of that part of the scripts depends on the version of your mysql. 那些不是注释,脚本部分的执行取决于你的mysql的版本。

You can delete "the comment part", like 您可以删除“评论部分”,例如

/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */

to

SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0

making the script more "comfortable" for reading. 使脚本更加“舒适”阅读。

If you try to run a "comfortable" script in a version newer than the specified in the "comment", you will get an error. 如果您尝试在比“注释”中指定的版本更新的版本中运行“舒适”脚本,则会出现错误。

If you've stumbled up on this answer trying to include your structure.sql file in git/github, you can strip out auto-increment with the following code right after you rake db:structure:dump 如果你偶然发现这个答案试图在git / github中包含你的structure.sql文件,你可以在你运行db:structure:dump后立即用以下代码去掉自动增量

# Remove beginning auto increments to prevent merge conflicts
filename = 'db/structure.sql'
File.atomic_write(filename) do |output|
  File.open(filename, 'rb').each do |input|
    output.write(input.gsub(/\s+AUTO_INCREMENT=\d+\s+/, ' '))
  end
end

可能在其上运行正则表达式以删除包含40014或40111等的行。

Since you are on Windows, if no-one finds a better solution then you could use a Python script instead: 由于您使用的是Windows,如果没有人找到更好的解决方案,那么您可以使用Python脚本代替:

import re, sys
sql = sys.stdin.read()
regex = re.compile(r'/\*![^\n]* \*/;\n', re.M)
print regex.sub('', sql)

Usage from command line: 从命令行使用:

python program.py < your.sql > output.sql

It removes all lines like this: 它会删除所有这样的行:

/*!....... */;

I made this script to normalize the dump, including removing conditional comments: https://github.com/luissquall/dbdump . 我制作了这个脚本来规范化转储,包括删除条件注释: https//github.com/luissquall/dbdump

You just have to: 你只需要:

npm install -g @luissquall/dbdump

# Redirect output to a file
dbdump -u user -p -d database > struct.sql

It's really important to keep the conditional-execution comments. 保持条件执行注释非常重要。 But if you absolutely know that the MySQL version that will load the dump is greater or equal to the one that creates it, you can remove the "comment" part with this: 但是如果您完全知道将加载转储的MySQL版本大于或等于创建它的版本,您可以删除“注释”部分:

sed -r  s'#/\*![0-9]{5} ?([^*]*)\*/#\1#'g

It will convert lines such as 它会转换如

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;

to

SET SQL_MODE=@OLD_SQL_MODE ;

Because this line must run on any MySQL >= 4.1.1 因为这一行必须在任何MySQL> = 4.1.1上运行

Note that this will not remove multi-line conditional-execution comments, such as when dumping a trigger. 请注意,这不会删除多行条件执行注释,例如转储触发器时。

Since it's impossible to predict the future, it's better to store the dump with the comments on, and only remove them when you want to visualize it. 由于无法预测未来,因此最好将转储与注释一起存储,并且只有在想要显示它时才将其删除。

mysqldump ... > dump.sql
cat dump.sql | sed -E  s'#/\*![0-9]{5} ?([^*]*)\*/#\1#'g > dump.no-comments.sql

I used the following multiline Perl regexp to remove these lines from my dump file:我使用以下多行 Perl 正则表达式从我的转储文件中删除这些行:

perl -0pie 's/\/\*\!\d+(.*?)\*\//\1/gms' dump.sql

I found other sed solutions not sufficient in cases where my trigger procedures spanned multiple lines.我发现其他sed解决方案在我的触发过程跨越多行的情况下不够用。

As @Ollie and a few others pointed out, these are are conditional-execution tokens written in comment style but served a purpose. 正如@Ollie和其他一些人指出的那样,这些是以评论风格编写的条件执行令牌,但却有用。 Without them, you may run into issues of recreating tables with heavily enforced foreign key constraint. 如果没有它们,您可能会遇到使用严格强制的外键约束重新创建表的问题。 For instance, table A has FK for table B and thus table A cannot be created until table B do and so on so forth. 例如,表A对于表B具有FK,因此在表B执行之前不能创建表A,依此类推。 Without disabling the key checks, you may never be able to recreate them depending how your table order is fined. 在不禁用密钥检查的情况下,您可能永远无法根据表顺序的细化重新创建它们。

Use --dump-date=FALSE 使用--dump-date=FALSE

Does exactly what OP asks for. 正是OP要求的。 (not exactly, I see) (不完全是,我明白了)

Source: mysqldump option summary 来源: mysqldump选项摘要

Edit: Just after a minute I realized, this is what me was looking for not the OP, but leaving here... in hope someone can use it: This date line which ruins source control, because it always a change... 编辑:一分钟后我才意识到,这就是在寻找的不是OP,而是离开这里...希望有人可以使用它:这个日期线会破坏源代码控制,因为它总是一个变化......

I dont know if it is what are you looking for, i simply wanted to get rid of all the mysql comments stuff to be able to use a syntax highlighter, i used a simple regex and replace all with the following "/\\*![0-9]{5}|\\*/" and voila! 我不知道它是不是你在寻找什么,我只是想摆脱所有的mysql评论的东西,以便能够使用语法荧光笔,我使用一个简单的正则表达式并用以下“/ \\ *!”替换所有0-9] {5} | \\ * /“瞧! nice colors in the code ;) 代码中的漂亮颜色;)

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

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