使用mysqldump实现mysql备份
注意:备份文件和二进制日志文件不能与mysql放在同一磁盘下
节点1
1、节点1上修改mysql配置文件,开起二进制日志保存
这里我将二进制日志放在/data/mysql/目录下,/data/是我创建的另外一个lvm磁盘,本来想直接放在/data/下,发现无法启动mysql,所以建议还是放在/data/mysql中
[root@node1 ~]# mkdir -pv /data/mysql/ [root@node1 ~]# chown mysql:mysql /data/* [root@node1 mysql]# cd /var/lib/mysql [root@node1 mysql]# cp -a mysql-bin.000001 mysql-bin.000002 mysql-bin.index /data/mysql/ [root@node1 ~]# vim /etc/my.cnf.d/server.cnf [server] log_bin=/data/mysql/mysql-bin [root@node1 ~]# service mariadb restart
2、查看二进制日志的一些信息
[root@node1 ~]# mysql mariadb [(none)]> show master logs; +------------------+-----------+ | log_name | file_size | +------------------+-----------+ | mysql-bin.000001 | 264 | | mysql-bin.000002 | 245 | +------------------+-----------+
3、查看表的存储引擎类型并备份
mariadb [hellodb]> show table status\g;
如果engine是myisam则备份方案如下,需要对锁表后操作
[root@node1 ~]# mysqldump -uroot --lock-tables --master-data=2 --flush-logs --databases hellodb > /root/hellodb_myis.sql
如果engine是innodb则备份方案如下
[root@node1 ~]# mysqldump -uroot --single-transaction --master-data=2 --flush-logs --databases hellodb > /root/hellodb_inno.sql --single-transaction:热备 --master-data=2:记录为注释的change master to语句 --flush-logs:日志滚动
批量修改表的存储引擎【将得到的结果一次执行即可修改,不建议直接在mysql中修改】
mariadb [hellodb]> select concat('alter table ',table_name,' engine=innodb;') from information_schema.tables where table_schema='hellodb' and engine='myisam';
4、修改表内数据
mariadb [(none)]> use hellodb; mariadb [hellodb]> insert into students (name,age,gender,classid,teacherid) values ('caocao',99,'m',6,8); mariadb [hellodb]> delete from students where stuid=3;
5、复制备份文件到另一节点
[root@node1 ~]# scp hellodb_inno.sql 192.168.1.114:/root/
节点2
6、在另一个节点进行mysql恢复
修改节点2的配置文件
[root@node2 ~]# mkdir -pv /data/mysql [root@node2 ~]# vim /etc/my.cnf [mysqld] log_bin=/data/mysql/mysql-bin [root@node2 ~]# chown mysql:mysql /data/* [root@node2 ~]# chown mysql:mysql /data [root@node2 ~]# service mariadb start
还原备份文件
[root@node2 ~]# mysql < /root/hellodb_inno.sql [root@node2 ~]# less hellodb_inno.sql -- change master to master_log_file='mysql-bin.000002', master_log_pos=245;
根据表中的显示,在备份那一刻,二进制日志mysql-bin.000002,操作到了245
7、在节点2上恢复二进制日志
在节点1上将245之后的二进制日志文件转换为sql文件
[root@node1 ~]# mysqlbinlog --start-position=245 /var/lib/mysql/mysql-bin.000002 > binlog.sql
复制给节点2
[root@node1 ~]# scp binlog.sql 192.168.1.114:/root/
利用刚才生产的sql文件来恢复备份之后操作的内容
[root@node2 ~]# mysql < /root/binlog.sql
8、查看恢复情况
[root@node2 ~]# mysql mariadb [(none)]> use hellodb; mariadb [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | stuid | name | age | gender | classid | teacherid | +-------+---------------+-----+--------+---------+-----------+ | 1 | shi zhongyu | 22 | m | 2 | 3 | | 2 | shi potian | 22 | m | 1 | 7 | | 4 | ding dian | 32 | m | 4 | 4 | | 5 | yu yutong | 26 | m | 3 | 1 | | 6 | shi qing | 46 | m | 5 | null | | 7 | xi ren | 19 | f | 3 | null | | 8 | lin daiyu | 17 | f | 7 | null | | 9 | ren yingying | 20 | f | 6 | null | | 10 | yue lingshan | 19 | f | 3 | null | | 11 | yuan chengzhi | 23 | m | 6 | null | | 12 | wen qingqing | 19 | f | 1 | null | | 13 | tian boguang | 33 | m | 2 | null | | 14 | lu wushuang | 17 | f | 3 | null | | 15 | duan yu | 19 | m | 4 | null | | 16 | xu zhu | 21 | m | 1 | null | | 17 | lin chong | 25 | m | 4 | null | | 18 | hua rong | 23 | m | 7 | null | | 19 | xue baochai | 18 | f | 6 | null | | 20 | diao chan | 19 | f | 7 | null | | 21 | huang yueying | 22 | f | 6 | null | | 22 | xiao qiao | 20 | f | 1 | null | | 23 | ma chao | 23 | m | 4 | null | | 24 | xu xian | 27 | m | null | null | | 25 | sun dasheng | 100 | m | null | null | | 26 | caocao | 99 | m | 6 | 8 | +-------+---------------+-----+--------+---------+-----------+