操作巨大的 MySQL 转储文件

发布于 2024-07-08 03:07:29 字数 143 浏览 6 评论 0原文

获取单个表的数据、删除单个表或将整个转储文件分解为每个包含单独表的文件的最简单方法是什么? 我通常最终会进行大量的 vi 正则表达式修改,但我打赌有更简单的方法可以使用 awk/perl 等来完成这些事情。Google 结果的第一页带回了一堆无法工作的 perl 脚本。

What's the easiest way to get the data for a single table, delete a single table or break up the whole dump file into files each containing individual tables? I usually end up doing a lot of vi regex munging, but I bet there are easier ways to do these things with awk/perl, etc. The first page of Google results brings back a bunch of non-working perl scripts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

埋葬我深情 2024-07-15 03:07:30

此 shell 脚本将获取您想要的表并将它们传递给 splitted.sql

它能够理解正则表达式,因为我添加了 sed -r 选项。

MyDumpSplitter 还可以将转储拆分为单独的表转储。

This shell script will grab the tables you want and pass them to splitted.sql.

It’s capable of understanding regular expressions as I’ve added a sed -r option.

Also MyDumpSplitter can split the dump into individual table dumps.

深居我梦 2024-07-15 03:07:30

Maatkit seems quite appropriate for this with mk-parallel-dump and mk-parallel-restore.

指尖微凉心微凉 2024-07-15 03:07:30

我在这方面有点晚了,但如果它可以帮助任何人,我必须拆分一个巨大的 SQL 转储文件才能将数据导入到另一个 Mysql 服务器。
我最终做的是使用系统命令拆分转储文件。

split -l 1000 import.sql splited_file

上面将每 1000 行分割 sql 文件。

希望这对某人有帮助

I am a bit late on that one, but if it can help anyone, I had to split a huge SQL dump file in order to import the data to another Mysql server.
what I ended up doing was splitting the dump file using the system command.

split -l 1000 import.sql splited_file

The above will split the sql file every 1000 lines.

Hope this helps someone

玩套路吗 2024-07-15 03:07:29

当我需要从 sql 转储中提取单个表时,我使用 grep、head 和 tail 的组合。

例如:

grep -n "CREATE TABLE" dump.sql

然后,这将为您提供每个表的行号,因此,如果您的表位于第 200 行,而后一个表位于第 269 行,我会这样做:

head -n 268 dump.sql > tophalf.sql
tail -n 69 tophalf.sql > yourtable.sql

我想您可以扩展这些原则来构建一个可以拆分的脚本将整个事情分解为每个表一个文件。

有人想在这里尝试一下吗?

另一点可能有助于启动 bash 循环:

grep -n "CREATE TABLE " dump.sql  | tr ':`(' '  ' | awk '{print $1, $4}'

这为您提供了一个很好的行号和表名称列表,例如:

200 FooTable
269 BarTable

When I need to pull a single table from an sql dump, I use a combination of grep, head and tail.

Eg:

grep -n "CREATE TABLE" dump.sql

This then gives you the line numbers for each one, so if your table is on line 200 and the one after is on line 269, I do:

head -n 268 dump.sql > tophalf.sql
tail -n 69 tophalf.sql > yourtable.sql

I would imagine you could extend upon those principles to knock up a script that would split the whole thing down into one file per table.

Anyone want a go doing it here?

Another bit that might help start a bash loop going:

grep -n "CREATE TABLE " dump.sql  | tr ':`(' '  ' | awk '{print $1, $4}'

That gives you a nice list of line numbers and table names like:

200 FooTable
269 BarTable
夜光 2024-07-15 03:07:29

如果可以的话,可以使用mysqldump -T,这样可以省去很多麻烦。

来自 文档

--tab=路径,-T 路径

生成制表符分隔的数据文件。 对于每个转储的表,mysqldump
创建包含 CREATE TABLE 语句的 tbl_name.sql 文件
创建表,以及包含其表的 tbl_name.txt 文件
数据。 选项值是写入文件的目录。

默认情况下,.txt 数据文件使用制表符进行格式化
列值和每行末尾的换行符之间。 这
可以使用 --fields-xxx 显式指定格式和
--lines-termied-by 选项。

注意
仅当 mysqldump 在
与 mysqld 服务器相同的机器。 您必须具有 FILE 权限,
并且服务器必须有写入目录中文件的权限
您指定的。

Save yourself a lot of hassle and use mysqldump -T if you can.

From the documentation:

--tab=path, -T path

Produce tab-separated data files. For each dumped table, mysqldump
creates a tbl_name.sql file that contains the CREATE TABLE statement
that creates the table, and a tbl_name.txt file that contains its
data. The option value is the directory in which to write the files.

By default, the .txt data files are formatted using tab characters
between column values and a newline at the end of each line. The
format can be specified explicitly using the --fields-xxx and
--lines-terminated-by options.

Note
This option should be used only when mysqldump is run on the
same machine as the mysqld server. You must have the FILE privilege,
and the server must have permission to write files in the directory
that you specify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文