這篇文章主要介紹MySQL數(shù)據(jù)庫備份怎么實(shí)現(xiàn),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
目前成都創(chuàng)新互聯(lián)公司已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、桃江網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
本文實(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)對(duì)象的定義存儲(chǔ)在數(shù)據(jù)字典中,這些表不會(huì)被備份。
要將存儲(chǔ)過程和事件也包含,請(qǐng)使用如下語句:
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)前庫的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
對(duì)以 t 結(jié)尾的所有數(shù)據(jù)庫進(jìn)行備份,多個(gè)數(shù)據(jù)庫用逗號(hào)分隔,數(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ù)庫用逗號(hào)分隔,數(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)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:mysql數(shù)據(jù)庫備份怎么實(shí)現(xiàn)
文章來源:http://chinadenli.net/article12/pioodc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)