一、 GTID 简介 什么是 GTID GTID(Global Transaction ID) 是对于一个已提交事务的编号,并且是一个全局唯一的编号。 GTID 实际上是由 UUID+TID 组成的。其中 UUID 是一个 MySQL 实例的唯一标识。 TID 代表了该实例上已经提交的事务数量,并且随着事务提交单
一、GTID简介
什么是GTID
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。
GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。下面是一个GTID的具体形式
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
更详细的介绍可以参见:官方文档
GTID的作用
那么GTID功能的目的是什么呢?具体归纳主要有以下两点:
1、根据GTID可以知道事务最初是在哪个实例上提交的
2、GTID的存在方便了Replication的Failover
二、介绍半同步复制
半同步复制可以作为默认的异步复制的替代方案,用以提高数据完整性。
使用半同步复制,提交操作只有当一个从服务器已经接收到更新,或者超时后才返回客户端。因此,它可以确保数据存在于主服务器以及至少一个从服务器中(注意,提交操作返回时,从服务器已经接收到更新,但不一定已经应用了该更新)。
可以组合使用不同的复制模式,因此一些从服务器配置为异步复制,而其他从服务器使用半同步复制。这样最终意味着开发人员/DBA能够基于每个从服务器确定合适的数据一致性和性能级别。
以上描述的不同复制模式可以与完全同步复制进行比较,后者使用“两阶段提交”协议同时将数据提交到两个或者更多实例中。同步复制能够确保多个系统之间的一致性,故障时提供更快的故障转移时间,但是会因为在节点之间传递更多的消息导致性能消耗。
三、环境
环境
系统:centos6.5
master IP: 172.19.0.105
slave IP:172.19.0.102
mysql版本:5.6.23
四、mysql安装(两台)
1、采用yum安装依赖包
yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* cmake bison
2、创建mysql用户与用户组
groupadd mysql
useradd -r -g mysql mysql
3.解压安装mysql源代码包
tar zxvf mysql-5.6.23.tar.gz
cd mysql-5.6.23
cmake ./ -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/mysql/data -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysql.sock -DMYSQL_USER=mysql -DWITH_DEBUG=0
make && make install
4、赋予相关权限
chown -R mysql:mysql /opt/mysql
/opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
5、mysql配置文件
cp support-files/my-default.cnf /etc/my.cnf #配置文件
cp support-files/mysql.server /etc/init.d/mysql #启动文件
chmod +x /etc/init.d/mysql
mkdir /opt/mysql/logs #创建存放日志文件
chown -R mysql:mysql /opt/mysql/logs #赋予相关权限
chkconfig mysql on #增加开机启动
6、启动 mysql
/etc/init.d/mysql start
7、设置mysql初始密码,123456是密码,你可以设置自己需要的密码
/opt/mysql/bin/mysqladmin -u root password '123456'
8、增加软连接
ln -s /opt/mysql/bin/* /usr/bin/
五、主从模式配置
1、配置master节点 在[mysqld]下增加
log-bin=master-bin
binlog_format = ROW
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 #开启基于库的多线程复制。默认是0,不开启,最大并发数为1024个线程
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=MASTER
2、配置slave节点 在[mysqld]下增加把server-id = 1修改成2与master要不一样,否则会报错
server-id = 2
log-bin=mysql-bin
binlog_format = ROW
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 #开启基于库的多线程复制。默认是0,不开启,最大并发数为1024个线程
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=SLAVE
3、master与slave配置好重启
/etc/init.d/mysql restart
4、创建复制用户
在主库(master)执行
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.19.0.%' IDENTIFIED BY 'repl';
flush privileges;
在主库(master)执行安装相关插入件启动半同步复制
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SET GLOBAL rpl_semi_sync_master_enabled=1;
SET GLOBAL rpl_semi_sync_master_timeout=1000;
在从库(slave)执行安装相关插入件启动半同步复制
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled=1;
在从库(slave)执行复制连接
CHANGE MASTER TO MASTER_HOST='172.19.0.105',MASTER_USER='repl',MASTER_PASSWORD='repl',master_auto_position=1,master_delay=20;
start slave;
查看主从已经启动gtid功能
在主库查看连接的slave主机
查看从库复制状态
以上说明成功
下面是我修改一些参数配置为了优化,大家可以根据自己环境来修改
主库(master)配置:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[client]
port = 3306
socket = /opt/mysql/data/mysql.sock
default-character-set = utf8
################Basic############
[mysqld]
server-id = 1
port = 3306
socket = /opt/mysql/data/mysql.sock
skip-external-locking
skip-name-resolve
default-storage-engine=INNODB
character-set-server=utf8
wait_timeout=500
connect_timeout=20
interactive_timeout=500
back_log=500
event_scheduler=ON
##########binlog##########
expire-logs-days=5
log-bin=master-bin
binlog_format = ROW
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=2 #开启基于库的多线程复制。默认是0,不开启,最大并发数为1024个线程
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=MASTER
#############半同步###########
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000
rpl_semi_sync_master_trace_level=32
rpl_semi_sync_master_wait_no_slave=on
#######################
max_binlog_size=128M
binlog_cache_size=2M
sync_binlog=1
#################slow log ###########
slow-query_log=1
slow-query_log_file=/opt/mysql/logs/mysql.slow
long_query_time=2
####################error log####
log_error=/opt/mysql/logs/error.log
#################per_thread_buffers#############
max_connections=1024
max_user_connections=2000
max_connect_errors=10000
key_buffer_size=64M
max_allowed_packet=128M
table_open_cache=6144
table_definition_cache=4096
sort_buffer_size=512k
read_buffer_size=512k
read_rnd_buffer_size=512k
join_buffer_size=512k
tmp_table_size=64M
max_heap_table_size=64M
query_cache_type=0
query_cache_size=0
bulk_insert_buffer_size=32M
thread_cache_size=64
thread_concurrency=32
thread_stack=256K
################INNODB################
innodb_data_home_dir = /opt/mysql/data
innodb_log_group_home_dir = /opt/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances = 4
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=10
innodb_file_per_table
innodb_log_files_in_group=3
innodb_support_xa=1
innodb_sync_spin_loops=40
innodb_max_dirty_pages_pct=90
innodb_additional_mem_pool_size=20M
innodb_log_buffer_size=128M
innodb_flush_method=O_DIRECT
innodb_file_format=Barracuda
innodb_io_capacity=2000
log_bin_trust_function_creators=1
innodb_purge_threads=1
innodb_purge_batch_size=32
innodb_old_blocks_pct=75
transaction_isolation=READ-COMMITTED
#innodb_read_io_threads=8
#innodb_write_io_threads=8
[mysqldump]
quick
max_allowed_packet=128M
myisam_max_sort_sort_file_size=2G
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 64M
sort_buffer_size =256K
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
从库(slave)配置:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[client]
port = 3306
socket = /opt/mysql/data/mysql.sock
default-character-set = utf8
################Basic############
[mysqld]
server-id = 2
port = 3306
socket = /opt/mysql/data/mysql.sock
skip-external-locking
skip-name-resolve
default-storage-engine=INNODB
character-set-server=utf8
wait_timeout=500
connect_timeout=20
interactive_timeout=500
back_log=500
event_scheduler=ON
##########binlog##########
expire-logs-days=5
log-bin=mysql-bin
binlog_format = ROW
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=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=SLAVE
##############半同步#####################
rpl_semi_sync_slave_enabled=1
##################################
max_binlog_size=128M
binlog_cache_size=2M
sync_binlog=1
#################slow log ###########
slow-query_log=1
slow-query_log_file=/opt/mysql/logs/mysql.slow
long_query_time=2
####################error log####
log_error=/opt/mysql/logs/error.log
#################per_thread_buffers#############
max_connections=1024
max_user_connections=2000
max_connect_errors=10000
key_buffer_size=64M
max_allowed_packet=128M
table_open_cache=6144
table_definition_cache=4096
sort_buffer_size=512k
read_buffer_size=512k
read_rnd_buffer_size=512k
join_buffer_size=512k
tmp_table_size=64M
max_heap_table_size=64M
query_cache_type=0
query_cache_size=0
bulk_insert_buffer_size=32M
thread_cache_size=64
thread_concurrency=32
thread_stack=256K
################INNODB################
innodb_data_home_dir = /opt/mysql/data
innodb_log_group_home_dir = /opt/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances = 4
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=10
innodb_file_per_table
innodb_log_files_in_group=3
innodb_support_xa=1
innodb_sync_spin_loops=40
innodb_max_dirty_pages_pct=90
innodb_additional_mem_pool_size=16M
innodb_log_buffer_size=64M
innodb_flush_method=O_DIRECT
innodb_file_format=Barracuda
innodb_io_capacity=2000
log_bin_trust_function_creators=1
innodb_purge_threads=1
innodb_purge_batch_size=32
innodb_old_blocks_pct=75
transaction_isolation=READ-COMMITTED
#innodb_read_io_threads=8
#innodb_write_io_threads=8
[mysqldump]
quick
max_allowed_packet=128M
myisam_max_sort_sort_file_size=2G
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 64M
sort_buffer_size =256K
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
主从修改配置后,我们重启mysql数据库,在来查看半同步复制
主库查看半同步复制
从库查看半同步复制
上图说明半同步复制成功