我有一个mysqldump备份我的mysql数据库,包括我们所有的表,这是大约440兆。我只想从mysqldump中恢复其中一个表的内容。这可能吗?从理论上讲,我可以只删除重建我想要的表的部分,但我甚至不知道如何有效地编辑这种大小的文本文档。
当前回答
sed -n -e '/-- Table structure for table `my_table_name`/,/UNLOCK TABLES/p' database_file.sql > table_file.sql
这是一种比上面其他解决方案更好的解决方案,因为并非所有SQL转储都包含DROP TABLE语句。这将工作将所有类型的转储。
其他回答
Get a decent text editor like Notepad++ or Vim (if you're already proficient with it). Search for the table name and you should be able to highlight just the CREATE, ALTER, and INSERT commands for that table. It may be easier to navigate with your keyboard rather than a mouse. And I would make sure you're on a machine with plenty or RAM so that it will not have a problem loading the entire file at once. Once you've highlighted and copied the rows you need, it would be a good idea to back up just the copied part into it's own backup file and then import it into MySQL.
sed -n -e '/-- Table structure for table `my_table_name`/,/UNLOCK TABLES/p' database_file.sql > table_file.sql
这是一种比上面其他解决方案更好的解决方案,因为并非所有SQL转储都包含DROP TABLE语句。这将工作将所有类型的转储。
前面提到的“sed”解决方案很好,但不是100%安全
你可以使用INSERT命令,其中的数据包含: ... 创建表(无论)……mytable… 或者甚至是精确的字符串"CREATE TABLE ' mytable ';" 如果您正在存储DML命令,例如!
(如果表很大,你不需要手动检查)
我会验证所使用的转储版本的确切语法,并有一个更严格的模式搜索:
避免”。*”并使用“^”来确保我们从行开头开始。 我更喜欢抓住最初的DROP
总而言之,这对我来说更好:
sed -n -e '/^DROP TABLE IF EXISTS \`mytable\`;/,/^UNLOCK TABLES;/p' mysql.dump > mytable.dump
一种可能的处理方法是恢复到临时数据库,并从临时数据库中转储该表。然后使用新的脚本。
一个简单的解决方案是仅为希望单独恢复的表创建一个转储。你可以使用mysqldump命令来这样做,语法如下:
mysqldump -u [user] -p[password] [database] [table] > [output_file_name].sql
然后像往常一样导入它,它只会导入转储的表。
推荐文章
- SQL Server恢复错误-拒绝访问
- 警告用户/local/mysql/data目录不属于mysql用户
- 添加一个复合主键
- 无法添加或更新子行:外键约束失败
- 如何从本地机器mysqldump远程数据库
- 如何正确地创建复合主键- MYSQL
- 仅在Datetime列上按日期分组
- 在MySQL数据库中存储货币值的最佳数据类型
- 如何自定义'显示进程列表'在mysql?
- MySQL删除一些外键
- 用MySQL LEFT JOIN删除行
- MySQL - length() vs char_length()
- 使用PDO进行行计数
- 在Mac上安装MySQL后,使用ALTER USER语句重置MySQL root密码
- my.cnf文件在macOS上的位置