9、單機運行環境搭建之 --CentOS-6.4下mysqldump 備份與還原資料庫

來源:互聯網
上載者:User

 建立一個測試用的資料庫

mysql -u root -p
#設定一下許可權,方便在備份時使用
set password for 'root'@'localhost' =password('dsideal');

flush privileges;

#刪除舊的測試資料庫
drop database test;

#建立新的測試資料庫create database test;use test;create table test (id int); insert into test values(1);insert into test values(2);insert into test values(3);insert into test values(4);insert into test values(5);select * from test; 

 然後備份test的整個資料庫

#使用下面的 vi /usr/local/backup_test.sh

內容如下:

#!/bin/sh

date_str=`date +%Y%m%d`cd /data2/backupmysqldump -h localhost -u root --password=dsideal -R -E -e \ --max_allowed_packet=1048576 --net_buffer_length=16384 test\ | gzip > /tmp/data/dsideal_test_$date_str.sql.gz

echo "DataBase Backup Success!"

參數說明:

max_allowed_packet 和 net_buffer_length 這兩個參數起著決定性作用,速度差別幾百上千倍我這裡的配置是:--max_allowed_packet=25165824 --net_buffer_length=16384其原理是合并多條資料成為一個 SQL 插入語句。

匯出時候注意點:
-e
 使用包括幾個VALUES列表的多行INSERT文法;
--max_allowed_packet=XXX 用戶端/伺服器之間通訊的緩衝區的最大大小;
--net_buffer_length=XXX TCP/IP和通訊端通訊緩衝區大小,建立長度達net_buffer_length的行。
注意:max_allowed_packet和net_buffer_length不能比目標資料庫的設定數值 大,否則可能出錯。
首先確定目標庫的參數值
mysql>show variables like 'max_allowed_packet';
mysql>show variables like 'net_buffer_length';
根據參數值書寫mysqldump命令,如:
mysql>mysqldump -uroot -psupidea jb51.net goodclassification -e --max_allowed_packet=1048576 --net_buffer_length=16384 >www.jb51.net.sql

 

#運行

sh /usr/local/backup_test.sh

還原:

從壓縮檔直接恢複:

gzip < dsideal_test_20130405.sql.gz | mysqldump -u root -p test

 

 注意:這裡的TEST資料庫必須是存在的,否則會還原不了,就是說必須要保證這個資料庫存在,新的機器就必須手工建立一下。

 #修改檔案屬性,使其可執行

chmod +x /usr/local/backup_test.sh

 #修改/etc/crontab

crontab -e
在下面添加

01 3 * * * root /usr/local/backup_test.sh

#表示每天3點鐘執行備份

#如果需要異機備份

http://www.cnblogs.com/dwzjs/archive/2011/04/25/2027687.html

Crontab 定時任務管理

crontab -e01 03 * * * /var/proftpd_data/backup/bin/mysqlFullBackup.sh10 04 * * * /var/proftpd_data/backup/bin/ftpDownload.sh前五個欄位的整數取值範圍及意義是:0~59 表示分1~23 表示小時1~31 表示日1~12 表示月份0~6 表示星期(其中0表示星期日)

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.