MySQL5.6基于GTID的主从复制
mysql 5.6 的新特性之一,是加入了全局事务 id (gtid) 来强化数据库的主备一致性,故障恢复,以及容错能力。
什么是gtid?
官方文档:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html在这篇文档里,我们可以知道全局事务 id 的官方定义是:gtid = source_id:transaction_id
mysql 5.6 中,每一个 gtid 代表一个数据库事务。在上面的定义中,source_id 表示执行事务的主库 uuid(server_uuid),transaction_id 是一个从 1 开始的自增计数,表示在这个主库上执行的第 n 个事务。mysql 会保证事务与 gtid 之间的 1 : 1 映射。
一、环境准备
操作系统:centos6.5 64位
数据库版本:mysql5.6.23
拓扑如下:
三、安装主数据库(masterdb.example.com)
1、准备数据存放目录、创建用户
[root@masterdb ~]#mkdir /data/mysqldata -p #创建数据存放目录 [root@masterdb ~]#mkdir /data/mysqllog/logs -p #创建日志存放目录 [root@masterdb ~]#groupadd -r mysql [root@masterdb ~]#useradd -g mysql -r -s /sbin/nologin -m -d /data/mysqldata mysql [root@masterdb ~]#chown -r mysql:mysql /data/mysqldata [root@masterdb ~]#chown -r mysql:mysql /data/mysqllog/logs
2、安装并初始化mysql5.6.23
[root@masterdb ~]# tar xf mysql-advanced-5.6.23-linux-glibc2.5-x86_64.tar.gz -c /usr/local/ [root@masterdb ~]# cd /usr/local/ [root@masterdb ~]# ln -sv mysql-advanced-5.6.23-linux-glibc2.5-x86_64 mysql [root@masterdb ~]# chown -r root.mysql mysql [root@masterdb ~]# cd mysql [root@masterdb ~]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@masterdb ~]# cp support-files/my-default.cnf /etc/my.cnf [root@masterdb ~]# chmod +x /etc/rc.d/init.d/mysqld [root@masterdb ~]# chkconfig --add mysqld [root@masterdb ~]# chkconfig mysqld on [root@masterdb ~]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldata/
3、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
manpath /usr/local/mysql/man
4、输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
[root@masterdb ~]#ln -sv /usr/local/mysql/include /usr/include/mysql
5、输出mysql的库文件给系统库查找路径:
[root@masterdb ~]#echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
[root@masterdb ~]# ldconfig
6、修改path环境变量,让系统可以直接使用mysql的相关命令:
[root@masterdb ~]# vim /etc/profile.d/mysql.sh export path=$path:/usr/local/mysql/bin [root@masterdb ~]#source /etc/profile.d/mysql.sh
从数据库安装同上,具体过程略过。
四、分别为主从数据库提供配置文件/etc/my.cnf
要在mysql 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项:
binlog-format:二进制日志的格式,有row、statement和mixed几种类型;
需要注意的是:当设置隔离级别为read-commited必须设置二进制日志格式为row,现在mysql官方认为statement这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;
log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于启动gtid及满足附属的其它需求;
master-info-repository和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;
sync-master-info:启用之可确保无信息丢失;
slave-paralles-workers:设定从服务器的sql线程数;0表示关闭多线程复制功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;
binlog-rows-query-log-events:启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;
log-bin:启用二进制日志,这是保证复制功能的基本前提;
server-id:同一个复制拓扑中的所有服务器的id号必须惟一;
主数据库上:
[client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash default-character-set = utf8 [mysqld] server-id = 1 port = 3306 user = mysql basedir = /usr/local/mysql datadir = /data/mysqldata socket = /tmp/mysql.sock default-storage-engine = innodb character-set-server = utf8 connect_timeout = 60 interactive_timeout = 28800 wait_timeout = 28800 back_log = 500 event_scheduler = on skip_name_resolve = on; ###########binlog########## log-bin = /data/mysqllog/logs/mysql-bin binlog_format = row max_binlog_size = 128m binlog_cache_size = 2m expire-logs-days = 5 log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=table relay-log-info-repository=table sync-master-info=1 slave-parallel-workers=4 #rpl_semi_sync_master_enabled = 1 slow_query_log = 1 slow_query_log_file = /data/mysqllog/logs/mysql.slow long_query_time = 1 log_error = /data/mysqllog/logs/error.log max_connections = 3000 max_connect_errors = 32767 log_bin_trust_function_creators = 1 transaction_isolation = read-committed
从数据库上:
[client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysql] no-auto-rehash default-character-set = utf8 [mysqld] server-id = 205 port = 3306 user = mysql basedir = /usr/local/mysql datadir = /data/mysqldata socket = /tmp/mysql.sock default-storage-engine = innodb character-set-server = utf8 connect_timeout = 60 wait_timeout = 18000 back_log = 500 event_scheduler = on ###########binlog########## log-bin = /data/mysqllog/logs/mysql-bin binlog_format = row max_binlog_size = 128m binlog_cache_size = 2m expire-logs-days = 5 log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=table relay-log-info-repository=table sync-master-info=1 slave-parallel-workers=4 #rpl_semi_sync_slave_enabled = 1 skip-slave-start slow_query_log = 1 slow_query_log_file = /data/mysqllog/logs/mysql.slow long_query_time = 2 log-error = /data/mysqllog/logs/error.log max_connections = 3000 max_connect_errors = 10000 log_bin_trust_function_creators = 1 transaction_isolation = read-committed
五、分别在主从数据库上启动mysqld服务
[root@masterdb ~]# service mysqld start starting mysql...... [ ok ] [root@masterdb ~]# [root@slavedb ~]# service mysqld start starting mysql...... [ ok ] [root@slavedb ~]#
六、在主数据库上创建复制用户
说明:172.16.88.205是从节点服务器;如果想一次性授权更多的节点,可以自行根据需要修改;
七、启动从数据库上的复制线程
mysql> change master to master_host='masterdb.example.com', master_user='repluser', master_password='replpassword', master_auto_position=1; mysql>start slave;
八、在从数据库上查看复制状态
mysql> show slave status\g; *************************** 1. row *************************** slave_io_state: waiting for master to send event master_host: masterdb.56xyl.com master_user: repluser master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000002 read_master_log_pos: 191 relay_log_file: slavedb-relay-bin.000003 relay_log_pos: 401 relay_master_log_file: mysql-bin.000002 slave_io_running: yes #io线程已正常运行 slave_sql_running: yes #sql线程已正常运行 replicate_do_db: replicate_ignore_db: replicate_do_table: replicate_ignore_table: replicate_wild_do_table: replicate_wild_ignore_table: last_errno: 0 last_error: skip_counter: 0 exec_master_log_pos: 191 relay_log_space: 1899 until_condition: none until_log_file: until_log_pos: 0 master_ssl_allowed: no master_ssl_ca_file: master_ssl_ca_path: master_ssl_cert: master_ssl_cipher: master_ssl_key: seconds_behind_master: 0 master_ssl_verify_server_cert: no last_io_errno: 0 last_io_error: last_sql_errno: 0 last_sql_error: replicate_ignore_server_ids: master_server_id: 1 master_uuid: 971d7245-c3f8-11e5-8b6b-000c2999e5a5 master_info_file: mysql.slave_master_info sql_delay: 0 sql_remaining_delay: null slave_sql_running_state: slave has read all relay log; waiting for the slave i/o thread to update it master_retry_count: 86400 master_bind: last_io_error_timestamp: last_sql_error_timestamp: master_ssl_crl: master_ssl_crlpath: retrieved_gtid_set: 971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6 executed_gtid_set: 89e78301-c3f4-11e5-8b51-00505624d26a:1-3, 971d7245-c3f8-11e5-8b6b-000c2999e5a5:1-6 auto_position: 1 1 row in set (0.00 sec) error: no query specified mysql>
九、测试
在主库上创建数据库:
mysql> create database log_statics; query ok, 1 row affected (0.11 sec) mysql> use log_statics; database changed 到从数据库上查看log_statics是否已经复制过去 mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | log_statics | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.01 sec) mysql>
可以看到log_statics数据库已经存在于从数据库上。
以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇: python去除字符串中的换行符
下一篇: 详解java8中的Stream数据流