Mysql数据库log-slave-updates 参数解释
如果写的不对,还请大家批评指正。谢谢
本文需要验证的疑问:
从库做为其他从库的主库时 log-slave-updates参数是必须要添加的,因为从库要作为其他从库的主库,必须添加该参数。该参数就是为了让从库从主库复制数据时可以写入到binlog日志,为什么要用这个参数写binlog日志呢,不是在配置文件中开启log-bin = /data/3307/mysql-bin选项就可以吗?
答:从库开启log-bin参数,如果直接往从库写数据,是可以记录log-bin日志的,但是从库通过I0线程读取主库二进制日志文件,然后通过SQL线程写入的数据,是不会记录binlog日志的。也就是说从库从主库上复制的数据,是不写入从库的binlog日志的。所以从库做为其他从库的主库时需要在配置文件中添加log-slave-updates参数。
验证过程:
实验环境时多实例主从同步,3306为主库,3307为从库。
一、未添加log-slave-updates参数时:
1、两个数据库已经可以主从同步,并且主从数据已经一致,从库开启log-bin功能。
mysql-slave>system mysql -uroot -p'123' -S /data/3307/mysql.sock -e "show slave status\G"|egrep "Seconds_Behind_Master|_Running"
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
检查主从数据是否一致:
主库:
mysql-master>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|mysql |
|oldboy |
|performance_schema |
|test |
+--------------------+
5 rowsin set (0.00 sec)
从库:
mysql-slave>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|mysql |
|oldboy |
|performance_schema |
|test |
+--------------------+
5 rowsin set (0.01 sec)
查看主库文件
[root@DRS3306]# ll
total68
drwxr-xr-x6 mysql mysql 4096 Apr 27 03:49 data
-rw-r--r--1 mysql mysql 1899 Apr 26 23:41 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:35 mysql
-rw-rw----1 mysql mysql 126 Apr 23 07:35mysql-bin.000001
-rw-rw----1 mysql mysql 126 Apr 26 22:59mysql-bin.000002
-rw-rw----1 mysql mysql 698 Apr 26 23:36mysql-bin.000003
-rw-rw----1 mysql mysql 126 Apr 26 23:42mysql-bin.000004
-rw-rw---- 1 mysqlmysql 1034 Apr 27 03:49 mysql-bin.000005
-rw-rw----1 mysql mysql 140 Apr 26 23:42mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 26 23:42mysql.sock
-rw-r-----1 mysql root 14731 Apr 26 23:51mysql_oldboy3306.err
-rw-rw----1 mysql mysql 6 Apr 26 23:42mysqld.pid
-rw-rw----1 mysql mysql 107 Apr 26 23:42relay-bin.000003
-rw-rw----1 mysql mysql 28 Apr 26 23:42relay-bin.index
-rw-rw----1 mysql mysql 37 Apr 26 23:51relay-log.info
从库配置文件时间:
[root@DRS3307]# ll
total72
drwxr-xr-x6 mysql mysql 4096 Apr 27 03:49 data
-rw-r--r--1 mysql mysql 1905 Apr 27 03:47 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:47 mysql
-rw-rw----1 mysql mysql 203 Apr 27 03:47mysql-bin.000001
-rw-rw---- 1 mysqlmysql 107 Apr 27 03:47 mysql-bin.000002
-rw-rw---- 1 mysqlmysql 56 Apr 27 03:47 mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 27 03:47mysql.sock
-rw-r-----1 mysql root 23291 Apr 27 03:47mysql_oldboy3307.err
-rw-rw----1 mysql mysql 5 Apr 27 03:47mysqld.pid
-rw-rw----1 mysql mysql 150 Apr 27 03:47relay-bin.000005
-rw-rw---- 1 mysqlmysql 413 Apr 27 03:49 relay-bin.000006
-rw-rw----1 mysql mysql 56 Apr 27 03:47relay-bin.index
-rw-rw----1 mysql mysql 54 Apr 27 03:49relay-log.info
测试:在主库上创建数据库,看看从库是否写二进制日志。
[root@DRS 3306]# date
Sun Apr 27 04:02:26 CST 2014
[root@DRS3306]# ll
total68
drwxr-xr-x7 mysql mysql 4096 Apr 27 04:02 data
-rw-r--r--1 mysql mysql 1899 Apr 26 23:41 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:35 mysql
-rw-rw----1 mysql mysql 126 Apr 23 07:35mysql-bin.000001
-rw-rw----1 mysql mysql 126 Apr 26 22:59mysql-bin.000002
-rw-rw----1 mysql mysql 698 Apr 26 23:36mysql-bin.000003
-rw-rw----1 mysql mysql 126 Apr 26 23:42mysql-bin.000004
-rw-rw---- 1 mysqlmysql 1115 Apr 27 04:02 mysql-bin.000005#时间和操作前不一样了,证明主库已经写binlog日志。
-rw-rw----1 mysql mysql 140 Apr 26 23:42mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 26 23:42mysql.sock
-rw-r-----1 mysql root 14731 Apr 26 23:51mysql_oldboy3306.err
-rw-rw----1 mysql mysql 6 Apr 26 23:42 mysqld.pid
-rw-rw----1 mysql mysql 107 Apr 26 23:42relay-bin.000003
-rw-rw----1 mysql mysql 28 Apr 26 23:42relay-bin.index
-rw-rw----1 mysql mysql 37 Apr 26 23:51relay-log.info
从库配置文件时间变化:
[root@DRS3307]# ll
total72
drwxr-xr-x7 mysql mysql 4096 Apr 27 04:02 data
-rw-r--r--1 mysql mysql 1905 Apr 27 03:47 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:47 mysql
-rw-rw----1 mysql mysql 203 Apr 27 03:47mysql-bin.000001
-rw-rw---- 1 mysqlmysql 107 Apr 27 03:47 mysql-bin.000002#时间和操作前的是一样的,确认没有写binlog日志
-rw-rw----1 mysql mysql 56 Apr 27 03:47mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 27 03:47mysql.sock
-rw-r-----1 mysql root 23291 Apr 27 03:47mysql_oldboy3307.err
-rw-rw----1 mysql mysql 5 Apr 27 03:47mysqld.pid
-rw-rw----1 mysql mysql 150 Apr 27 03:47relay-bin.000005
-rw-rw---- 1 mysqlmysql 494 Apr 27 04:02 relay-bin.000006#证明从库已经写relay-log日志
-rw-rw----1 mysql mysql 56 Apr 27 03:47relay-bin.index
-rw-rw---- 1 mysqlmysql 54 Apr 27 04:02 relay-log.info
直接向从库写入数据:
[root@DRS 3307]# date
Sun Apr 27 04:09:29 CST2014
mysql-slave>create database qq;
QueryOK, 1 row affected (0.00 sec)
mysql-slave>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|cbd |
|mysql |
|oldboy |
|performance_schema |
|qq |
|test |
+--------------------+
7 rowsin set (0.00 sec)
查看从库日志文件时间变化:
[root@DRS3307]# ll
total72
drwxr-xr-x8 mysql mysql 4096 Apr 27 04:10 data
-rw-r--r--1 mysql mysql 1905 Apr 27 03:47 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:47 mysql
-rw-rw----1 mysql mysql 203 Apr 27 03:47mysql-bin.000001
-rw-rw---- 1 mysqlmysql 186 Apr 27 04:10 mysql-bin.000002#和在从库上创建qq数据库的时间一致,证明已经写bin-log日志了。
-rw-rw----1 mysql mysql 56 Apr 27 03:47mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 27 03:47mysql.sock
-rw-r-----1 mysql root 23291 Apr 27 03:47mysql_oldboy3307.err
-rw-rw----1 mysql mysql 5 Apr 27 03:47mysqld.pid
-rw-rw----1 mysql mysql 150 Apr 27 03:47relay-bin.000005
-rw-rw----1 mysql mysql 494 Apr 27 04:02relay-bin.000006
-rw-rw----1 mysql mysql 56 Apr 27 03:47relay-bin.index
-rw-rw----1 mysql mysql 54 Apr 27 04:02relay-log.info
二、添加log-slave-updates参数后
1、在主库上做创建操作
[root@DRS 3307]# date
Sun Apr 27 04:19:10 CST2014
mysql-master>create database pang;
QueryOK, 1 row affected (0.00 sec)
mysql-master>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|cbd |
|mysql |
|oldboy |
| pang |
|performance_schema |
|test |
+--------------------+
7 rowsin set (0.00 sec)
检查同步情况:
mysql-slave>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|cbd |
|mysql |
|oldboy |
| pang |#已经同步完成
|performance_schema |
|qq |
|test |
+--------------------+
8 rowsin set (0.00 sec)
检查配置文件时间变化:
主库配置文件时间:
[root@DRS3306]# ll
total68
drwxr-xr-x8 mysql mysql 4096 Apr 27 04:20 data
-rw-r--r--1 mysql mysql 1899 Apr 26 23:41 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:35 mysql
-rw-rw----1 mysql mysql 126 Apr 23 07:35mysql-bin.000001
-rw-rw----1 mysql mysql 126 Apr 26 22:59mysql-bin.000002
-rw-rw----1 mysql mysql 698 Apr 26 23:36mysql-bin.000003
-rw-rw----1 mysql mysql 126 Apr 26 23:42mysql-bin.000004
-rw-rw---- 1 mysqlmysql 1198 Apr 27 04:20 mysql-bin.000005#主库写入时的时间,也就创建数据库时写入log-bin日志的时间
-rw-rw----1 mysql mysql 140 Apr 26 23:42mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 26 23:42 mysql.sock
-rw-r-----1 mysql root 14731 Apr 26 23:51mysql_oldboy3306.err
-rw-rw----1 mysql mysql 6 Apr 26 23:42mysqld.pid
-rw-rw----1 mysql mysql 107 Apr 26 23:42relay-bin.000003
-rw-rw----1 mysql mysql 28 Apr 26 23:42relay-bin.index
-rw-rw----1 mysql mysql 37 Apr 26 23:51relay-log.info
从库配置文件时间变化:
[root@DRS3307]# ll
total80
drwxr-xr-x9 mysql mysql 4096 Apr 27 04:20 data
-rw-r--r--1 mysql mysql 1922 Apr 27 04:16 my.cnf
-rwx------1 mysql mysql 1304 Apr 26 23:47 mysql
-rw-rw----1 mysql mysql 203 Apr 27 03:47mysql-bin.000001
-rw-rw----1 mysql mysql 205 Apr 27 04:16mysql-bin.000002
-rw-rw---- 1 mysql mysql 190 Apr 27 04:20 mysql-bin.000003#从库已经开始写binlog日志了,并且和主库时间一致。
-rw-rw----1 mysql mysql 84 Apr 27 04:16mysql-bin.index
srwxrwxrwx1 mysql mysql 0 Apr 27 04:16mysql.sock
-rw-r-----1 mysql root 25871 Apr 27 04:16mysql_oldboy3307.err
-rw-rw----1 mysql mysql 5 Apr 27 04:16mysqld.pid
-rw-rw----1 mysql mysql 150 Apr 27 04:16relay-bin.000007
-rw-rw----1 mysql mysql 336 Apr 27 04:20relay-bin.000008
-rw-rw----1 mysql mysql 56 Apr 27 04:16relay-bin.index
-rw-rw---- 1 mysql mysql 54 Apr 27 04:20 relay-log.info#relay-log日志文件也变化了
由上面的实验可以知道,
1、从库只开启log-bin功能,不添加log-slave-updates参数,从库从主库复制的数据不会写入log-bin日志文件里。
2、直接向从库写入数据时,是会写入log-bin日志的。
3、开启log-slave-updates参数后,从库从主库复制的数据会写入log-bin日志文件里。这也是该参数的功能。