全备+binlog恢复
启动数据库:service mysql start
开启binlog服务:vi /etc/my.cnf
添加:
server_id=1
gtid_mode=on
enforce_gtid_consistency=on
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row
#relay log
skip-slave-start=1
进入数据库:mysql -uroot -p
创建数据库:create database ceshi; (库名随意)
打开数据库:use ceshi;
在数据库创建:create table t1 (id int,name varchar(10)); (表名随意)
在数据库下插入数据:insert into t1 values(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e') ; (t1是表名,数据随意)
查看表中数据:select * from t1;
提交:commit;
备份数据库:mysqldump -uroot -p --set-gtid-purged=OFF --master-data=2 --single-transaction -A > /opt/full_.sql
查看是否有刚才创建数据库的命令:vi /opt/full_.sql
进入数据库:mysql -uroot -p
打开数据库:use ceshi;
插入两行数据:insert into t1 values (6,'f'),(7,'g');
查看表中的数据:select * from t1;
修改三行表中的数据:update t1 set name='h' where id=2;
update t1 set name='i' where id=3;
update t1 set name='j' where id=5;
(数据随意修改)
删除一行数据:delete from t1 where id=2;
删除表中的所有数据:truncate table t1;
再在表中插入五条数据:insert into t1 values (8,'aa'),(9,'bb'),(10,'cc'),(11,'dd'),(12,'ee');
修改三行:update t1 set name='hh' where id=10;
update t1 set name='ff' where id=12;
update t1 set name='ss' where id=8;
提交:commit;
进入数据库查看终点:show master status;
截取binlog起点及终点的日志:mysqlbinlog --skip-gtids --start-position=154 --stop-position=2738 /data/mysql/data/master-binlog.000001 > /opt/binlog.sql
将全备文件及截取的binlog文件恢复到临时库上(另外一台临时服务器上):
scp /opt/full_.sql root@192.168.100.10:/opt (192.168.100.10指要复制到的主机的IP地址密码是主机密码)
到第二台主机上
查看:ls
进入数据库:mysql -uroot -p
恢复:source /opt/full_.sql
打开数据库:use ceshi;
查看表是否恢复原来的数据:select * from t1;