mariadb开启binlog日志文件

数据库版本

mysql> select version();
+----------------+
| version()      |
+----------------+
| 10.4.8-MariaDB |
+----------------+

1.编辑配置文件

[root@IT0200005 ~]# vim /etc/my.cnf
# 在[mysqld]下添加
log-bin = /mariadb/data/bin-log/mariadb-log # binlog日志路径,格式为mariadb-log.00000*,递增
log-bin-index = /mariadb/data/bin-log/mariadb-log.index
expire-logs-days = 7 # binlog日志保留天数
server-id = 1
binlog-format = ROW # binlog日志有三种格式,分别是Statement、MiXED、ROW
# max-binlog-size = 100M   # binlog每个日志文件大小

2.恢复操作

# 查看是否开启binlog日志,log_bin为ON表示开启
mysql> show variables like '%log_bin%';
+---------------------------------+-----------------------------------------+
| Variable_name                   | Value                                   |
+---------------------------------+-----------------------------------------+
| log_bin                         | ON                                      |
| log_bin_basename                | /mariadb/data/bin-log/mariadb-log       |
| log_bin_compress                | OFF                                     |
| log_bin_compress_min_len        | 256                                     |
| log_bin_index                   | /mariadb/data/bin-log/mariadb-log.index |
| log_bin_trust_function_creators | OFF                                     |
| sql_log_bin                     | ON                                      |
+---------------------------------+-----------------------------------------+
# 看binlog文件和位置
mysql> show master status;
# 查看日志事件
mysql> show binlog events in 'mariadb-log.000012' limit 10;
----------------------------------------------------
# 恢复数据
# 先把sql_log_bin设置为off,要不然会产生很多binlog日志
mysql> set sql_log_bin=off;
# 查看是否修改成功
show variables like '%log_bin%';
# 恢复数据方法一(mysqlbinlog在数据库安装目录bin目录下)
[root@localhost /]# /usr/local/mariadb/bin/mysqlbinlog /mariadb/data/bin-log/mariadb-log.000002 | mysql -u root erp_yili

# 恢复数据方法二
[root@localhost /]# /usr/local/mariadb/bin/mysqlbinlog --start-datetime="2020-04-15 8:55:00"  --stop-datetime="2020-04-15 10:05:00" /mariadb/data/bin-log/mariadb-log.000004 > /var/opt/baktest.sql
mysqlbinlog --start-position=100  --stop-position=389 --database=erp_yili  /mariadb/data/bin-log/mariadb-log.000004 > /var/opt/baktest.sql
mysql> source /var/opt/baktest.sql
# 开启sql_log_bin
mysql> set sql_log_bin=on;
---------------------------------------------------
# 查看可读形式binlog
/usr/local/mariadb/bin/mysqlbinlog -v --base64-output=decode-rows /mariadb/data/bin-log/mariadb-log.000012 \
    --start-datetime="2020-04-15 10:50:00"  \
    --stop-datetime="2020-04-15 11:00:00"
# 导出可读形式binlog到文件
/usr/local/mariadb/bin/mysqlbinlog --base64-output=decode-rows /mariadb/data/bin-log/mariadb-log.000012 \
    --start-datetime="2020-04-15 10:58:00"  \
    --stop-datetime="2020-04-15 11:00:00" >  /var/opt/baktest.sql

4.其他命令

# 生成新的log文件(文件名递增)
[root@localhost /]# mysqladmin --socket=/mariadb/sock/mysql.sock -uroot flush-logs
# 查看binlog过期时间
mysql> show variables like 'expire_logs_days';
# 设置binlog过期时间
mysql> set global expire_logs_days=7;
# 清除指定日期之前的binlog日志
mysql> purge binary logs before '2020-04-15 00:00:00';

你可能感兴趣的:(Linux,MySQL数据库)