mysql 三種災備方式
-
mkdir /home/mybinlog
-
chown mysql:mysql /home/mybinlog
-
vim /etc/my.cnf
-
log-bin=/home/mybinlog/mysql-bin ##二進位制日誌目錄及檔名字首
-
innodb_file_per_table = 1 ##啟用InnoDB表每表一檔案,預設所有庫使用一個表空間
-
然後啟動mysql
-
mysql <~/Desktop/hellodb.sql
-
use hellodb
-
create table study (id tinyint auto_increment primary key,name char(10));
-
insert into study (name) values ('redhat'),('php'),('asp'),('CCNA'),('MSIC'),('CCNP'),('HCSE');
-
mkdir /home/mysqlbackup \\用於存放備份檔案
-
mkdir /home/mysqlbackup/mylogbin \\用於存放備份的二進位制日誌
-
mkdir /home/mysqlbackup/mylogstatus \\用於存放二進位制日誌起始位置
-
chown -R mysql:mysql /home/mysqlbackup
-
mysqldump -uroot -predhat --single-transaction --master-data=2 --all-databases --routines --events > /home/mysqlbackup/mydb_all_`date +%F`.sql
-
flush logs
-
mysql -uroot -predhat -e 'show master status' > /home/mysqlbackup/mylogstatus/`date +%F-%H`.txt
-
--databases \\指定資料庫名
-
--lock-all-tables \\為所有表加讀鎖,如果使用的是非事務型表,推薦使用這個選項備份。
-
--master-data=2 \\在備份檔案中記錄當前二進位制日誌的位置,並且為註釋的,1是不註釋掉在主從複製中才有意義
-
--single-transaction \\(使得轉儲的資料為事務開始前的資料)啟動一個大事物,併為每張表建立快照,基於此項能實現熱備InnoDB表,由此,不需要同時使用--lock-all-tables
-
--routines \\同時,備份儲存過程和儲存函式
-
--events \\同時,備份事件排程器程式碼
-
--all-databases \\備份伺服器上的所有庫
-
create table system(id tinyint auto_increment primary key,Name char(10));
-
insert into system (Name) values ('windows'),('Centos'),('fedora');
-
cat /home/mysqlbackup/mylogstatus/2014-06-30-15.txt
-
show master status;
-
# mysqlbinlog --start-position=107 --stop-position=504 /home/mybinlog/mysql-bin.000007 >/home/mysqlbackup/incremental-`date +%F-%H`.sql
-
mysql -uroot -predhat -e 'show master status' > /home/mysqlbackup/mylogstatus/`date +%F-%H`.txt
-
insert into system (Name) values ('rlel'),('suselinux'),('VMX');
-
create table firm(id tinyint auto_increment primary key ,name char(10))
-
insert into firm (name) values ('baidu'),('huabang'),('sohu');
-
service mysqld stop
-
rm -rf /mydata/data/*
-
./scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
-
# service mysqld start
-
mysql < /home/mysqlbackup/mydb_all_2014-06-30.sql
-
mysql < /home/mysqlbackup/incremental-2014-06-30-16.sql
-
cat /home/mysqlbackup/mylogstatus/2014-06-30-16.txt
-
mysqlbinlog --start-position=504 /home/mybinlog/mysql-bin.000007 >/home/mysqlbackup/huifu-2014-06-30-17.sql
-
mysql -uroot -predhat < /home/mysqlbackup/huifu-2014-06-30-17.sql
-
flush tables with read lock;
-
flush logs;
-
mysql -uroot -p -e 'show master status'>/var/mybackup/logstatus/`date +%F-+%H`.txt
-
lvcreate -L 200M -n mydata-snap -p r -s /dev/mapper/vg0-lv0
-
unlock tables;
-
mkdir /mysnap
-
mount /dev/vg0/mydata-snap /mysnap/
-
cp /mysnap/* /var/mybackup/2014-06-30/ -a
-
umount /mysnap/
-
lvremove /dev/vg0/mydata-snap
-
這樣備份就完成了
-
create table uu(id int primary key,yy int);
-
insert into uu (id,yy) values (123,111),(223,121);
-
/etc/init.d/mysqld stop
-
rm -rf /mydata/data/*
-
cp /var/mybackup/2014-06-30/* /mydata/data/ -a
-
service mysqld start
-
cat /var/mybackup/logstatus/2014-06-30-+22.txt
-
mysqlbinlog --start-position=107 /var/mybinlog/mysql-bin.000002 >/var/mybackup/incremental-2014-06-30-23.sql
-
mysql -uroot -predhat < /var/mybackup/incremental-2014-06-30-23.sql
-
yum install percona-xtrabackup-2.1.4-656.rhel6.i686.rpm
-
innobackupex --user=root --password=redhat /var/mybackup/
-
service mysqld stop
-
rm -rf /mydata/data/*
-
innobackupex --apply-log /var/mybackup/2014-07-01_13-49-38/
-
innobackupex --copy-back /var/mybackup/2014-07-01_13-49-38/
-
chown -R mysql.mysql /mydata/
-
service mysqld start
-
innobackupex --user=root --password=redhat /var/mybackup/
-
create database redhat;
-
use redhat
-
create table study(id tinyint auto_increment primary key,students char(10),course char(10));
-
insert into study (students,course) values ('tuchao','linux'),('fangchao','bashshell'),('tyz','lvm'),('yujiaqing','auto china');
-
select * from study;
-
innobackupex --user=root --password=redhat --incremental /var/mybackup/incrbak/ --incremental-basedir=/var/mybackup/2014-07-01_14-41-41/
-
--incremental-basedir //這裡要指定上一次最近的備份為基準,進行增量備份。
-
cat incrbak/2014-07-01_14-51-28/xtrabackup_checkpoints
-
insert into study (students,course) values ('wujihe','Java'),('qiulin','Ios kaifa');
-
innobackupex --user=root --password=redhat --incremental /var/mybackup/incrbak/ --incremental-basedir=/var/mybackup/incrbak/2014-07-01_14-51-28/
-
cat incrbak/2014-07-01_15-11-57/xtrabackup_checkpoints
-
create table test(id tinyint auto_increment primary key ,name char(10));
-
insert into test (name) values ('aaa'),('bbb'),('redhat'),('hello world'),('centos');
-
select * from test;
-
service mysqld stop
-
rm -rf /mydata/data/*
-
innobackupex --apply-log --redo-only /var/mybackup/2014-07-01_14-41-41/
-
--redo-only //只做提交處理,不做回滾。
-
innobackupex --apply-log --redo-only /var/mybackup/2014-07-01_14-41-41/ --incremental-dir=/var/mybackup/incrbak/2014-07-01_14-51-28/
-
innobackupex --apply-log --redo-only /var/mybackup/2014-07-01_14-41-41/ --incremental-dir=/var/mybackup/incrbak/2014-07-01_15-11-57/
-
cd /var/mybackup/2014-07-01_14-41-41/
-
cat xtrabackup_checkpoints
-
innobackupex --copy-back /var/mybackup/2014-07-01_14-41-41/
-
chown -R mysql.mysql /mydata/
-
service mysqld start
-
cat /var/mybackup/incrbak/2014-07-01_15-11-57/xtrabackup_binlog_info
-
mysqlbinlog --start-position=941 /home/mybinlog/mysql-bin.000009 >/tmp/huifu-15-56.sql
-
source /tmp/huifu-15-56.sql