溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql數(shù)據(jù)庫備份怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2021-05-27 11:49:48 來源:億速云 閱讀:136 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹mysql數(shù)據(jù)庫備份怎么實(shí)現(xiàn),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

本文實(shí)例講述了mysql 數(shù)據(jù)庫備份的多種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:

一、使用mysqldump進(jìn)行備份

1、完整備份所有數(shù)據(jù)庫

mysqldump -u root -p --all-databases > E:/all.sql

在mysql8之前,存儲(chǔ)過程和事件存儲(chǔ)在mysql.proc和mysql.event表中。

從mysql8開始,相應(yīng)對象的定義存儲(chǔ)在數(shù)據(jù)字典中,這些表不會(huì)被備份。

要將存儲(chǔ)過程和事件也包含,請使用如下語句:

mysqldump -u root -p --all-databases --routines --events > E:/all.sql

2、時(shí)間點(diǎn)恢復(fù)

要獲得時(shí)間點(diǎn)恢復(fù),應(yīng)該指定--single-transaction 和 --master-data

--single-transaction 在備份之前,會(huì)將事務(wù)隔離級別設(shè)為REPEATABLE READ模式,并執(zhí)行 START TRANSACTION 來提供一致的備份。

--master-data 將服務(wù)器的二進(jìn)制日志的位置輸出到 sql 文件。

mysqldump -u root -p --all-databases --routines --events --single-transaction --master-data > E:/all.sql

--master-data = 2表示在導(dǎo)出過程中,記錄當(dāng)前庫的binlog和pos點(diǎn),并在導(dǎo)出文件中注釋這一行。

--master-data = 1表示在導(dǎo)出過程中,記錄當(dāng)前庫的binlog和pos點(diǎn),并在導(dǎo)出文件中不注釋這一行。

3、在從庫導(dǎo)出時(shí),記錄主庫的二進(jìn)制日志位置

mysqldump -u root -p --all-databases --routines --events --single-transaction --dump-slave > E:/all.sql

--dump-slave = 2表示在導(dǎo)出過程中,記錄主庫的binlog和pos點(diǎn),并在導(dǎo)出文件中注釋這一行。

--dump-slave = 1表示在導(dǎo)出過程中,記錄主庫的binlog和pos點(diǎn),并在導(dǎo)出文件中不注釋這一行。

4、指定數(shù)據(jù)庫和表導(dǎo)出

mysqldump -u root -p --databases 數(shù)據(jù)庫 > E:/bak.sql
mysqldump -u root -p --databases 數(shù)據(jù)庫 --tables 數(shù)據(jù)表 > E:/bak.sql

5、忽略表

mysqldump -u root -p --databases 數(shù)據(jù)庫 --ignore-table=數(shù)據(jù)庫.數(shù)據(jù)表 > E:/bak.sql

6、指定行

mysqldump -u root -p --databases 數(shù)據(jù)庫 --tables 數(shù)據(jù)表 --where="條件" > E:/bak.sql

或者用limit限制結(jié)果集

mysqldump -u root -p --databases 數(shù)據(jù)庫 --tables 數(shù)據(jù)表 --where="條件 LIMIT 條數(shù)" > E:/bak.sql

7、導(dǎo)出遠(yuǎn)程服務(wù)器

mysqldump -u root -p -h 主機(jī)IP --all-databases --routines --events --triggers > E:/all.sql

8、用于與其他服務(wù)器合并數(shù)據(jù)的備份

mysqldump -u root -p --databases 數(shù)據(jù)庫 --skip-add-drop-table --replace > E:/bak.sql

--skip-add-drop-table: 不會(huì)將drop table語句寫入導(dǎo)出文件中。

--replace:將使用replace into語句而不是insert語句導(dǎo)出。

二、使用mysqlpump進(jìn)行備份

1、并行處理,通過指定線程數(shù)量加速備份過程

mysqlpump --default-parallelism=8 > E:/all.sql

2、也可以指定每個(gè)數(shù)據(jù)庫的線程數(shù)

mysqlpump -u root -p --parallel-schemas=4:數(shù)據(jù)庫 --default-parallelism=2 > E:/all.sql

3、排除或包含數(shù)據(jù)庫

mysqlpump -u root -p --include-databases=%t > E:/bak.sql

對以 t 結(jié)尾的所有數(shù)據(jù)庫進(jìn)行備份,多個(gè)數(shù)據(jù)庫用逗號分隔,數(shù)據(jù)庫名可以使用%或_通配符。

除此之外,還有類似--include-events,--include-routines,--include-tables,--include-triggers,--include-users等

mysqlpump -u root -p --exclude-databases=a% > E:/bak.sql

排除以 a 開頭的數(shù)據(jù)庫進(jìn)行備份,多個(gè)數(shù)據(jù)庫用逗號分隔,數(shù)據(jù)庫名可以使用%或_通配符。

除此之外,還有類似--exclude-events,--exclude-routines,--exclude-tables,--exclude-triggers,--exclude-users等

4、備份用戶

mysqlpump -u root -p --exclude-databases=% --users > E:/user.sql

可以通過--exclude-users來排除某些用戶

mysqlpump --exclude-databases=% --exclude-users=root --users > E:/user.sql

5、壓縮備份

通過使用--compress-output = lz4 或 --compress-output = zlib

mysqlpump -u root -p --compress-output=lz4 > E:/all.lz4
mysqlpump -u root -p --compress-output=zlib > E:/all.zlib

通過如下語句進(jìn)行解壓

lz4_decompress E:/all.lz4 all.sql
zlib_decompress E:/all.zlib all.sql

三、使用mydumper進(jìn)行備份

mydumper需要單獨(dú)安裝,官網(wǎng):https://github.com/maxbube/mydumper/releases

1、完全備份

mydumper -u root --password=密碼 --outputdir 導(dǎo)出路徑

2、備份單獨(dú)的表

mydumper -u root --password=密碼 -B 數(shù)據(jù)庫 -T 數(shù)據(jù)表 --triggers --events --routines --outputdir 導(dǎo)出路徑

3、使用正則表達(dá)式來備份特定數(shù)據(jù)庫

mydumper -u root --passoword=密碼 --regex '^(?!(mysql|test))' --outputdir 導(dǎo)出路徑

從備份中排除mysql和test數(shù)據(jù)庫。

4、備份大表

mydumper -u root --password=密碼 -B 數(shù)據(jù)庫 -T 數(shù)據(jù)表 --triggers --events --routines --rows=100000 -t 8 --trx-consistency-only --outputdir 導(dǎo)出路徑

--rows 表示將表分成多少行的塊

--trx-consistency-only 如果是innodb,將使鎖定最小化。

-t 指定線程數(shù)量

5、壓縮備份

mydumper -u root --password=密碼 -B 數(shù)據(jù)庫 -T 數(shù)據(jù)表 -t 8 --trx-consistency-only --compress --outputdir 導(dǎo)出路徑

6、僅備份數(shù)據(jù)

通過--no-schemas選項(xiàng)來跳過 schema 并且僅備份數(shù)據(jù)

mydumper -u root --password=密碼 -B 數(shù)據(jù)庫 -T 數(shù)據(jù)表 -t 8 --no-schemas --compress --trx-consistency-only --outputdir 導(dǎo)出路徑

四、使用普通文件進(jìn)行備份

可以通過直接復(fù)制數(shù)據(jù)目錄中的文件來進(jìn)行備份,需先關(guān)閉mysql,復(fù)制文件,然后啟動(dòng)mysql。

五、使用xtrabackup進(jìn)行備份

https://www.percona.com/downloads/XtraBackup/LATEST/

1、全量備份

xtrabackup --defaults-file=/etc/my.cnf --host=主機(jī)IP --user=用戶名 --password=密碼 --port=端口 --backup --parallel=3 --target-dir=備份目錄

--defaults-file 數(shù)據(jù)庫配置文件

--backup 執(zhí)行備份操作

--parallel 備份時(shí)并發(fā)的線程數(shù)

--target-dir 備份文件的目錄

2、增量備份

xtrabackup --defaults-file=/etc/my.cnf \
--host=主機(jī)IP \
--user=用戶名 \
--password=密碼 \
--port=3306 \
--backup \
--parallel=3 \
--target-dir=增量備份目錄 \
--incremental-basedir=全量備份目錄 \

增量備份是基于全量備份的,--incremental-basedir 指向全量備份目錄

以上是“mysql數(shù)據(jù)庫備份怎么實(shí)現(xiàn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI