【问题标题】:how to mysqldump last 10 rows for EVERY TABLE in a database?如何mysqldump数据库中每个表的最后10行?
【发布时间】:2015-11-17 12:18:39
【问题描述】:
有很多关于如何从数据库中的表中 mysqldump 的最后 'n' 行的文章。例如从StackOverflow 和ServerFault 中的这些答案中找到的mysqldump --user=superman --password=batman --host=gothamcity.rds.com --where="1=1 ORDER BY id DESC LIMIT 10" DB_NAME TABLE_NAME ./path/to/dump/file.sql
但是,我如何告诉 mysqldump 导出数据库中每个表的最后 'n' 行
【问题讨论】:
标签:
mysql
database
export
mysqldump
database-backups
【解决方案1】:
除了solution of @Syed Rakid Al Hasan
如果我们像这样改变mysqldump的写入部分
> $tablename.sql
进入
>>jokersDB.sql
我们可以将所有偏移量为 10 的数据库数据转储到单个文件中
完整命令:
mysql --user=superman --password=batman --host=gothamcity.rds.com --port=3306 --database=jokersDB --execute="show tables" --silent --batch |同时读取表名;执行 mysqldump --user=superman --password=batman --host=gothamcity.rds.com --port=3306 --where="1=1 ORDER BY id DESC LIMIT 10"表 >>jokersDB.sql ;完成
jokersDB.sql 文件必须存在且为空
【解决方案2】:
这是我在终端所做的。这个想法基本上是获取所有表名的列表,然后将该表名列表通过管道传输到 bash 中的 while 循环中,其中每个表都单独转储到单独的转储文件(由表名命名)中。
mysql --user=superman --password=batman --host=gothamcity.rds.com --port=3306 --database=jokersDB --execute="show tables" --silent --batch | while read tablename ; do mysqldump --user=superman --password=batman --host=gothamcity.rds.com --port=3306 --where="1=1 ORDER BY id DESC LIMIT 10" jokersDB $tablename --add-drop-table > $tablename.sql ; done
成功了。唯一的问题是,它将每个表转储到它自己的单独 SQL 文件中 - 并非所有表都转储到单个文件中。但我想这些单独文件的内容也可以通过其他一些 bash 命令合并成一个文件。
【解决方案3】:
您可以对多个表使用--where 标志,只要它在语法上对每个表都有意义。因此,如果您的所有表都有一个名为 id 的代理 PK 列,那么您根本不需要命名任何表。只需使用 --all-databases 标志(或命名您想要的数据库)和带有指定 ORDER BY/LIMIT 的 --where 标志进行转储。
mysqldump --user=superman --password=batman --host=gothamcity.rds.com --where="1=1 ORDER BY id DESC LIMIT 10" --databases DB_NAME > /path/to/dump/文件.sql