mysql 數(shù)據(jù)庫(kù)備份的多種實(shí)現(xiàn)方式總結(jié)
本文實(shí)例講述了mysql 數(shù)據(jù)庫(kù)備份的多種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
一、使用mysqldump進(jìn)行備份
1、完整備份所有數(shù)據(jù)庫(kù)
mysqldump -u root -p --all-databases > E:/all.sql
在mysql8之前,存儲(chǔ)過程和事件存儲(chǔ)在mysql.proc和mysql.event表中。
從mysql8開始,相應(yīng)對(duì)象的定義存儲(chǔ)在數(shù)據(jù)字典中,這些表不會(huì)被備份。
要將存儲(chǔ)過程和事件也包含,請(qǐng)使用如下語(yǔ)句:
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ù)隔離級(jí)別設(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)前庫(kù)的binlog和pos點(diǎn),并在導(dǎo)出文件中注釋這一行。
--master-data = 1表示在導(dǎo)出過程中,記錄當(dāng)前庫(kù)的binlog和pos點(diǎn),并在導(dǎo)出文件中不注釋這一行。
3、在從庫(kù)導(dǎo)出時(shí),記錄主庫(kù)的二進(jìn)制日志位置
mysqldump -u root -p --all-databases --routines --events --single-transaction --dump-slave > E:/all.sql
--dump-slave = 2表示在導(dǎo)出過程中,記錄主庫(kù)的binlog和pos點(diǎn),并在導(dǎo)出文件中注釋這一行。
--dump-slave = 1表示在導(dǎo)出過程中,記錄主庫(kù)的binlog和pos點(diǎn),并在導(dǎo)出文件中不注釋這一行。
4、指定數(shù)據(jù)庫(kù)和表導(dǎo)出
mysqldump -u root -p --databases 數(shù)據(jù)庫(kù) > E:/bak.sql mysqldump -u root -p --databases 數(shù)據(jù)庫(kù) --tables 數(shù)據(jù)表 > E:/bak.sql
5、忽略表
mysqldump -u root -p --databases 數(shù)據(jù)庫(kù) --ignore-table=數(shù)據(jù)庫(kù).數(shù)據(jù)表 > E:/bak.sql
6、指定行
mysqldump -u root -p --databases 數(shù)據(jù)庫(kù) --tables 數(shù)據(jù)表 --where="條件" > E:/bak.sql
或者用limit限制結(jié)果集
mysqldump -u root -p --databases 數(shù)據(jù)庫(kù) --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ù)庫(kù) --skip-add-drop-table --replace > E:/bak.sql
--skip-add-drop-table: 不會(huì)將drop table語(yǔ)句寫入導(dǎo)出文件中。
--replace:將使用replace into語(yǔ)句而不是insert語(yǔ)句導(dǎo)出。
二、使用mysqlpump進(jìn)行備份
1、并行處理,通過指定線程數(shù)量加速備份過程
mysqlpump --default-parallelism=8 > E:/all.sql
2、也可以指定每個(gè)數(shù)據(jù)庫(kù)的線程數(shù)
mysqlpump -u root -p --parallel-schemas=4:數(shù)據(jù)庫(kù) --default-parallelism=2 > E:/all.sql
3、排除或包含數(shù)據(jù)庫(kù)
mysqlpump -u root -p --include-databases=%t > E:/bak.sql
對(duì)以 t 結(jié)尾的所有數(shù)據(jù)庫(kù)進(jìn)行備份,多個(gè)數(shù)據(jù)庫(kù)用逗號(hào)分隔,數(shù)據(jù)庫(kù)名可以使用%或_通配符。
除此之外,還有類似--include-events,--include-routines,--include-tables,--include-triggers,--include-users等
mysqlpump -u root -p --exclude-databases=a% > E:/bak.sql
排除以 a 開頭的數(shù)據(jù)庫(kù)進(jìn)行備份,多個(gè)數(shù)據(jù)庫(kù)用逗號(hào)分隔,數(shù)據(jù)庫(kù)名可以使用%或_通配符。
除此之外,還有類似--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
通過如下語(yǔ)句進(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ù)庫(kù) -T 數(shù)據(jù)表 --triggers --events --routines --outputdir 導(dǎo)出路徑
3、使用正則表達(dá)式來備份特定數(shù)據(jù)庫(kù)
mydumper -u root --passoword=密碼 --regex '^(?!(mysql|test))' --outputdir 導(dǎo)出路徑
從備份中排除mysql和test數(shù)據(jù)庫(kù)。
4、備份大表
mydumper -u root --password=密碼 -B 數(shù)據(jù)庫(kù) -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ù)庫(kù) -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ù)庫(kù) -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ù)庫(kù)配置文件
--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 指向全量備份目錄
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
相關(guān)文章
mysql時(shí)間格式和Java時(shí)間格式的對(duì)應(yīng)方式
這篇文章主要介紹了mysql時(shí)間格式和Java時(shí)間格式的對(duì)應(yīng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
MySQL數(shù)據(jù)延遲跳動(dòng)的問題解決
這篇文章主要介紹了MySQL數(shù)據(jù)延遲跳動(dòng)的問題如何解決,幫助大家更好的理解和學(xué)習(xí)MySQL,感興趣的朋友可以了解下2020-08-08
mysql數(shù)據(jù)庫(kù)中各種鎖歸納總結(jié)
相對(duì)于其他的數(shù)據(jù)庫(kù)而言,MySQL的鎖機(jī)制比較簡(jiǎn)單,最顯著的特點(diǎn)就是不同的存儲(chǔ)引擎支持不同的鎖機(jī)制,這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫(kù)中各種鎖歸納總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-08-08
rpm -ivh方式安裝mysql并修改數(shù)據(jù)存儲(chǔ)位置的實(shí)現(xiàn)
在Linux環(huán)境下進(jìn)行MySQL的安裝可以使用不同的方式,但在本文中我們將關(guān)注一種特定的方式,即通過RPM包的方式進(jìn)行安裝,本文主要介紹了rpm -ivh方式安裝mysql并修改數(shù)據(jù)存儲(chǔ)位置的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09
IDEA鏈接MySQL報(bào)錯(cuò)08001和連接成功后不顯示表的問題及解決方法
這篇文章主要介紹了IDEA鏈接MySQL報(bào)錯(cuò)08001和連接成功后不顯示表的問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
探討:MySQL中如何查詢當(dāng)前正在運(yùn)行的SQL語(yǔ)句
本篇文章是對(duì)在MySQL中如何查詢當(dāng)前正在運(yùn)行的SQL語(yǔ)句進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
MySQL快速插入大量數(shù)據(jù)的解決方案和代碼示例
在這篇博客中,我們將深入探討如何高效插入大量數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù),無論你是數(shù)據(jù)庫(kù)新手還是經(jīng)驗(yàn)豐富的開發(fā)者,這篇文章都將為你提供實(shí)用的解決方案和代碼示例,幫助你解決插入3萬條數(shù)據(jù)需要20多秒的問題,需要的朋友可以參考下2024-08-08
詳解JDBC數(shù)據(jù)庫(kù)鏈接及相關(guān)方法的封裝
這篇文章主要介紹了詳解JDBC數(shù)據(jù)庫(kù)鏈接及相關(guān)方法的封裝的相關(guān)資料,下面是封裝的具體類,用到了泛型和反射,希望能幫助到大家,需要的朋友可以參考下2017-08-08
MySQL中interactive_timeout和wait_timeout的區(qū)別
這篇文章主要介紹了MySQL中interactive_timeout和wait_timeout的區(qū)別,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10

