您的位置:首页 > 数据库 > MySQL

mysql cp复制和mysqldump备份测试

2016-08-12 11:33 405 查看
本文来自我的githubpages博客http://galengao.github.io/即www.gaohuirong.cn

备份策略

针对不同的场景下,我们应该制定不同的备份策略对数据库进行备份,一般情况下,备份策略一般为以下三种:

直接cp,tar复制数据库文件

mysqldump复制BINLOGS

lvm2快照复制BINLOGS

xtrabackup
以上的几种解决方案分别针对于不同的场景

如果数据量较小,可以使用第一种方式,直接复制数据库文件

如果数据量还行,可以使用第二种方式,先使用mysqldump对数据库进行完全备份,然后定期备份BINARYLOG达到增量备份的效果

如果数据量一般,而又不过分影响业务运行,可以使用第三种方式,使用lvm2的快照对数据文件进行备份,而后定期备份BINARYLOG达到增量备份的效果

如果数据量很大,而又不过分影响业务运行,可以使用第四种方式,使用xtrabackup进行完全备份后,定期使用xtrabackup进行增量备份或差异备份

实战演练

cp复制数据文件备份及恢复

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
+--------------------+
4rowsinset(0.03sec)

mysql>createdatabasetest;
QueryOK,1rowaffected(0.03sec)

mysql>usetest
Databasechanged
mysql>showtables;
Emptyset(0.00sec)

mysql>createtablea(idint,namevarchar(10));
QueryOK,0rowsaffected(0.04sec)

mysql>showtables;
+----------------+
|Tables_in_test|
+----------------+
|a|
+----------------+
1rowinset(0.00sec)

mysql>insertintoa(id,name)values(1,'gao')
->;
QueryOK,1rowaffected(0.01sec)

mysql>commit;
QueryOK,0rowsaffected(0.00sec)

mysql>select*froma
->;
+------+------+
|id|name|
+------+------+
|1|gao|
+------+------+
1rowinset(0.00sec)

[root@my57mysql]#mkdir/backup

[root@my57mysql]#cp-a/data/mysql/data/*/backup/

[root@my57mysql]#ls/backup/
auto.cnfib_buffer_poolibdata1ib_logfile0ib_logfile1ibtmp1my57.errmy57.pidmysqlmysqld_safe.pidperformance_schemasystestxtrabackup_info

[root@my57mysql]#rm-rf/data/mysql/data/*
[root@my57mysql]#servicemysqlrestart

ERROR!MySQLserverPIDfilecouldnotbefound!
StartingMySQL...ERROR!TheserverquitwithoutupdatingPIDfile(/data/mysql/data//my57.pid).
这时启动不了,我们再把备份文件拷贝回来,在启动就可以了。

cp-a/backup/*/data/mysql/data/


mysqldump的复制与恢复

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
|test|
+--------------------+
5rowsinset(0.02sec)

mysql>usetest;
Readingtableinformationforcompletionoftableandcolumnnames
Youcanturnoffthisfeaturetogetaquickerstartupwith-A

Databasechanged
mysql>showtables;
+----------------+
|Tables_in_test|
+----------------+
|a|
|b|
+----------------+
2rowsinset(0.00sec)
mysql>select*fromb;
+------+
|id|
+------+
|1|
+------+
1rowinset(0.00sec)

#记住备份前position的值
mysql>showmasterstatus
->;
+----------------+----------+--------------+------------------+-------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
+----------------+----------+--------------+------------------+-------------------+
|bin-log.000001|567||||
+----------------+----------+--------------+------------------+-------------------+
1rowinset(0.00sec)



开始备份

[root@my57data]#mysqldump--all-databases--lock-all-tables>/backup/backup.sql


再创建一个数据库做增量测试

mysql>createdatabasetest1;
QueryOK,1rowaffected(0.00sec)

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
|test|
|test1|
+--------------------+
6rowsinset(0.00sec)

#再记下现在的position位置
mysql>SHOWMASTERSTATUS;
+----------------+----------+--------------+------------------+-------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
+----------------+----------+--------------+------------------+-------------------+
|bin-log.000001|869||||
+----------------+----------+--------------+------------------+-------------------+
1rowinset(0.00sec)
备份二进制日志

cpbin-log.000001/backup/


停止mysql在启动,编译安装的启动不了,必须重新初始化

[root@my57data]#servicemysqlstop
ShuttingdownMySQL.SUCCESS!

#模拟删除数据文件
[root@my57data]#rm-rf*

#编译安装的数据库删除数据文件后启动不了
[root@my57data]#servicemysqlstart
StartingMySQL...ERROR!TheserverquitwithoutupdatingPIDfile(/data/mysql/data//my57.pid).

#重新初始化mysql
[root@my57data]#/usr/local/mysql/bin/mysqld--initialize-insecure--user=mysql
2016-06-14T02:43:04.084864Z0[Warning]TIMESTAMPwithimplicitDEFAULTvalueisdeprecated.Pleaseuse--explicit_defaults_for_timestampserveroption(seedocumentationformoredetails).
2016-06-14T02:43:04.084944Z0[Warning]'NO_ZERO_DATE','NO_ZERO_IN_DATE'and'ERROR_FOR_DIVISION_BY_ZERO'sqlmodesshouldbeusedwithstrictmode.Theywillbemergedwithstrictmodeinafuturerelease.
2016-06-14T02:43:04.084952Z0[Warning]'NO_AUTO_CREATE_USER'sqlmodewasnotset.
2016-06-14T02:43:04.459619Z0[Warning]InnoDB:Newlogfilescreated,LSN=45790
2016-06-14T02:43:04.502310Z0[Warning]InnoDB:Creatingforeignkeyconstraintsystemtables.
2016-06-14T02:43:04.567746Z0[Warning]NoexistingUUIDhasbeenfound,soweassumethatthisisthefirsttimethatthisserverhasbeenstarted.GeneratinganewUUID:b7fa4c2e-31d9-11e6-983a-080027fff0b0.
2016-06-14T02:43:04.571117Z0[Warning]Gtidtableisnotreadytobeused.Table'mysql.gtid_executed'cannotbeopened.
2016-06-14T02:43:04.575259Z1[Warning]root@localhostiscreatedwithanemptypassword!Pleaseconsiderswitchingoffthe--initialize-insecureoption.

#启动
[root@my57data]#servicemysqlstart
StartingMySQL.SUCCESS!

#利用原来的别分还原,发现还原了但是缺少test1
mysql>sourcebackup.sql

mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
|test|
+--------------------+
5rowsinset(0.00sec)

#利用mysqlbinlog二进制恢复test1,这时上面的开始位置和结束位置就有用了
[root@my57backup]#mysqlbinlog--start-position=567--stop-position=869/backup/bin-log.000001|mysql-uroot-p
mysql>showdatabases;
+--------------------+
|Database|
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
|test|
|test1|
+--------------------+
6rowsinset(0.00sec)



xtrabackup备份参考另外文章

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: