0. precondition

a) install mysql 5.7, for  detail please refer my blog post.

1. login mysql and check the variables to see if the binlog has  been enabled.

mysql -h 127.0.0.1 -uroot -proot
show variables like '%log_bin%';

we can see, the log_bin is disabled.

2. Turn on mysql log_bin

sudo vim /etc/mysql/conf.d/mysql.cnf 

add the following config segment at the end of the file

# ----------------------------------------------
# Enable the binlog for replication & CDC
# ---------------------------------------------- # Enable binary replication log and set the prefix, expiration, and log format.
# The prefix is arbitrary, expiration can be short for integration tests but would
# be longer on a production system. Row-level info is required for ingest to work.
# Server ID is required, but this will vary on production systems
server-id =
log_bin = /var/lib/mysql/mysql-bin
expire_logs_days =
binlog_format = row
#Mysql Packet Size may need to be re-configured. MySQL may have, by default, a ridiculously low allowable packet size.
#To increase it, you’ll need to have the property max_allowed_packet set to a higher number, say 1024M.
max_allowed_packet=1024M

this configration means:

a) the server id is unique for each server, an is required for log_bin capture, it should be a numeric number equal or greater than 0, in my instance I set it to 223344, this number should be unique in the whole cluster.  seems it's a good idea to set it as the ip

address number of the machine install. I fact I have do this in my real production enviroment.

b) the path of the log_bin, this is required  to define the storage location fo the log_bin.

c) the log_bin retention time, in my case, I set it to 3 days.

d. the bin_log format, we should define it as row.

The whole definition file in my case is:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at % of total RAM for dedicated server, else %.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
#secure-file-priv=/var/lib/mysql-files
user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links= #log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid # ----------------------------------------------
# Enable the binlog for replication & CDC
# ---------------------------------------------- # Enable binary replication log and set the prefix, expiration, and log format.
# The prefix is arbitrary, expiration can be short for integration tests but would
# be longer on a production system. Row-level info is required for ingest to work.
# Server ID is required, but this will vary on production systems
server-id =
log_bin = /var/lib/mysql/mysql-bin
expire_logs_days =
binlog_format = row
#Mysql Packet Size may need to be re-configured. MySQL may have, by default, a ridiculously low allowable packet size.
#To increase it, you’ll need to have the property max_allowed_packet set to a higher number, say 1024M.
max_allowed_packet=1024M
#set default charactor set to utf-
character-set-server=utf8
collation-server=utf8_unicode_ci

3. restart mysql service

systemctl restart mysql

after the mysql restarted, we use the command

show variables like '%log_bin%';

and we should found the log_bin is turned on now:  log_bin                         | ON  

then we go to file system, and can fould like this :

the log bin files are just there now!

-rw-r----- 1 mysql mysql 177 Apr 20 15:06 mysql-bin.000001
-rw-r----- 1 mysql mysql 154 Apr 20 15:22 mysql-bin.000002
-rw-r----- 1 mysql mysql 64 Apr 20 15:22 mysql-bin.index

In order to read mysql binlog, we need to grant the mysql user the following permissions:

  • SELECT

  • RELOAD

  • SHOW DATABASES

  • REPLICATION SLAVE

  • REPLICATION CLIENT

The first three privileges are required when reading a consistent snapshot of the databases. The last two privileges allow the database to read the server’s binlog that is normally used for MySQL replication.

you can see the permmission for the user by execute the following command in mysql terminal.

show grants for cdc-user ;  -- cdc-user is the user name I used to do cdc synchronization.

the output is

--------------------------+
| Grants for cdc-user@% |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT RELOAD, PROCESS, ALTER, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cdc-user'@'%' IDENTIFIED BY PASSWORD '*C8C6BD45F62159406C6E0587C42BDE28FFA5F973' |
| GRANT SELECT, LOCK TABLES, SHOW VIEW ON `inventory`.* TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`help_relation` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_leap_second` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_name` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`proc` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`general_log` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`slow_log` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`func` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`help_topic` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_transition_type` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`event` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_transition` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`time_zone` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`help_keyword` TO 'cdc-user'@'%' |
| GRANT SELECT ON `mysql`.`help_category` TO 'cdc-user'@'%' |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+

this indicate the user also need select permission for mysql database.

Notes: every time we restart the mysql server instance, it will  call flush logs and then create a new binlog file. 

mysql 5.7 enable binlog的更多相关文章

  1. MySQL Q&A 解析binlog的两个问题

    MySQL Q&A 解析binlog的两个问题 博客分类: MySQL mysqlbinlog字符集解析binlog格式 连续碰到两个同学问类似的问题,必须要记录一下. 问题:     一个作 ...

  2. mysql之 innobackupex备份+binlog日志的完全恢复【转】

    前言: MySQL的完全恢复,我们可以借助于完整的 备份+binlog 来将数据库恢复到故障点. 备份可以是热备与逻辑备份(mysqldump),只要备份与binlog是完整的,都可以实现完全恢复. ...

  3. MySQL 之 mysqlbinlog解析binlog乱码问题解密

    发现mysql库的binlog日志出来都是乱码,如下所示: BINLOG ’ IXZqVhNIAAAALQAAAGcBAAAAAHoAAAAAAAEABHRlc3QAAno0AAEDAABUOcnY ...

  4. mysql中删除binlog的方法?mysql中如何删除binlog?

    需求描述: 在mysql中如何删除binlog,因为随着数据库的运行,mysql中产生的binlog会越来越大,有可能把磁盘撑爆了,所以记录下删除 binlog的方法. 操作过程: 1.通过系统参数控 ...

  5. MySQL redo log 与 binlog 的区别

    MySQL redo log 与 binlog 的区别 什么是redo log 什么是binlog redo log与binlog的区别 1. 什么是redo log? redo log又称重做日志文 ...

  6. MySQL 5.7 - 通过 BINLOG 恢复数据

    日常开发,运维中,经常会出现误删数据的情况.误删数据的类型大致可分为以下几类: 使用 delete 误删行 使用 drop table 或 truncate table 误删表 使用 drop dat ...

  7. mysql小白系列_04 binlog(未完)

    mysql打开.查看.清理binlog 1.开启日志 log_bin=/var/lib/mysql/mysql-bin mysql> show variables like '%log_bin% ...

  8. Mysql 数据恢复流程 基于binlog redolog undolog

    注:文中有个易混淆的地方 sql事务,即每次数据库操作生成的事务,这个事务trx_id只在undolog里存储,同时undolog维护了此事务是否完成的状态. 日志持久化事务,为了保证redolog和 ...

  9. mysql的DISABLE/ENABLE KEYS

    有一个表 tbl1 的结构如下: CREATE TABLE `tbl1` ( `id` int(10) unsigned NOT NULL auto_increment, `name` char(20 ...

随机推荐

  1. 将简单Excel表格显示到DataGridView中

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. zuul源码(2)

    路由 路由是网关的核心功能,既然在spring的框架下,那就要按Spring的规矩来. 路由规则类:org.springframework.cloud.netflix.zuul.filters.Rou ...

  3. PKU《程序设计》专项课程_递归汉诺塔问题

    取自coursera.org上公开课北京大学<C程序设计进阶> 递归调用注意的点 1.关注点放在求解的目标上,递推是,目标放在开头 2.找到第N次和第(N-1)次之间的关系,通项公式 3. ...

  4. 04bootstrap_表单

    03bootstrap_表单 表单的基本实例 1.默认表单:form 表单域 fieldset legend label 提示span class="help-block" 2.基 ...

  5. mysql_索引

    .默认情况下大多使用Btree索引,该索引就是通常所见 唯一索引.聚簇索引等等,Btree用在OLTP,加快查询速度 查询表索引 show  index  from  tablename 查询表结构 ...

  6. javascript第一个作业之网页计算器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 对于Linux内核执行过程的理解(基于fork、execve、schedule等函数)

    382 + 原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 一.实验环境 win10 -> VMware -> Ubuntu1 ...

  8. django1.4 简单事例 ,根目录下templates

    django发展很快,但是有的是用的老版本,比如我现在看到一个项目,它用的是 Django1.4,而且app不是创建在了项目的根目录下,这样,它的Setting中设置就会不一样,若是设置错误,就会找不 ...

  9. html基础学习笔记1

    <!DOCTYPE html> 声明为 HTML5 文档  <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(meta)数据,如 &l ...

  10. 自定义事件——Event和CustomEvent

    之前在学习自定义事件时,在MDN的Event.initEvent()页面顶端有写:该特性已从Web标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性. 作 ...