MySQL的备份
MySQL的备份
开启MySQL的log_bin
执行查看mysql的log_bin状态
> show variables like 'log_bin%'; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | OFF | | log_bin_basename | | | log_bin_index | | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | +---------------------------------+--------------------------------+
开启log_bin日志
打开mysql配置文件:
root@ubuntu:~# vi /etc/mysql/mysql.conf.d/mysqld.cnf
开启log_bin日志:
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
server-id表示单个结点的id,这里由于只有一个结点,所以可以把id随机指定为一个数,这里将id设置成1。若集群中有多个结点,则id不能相同。
第二句是指定binlog日志文件的名字为mysql-bin,以及其存储路径。
重启MySQL:
service mysql restart
重新查看log_bin状态:
> show variables like 'log_bin%'; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | ON | # log_bin日志开启 | log_bin_basename | /var/log/mysql/mysql-bin | | log_bin_index | /var/log/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | +---------------------------------+--------------------------------+
log_bin日志操作的常用命令
查看日志文件:
> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
创建新的日志文件:
> flush logs; # 创建一个新的日志文件用于记录 Query OK, 0 rows affected (0.01 sec) > show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000004 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
查看日志文件个数:
> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 177 | | mysql-bin.000002 | 177 | | mysql-bin.000003 | 201 | | mysql-bin.000004 | 154 | +------------------+-----------+
重置日志文件:
> reset master; # 重置日志文件后,日志文件恢复为最初的一个 Query OK, 0 rows affected (0.01 sec) > show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 154 | +------------------+-----------+
MySQL数据的备份与恢复(通过log_bin日志)
向mydb数据库的stu表中插入数据:
> insert into stu values(1, '张三'), -> (2, '李四'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0
查看log_bin日志文件:
因为log_bin日志文件是二进制文件,普通方式打开会乱码,用mysql自带的mysqlbinlog命令打开。重定向到more分页显示。
root@ubuntu:/var/log/mysql#mysqlbinlog mysql-bin.000001 | more
解码查看日志文件
root@ubuntu:/var/log/mysql# mysqlbinlog --base64-output=decode-rows -v mysql-bin.000001 # 日志文件中记录着增删改的操作
备份mydb数据库:
root@ubuntu:~# mysqldump -u root -p mydb>mydb.sql
# 继续增删改操作: > insert into stu values(3, '王舞'); # 增 Query OK, 1 row affected (0.00 sec) > update stu set name='张三2' where id=1; # 改 Query OK, 1 row affected (0.00 sec) # 误操作删库: > delete from stu; Query OK, 3 rows affected (0.00 sec) # 先通过导入备份数据恢复到最近一次备份的时候的数据: root@ubuntu:~# mysql -u root -p mydb<mydb.sql # 恢复最近的备份数据 > select * from stu; # 恢复了最近的备份数据 +----+--------+ | id | name | +----+--------+ | 1 | 张三 | | 2 | 李四 | +----+--------+
然后再备份log_bin日志:
root@ubuntu:/var/log/mysql# mysqlbinlog --stop-position=882 mysql-bin.000003 | mysql -u root -p # 备份log_bin日志,停止点在删除前的位置点882
> select * from stu; # 数据库备份后执行的增、改操作的内容恢复 +----+---------+ | id | name | +----+---------+ | 1 | 张三2 | | 2 | 李四 | | 3 | 王舞 | +----+---------+