mysql 可以从大型 mysqldump 中恢复单个表吗?

Dav*_*uer 6 mysql mysqldump

从大型(~5GB)数据库 mysqldump 文件恢复单个表的最佳方法是什么?


更新: 我找到了使用命令行工具解析表的解决方案(在下面发布),但是有没有办法用 mysqlimport 来做到这一点?

Dav*_*uer 10

我找到了两种解决方案,一种使用

grep -n "Table Structure" mydump.sql
# identify the first and last line numbers (n1 and n2) of desired table
sed -n n1,n2p mydump.sql > mytable.sql # (e.g. sed -n 48,112p)
Run Code Online (Sandbox Code Playgroud)

一个使用 awk

awk '/Table Structure for table .table1./, /Table structure for table .cultivars./{print}' mydump.sql > mytable.sql
Run Code Online (Sandbox Code Playgroud)


Abe*_*Abe 6

这种方法并没有解决必须导入大型 mysqldumpfile 的问题,但是在 Mysql 中,您可以执行以下操作:

  1. 恢复整个转储

     mysql fakedb < mydump.sql
    
    Run Code Online (Sandbox Code Playgroud)
  2. 删除当前表的内容

     mysql
     delete from production.target_table;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 从备份表插入

    insert into production.target_table select * from fakedb.targettable;
    
    Run Code Online (Sandbox Code Playgroud)


小智 5

我已经创建了 linux 和 windows 脚本来从转储文件中恢复特定的表:

linux(bash脚本):

#!/bin/bash

# Where to restore
db_host='localhost'
db_name='adhoctuts'
db_user='root'
db_pass='Adhoctuts2018#'

dump_file='/root/scripts/dump_ignore.sql'

# Associative table list array as source_table=>destination_table pairs
declare -A tbl_list=( ["tbl1"]="restored_tbl1" ["tbl2"]="restored_tbl2")

for tbl in "${!tbl_list[@]}"
do
    echo "Restore $tbl to ${tbl_list[$tbl]}"
    # extract the content between drop table and Table structure for, also replace the table name
    sed -n -e '/DROP TABLE IF EXISTS `'$tbl'`/,/\/*!40000 ALTER TABLE `'$tbl'` ENABLE KEYS \*\/;/p' $dump_file > tbl.sql
    sed -i 's/`'$tbl'`/`'${tbl_list[$tbl]}'`/g' tbl.sql
    mysql -h $db_host -u $db_user -p"$db_pass" $db_name < tbl.sql
    rm -f tbl.sql
done
Run Code Online (Sandbox Code Playgroud)

Windows 脚本(bat):

%= Define the database and root authorization details =% 
@ECHO OFF
SETLOCAL EnableDelayedExpansion

set db_host=192.168.70.138
set db_name=adhoctuts
set db_user=adhoctuts
set db_pass=Adhoctuts2018#

set dump_file=dump_ignore.sql

set tbl_cnt=2
set source_table[1]=tbl1
set destination_table[1]=restored_tbl1
set source_table[2]=tbl2
set destination_table[2]=restored_tbl2

set i=1
:loop   
    set src=!source_table[%i%]!
    set dest=!destination_table[%i%]!
    for /f "tokens=1 delims=[]" %%a in ('find /n "DROP TABLE IF EXISTS `%src%`"^<"%dump_file%"') do set /a start=%%a
    for /f "tokens=1 delims=[]" %%a in ('find /n "ALTER TABLE `%src%` ENABLE KEYS"^<"%dump_file%"') do set /a end=%%a
    (
    for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%dump_file%"') do (

    set "line=%%b "
     IF %%a geq %start% IF %%a leq %end% ECHO( !line:%src%=%dest%!
     )
    )>"tbl.sql"

    mysql -h %db_host% -u %db_user% -p"%db_pass%" %db_name% < "tbl.sql"
    del /f "tbl.sql"
    if %i% equ %tbl_cnt% goto :eof
    set /a i=%i%+1
goto loop
Run Code Online (Sandbox Code Playgroud)

您可以在此处定义需要恢复的表以及要恢复的名称。这是更通用的解决方案。

我还为 MySQL 选择性/特殊任务创建了单独的教程。如果需要,您可以检查:

https://youtu.be/8fWQbtIISdc

https://adhoctuts.com/mysql-selective-exceptional-permissions-and-backup-restore/