首先找到my.cnf配置文件位置:

执行命令:

[root@localhost ~]# mysql --help | grep 'Default options' -A 1
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

第一个文件 /etc/my.cnf 即为配置文件

#第一种方式:
#开启binlog日志
log_bin=ON
#binlog日志的基本文件名
log_bin_basename=/var/lib/mysql/mysql-bin
#binlog文件的索引文件,管理所有binlog文件
log_bin_index=/var/lib/mysql/mysql-bin.index
#配置serverid
server-id=123
 
#第二种方式:(把mysqlId加上否则可能不成功)<br>[mysqld]
#此一行等同于上面log_bin三行
log-bin=/var/lib/mysql/mysql-bin
#配置serverid
server-id=123

 ps:把mysqlId标签加上否则可能不成功

修改完配置后,重启mysql。执行SHOW VARIABLES LIKE 'log_bin'; Value 值为 ON即可。

mysql> SHOW VARIABLES LIKE 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> 

mysql重启方式:

# CentOS 6
service mysqld restart

# CentOS 7
systemctl restart mysqld

  

bug相关:

执行SHOW VARIABLES LIKE 'log_bin' 发现binlog还是没有开启,则排查my.cnf配置文件是否真正生效:

客户端连接mysql发现报错日志:

World-writable config file '/etc/my.cnf' is ignored

  

则更改my.cnf的权限

改成777 后,mysql  会忽略配置文件  World-writable config file '/etc/my.cnf' is ignored   ,客户端登陆时会提示这个问题,改一下权限为644,然后重启mysql发现配置生效,binlog为ON状态