1. 程式人生 > >select … into outfile 備份恢復(load data)以及mysqldump時間對比

select … into outfile 備份恢復(load data)以及mysqldump時間對比

IE sele 文本文 root 實現 default sin while con

select … into outfile ‘path‘ 備份

此種方式恢復速度非常快,比insert的插入速度要快的多,他跟有備份功能豐富的mysqldump不同的是,他只能備份表中的數據,並不能包含表的結構。如果備份完成之後,表被drop,是無法實現恢復操作的(除非有表結構)。

mysql> select * from t1 into outfile ‘/mydata/mysql/mysql3307/data/t1.sql‘;

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

mysql> show variables like ‘%secure%‘;

+--------------------------+-------+

| Variable_name | Value |

+--------------------------+-------+

| require_secure_transport | OFF |

| secure_auth | ON |

| secure_file_priv | NULL |

+--------------------------+-------+

3 rows in set (0.00 sec)

mysql> set secure_file_priv=‘/tmp‘;

ERROR 1238 (HY000): Variable ‘secure_file_priv‘ is a read only variable

mysql> set session secure_file_priv==‘/tmp‘;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘==‘/tmp‘‘ at line 1

mysql>

關閉MYSQL數據庫,設置secure_file_priv 目錄。

[root@mysql5 ~]# mysqladmin -S /tmp/mysql3307.sock -uroot -pmysql shutdown

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

[root@mysql5 ~]#

[root@mysql5 ~]#

[root@mysql5 ~]# ps -ef |grep mysql

root 3506 2071 0 01:24 pts/1 00:00:00 grep mysql

[root@mysql5 ~]#

vi /etc/my3307.cnf

[mysqld]裏面加入

secure_file_priv=/tmp

啟動後查看

mysql> show global variables like ‘%secu%‘;

+--------------------------+-------+

| Variable_name | Value |

+--------------------------+-------+

| require_secure_transport | OFF |

| secure_auth | ON |

| secure_file_priv | /tmp/ |

+--------------------------+-------+

3 rows in set (0.01 sec)

select * from t1 into outfile ‘/tmp/t1_onlydata.sql‘;

mysql> select * from t1 into outfile ‘/tmp/t1_onlydata.sql‘;

Query OK, 972864 rows affected (1.12 sec)

mysql> desc t1;

+-------+--------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+--------+------+-----+---------+-------+

| id | int(4) | YES | MUL | NULL | |

+-------+--------+------+-----+---------+-------+

1 row in set (0.00 sec)

技術分享圖片

全是文本文件數據。

select … into outfile ‘path‘ 恢復

清除t1表數據,並進行恢復

mysql> truncate table t1;

Query OK, 0 rows affected (0.04 sec)

mysql> desc t1;

+-------+--------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+--------+------+-----+---------+-------+

| id | int(4) | YES | MUL | NULL | |

+-------+--------+------+-----+---------+-------+

1 row in set (0.00 sec)

技術分享圖片

LOAD DATA恢復數據(只能恢復數據)

語法:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE ‘file_name‘

[REPLACE | IGNORE]

INTO TABLE tbl_name

[PARTITION (partition_name [, partition_name] ...)]

[CHARACTER SET charset_name]

[{FIELDS | COLUMNS}

[TERMINATED BY ‘string‘]

[[OPTIONALLY] ENCLOSED BY ‘char‘]

[ESCAPED BY ‘char‘]

]

[LINES

[STARTING BY ‘string‘]

[TERMINATED BY ‘string‘]

]

[IGNORE number {LINES | ROWS}]

[(col_name_or_user_var

[, col_name_or_user_var] ...)]

[SET col_name={expr | DEFAULT},

[, col_name={expr | DEFAULT}] ...]

技術分享圖片

load data INFILE ‘/tmp/t1_onlydata.sql‘ INTO TABLE test.t1;

技術分享圖片

查看表數據:

技術分享圖片

恢復成功。

load data 與insert的插入速度對比

於是,我猜想可以用mysqldump進行表結構的備份,用select …into outfile 備份數據,load data 恢復數據,測試兩者速度。

創建一張表。

技術分享圖片

delimiter //

create procedure per2()

begin

declare i int;

set i=1;

while i <= 1000000 do

insert into test.L values(i,‘aaaaa‘);

set i=i+1;

end while;

end

//

技術分享圖片

執行存儲過程:

call per2();

//

查看數據(實際我只插入僅14萬行數據)

技術分享圖片

mysqldump備份元數據文件

mysqldump -S /tmp/mysql3307.sock -uroot -pmysql --single-transaction --set-gtid-purged=OFF test L -d > /tmp/L_meta.sql

技術分享圖片

select … into outfile ‘path‘ 備份

select * from test.L into outfile ‘/tmp/20180525test_Ldata.sql‘

技術分享圖片

mysqldump備份整個表

mysqldump -S /tmp/mysql3307.sock -uroot -pmysql --single-transaction --set-gtid-purged=OFF test L > /tmp/L_table.sql

技術分享圖片

恢復時間對比

1、MYSQLdump先恢復表結構(或者直接建表)

先刪除,恢復表結構。

技術分享圖片

恢復表結構:

技術分享圖片

這核時間很快,就是建一張表的時間,不計算進去對整體時間沒有影響。

例如我建表:

技術分享圖片

建表時間0.03s。

2、load data恢復時間

load data INFILE ‘/tmp/20180525test_Ldata.sql‘ INTO TABLE test.L;

技術分享圖片

時間是1.59s。

3、MYSQLDUMP這種插入方式恢復

time mysql -S /tmp/mysql3307.sock -uroot -pmysql test < /tmp/L_table.sql

技術分享圖片

將近2s MYSQLDUMP恢復時間。

總結

元數據恢復+LOAD DATA時間一共

T1=0.03+1.59=1.62s

MYSQLDUMP恢復一共花了

T2=1.99s

對於14萬數據,load data比mysqldump快了近1/4,對於大數據量,應該快更多,在允許的情況下,可以利用元數據(或者表結構),配合LOAD DATA恢復單表。

select … into outfile 備份恢復(load data)以及mysqldump時間對比