一:概念理解:
1.TID:Transaction ID,即Mysql服务器的事务ID号。
2.GTID:Global Transaction ID,全局事务ID,在整个主从复制架构中任何两个事物ID是不能相同的。
3.全局事物ID是Mster服务器生成一个128位的UUID+事物的ID号组成的,UUID标示主服务器的身份,此UUID在整个主从复制架构中是绝对唯一,而且即使更换主服务器后UUID也不会改变而是继承当前主服务器的UUID身份。
4.全局事务ID有何用处?简单来讲GTID能够保证让一个从服务器到其他的从服务器那里实现数据复制而且能够实现数据整合的。GTID在分布式架构中可以保证数据的一致性。从而也实现了mysql的高可用性。
5.GTID相关操作:默认情况下将一个事务记录进二进制文件时将首先记录它的GTID而且GTID和事务相关信息一并要发送给从服务器由从服务器在在本地应用认证但是绝对不会改变原来的事务ID号。
6.因此在GTID的架构上就算有了N层架构,复制是N级架构,但是全局事务ID依然不会改变;有效的保证了数据的完整和安全性。
二:主要选项:
1、my.cnf配置:
Master:
[[email protected] ~]# grep -v ‘#‘ /etc/mysql/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
datadir = /data/mydata
innodb_file_per_table = 1
log-bin=/data/binlogs/master-bin
binlog_format=ROW
以下是主要的增该内容:
server-id= 100
log-slave-updates=True #slave更新是否记入日志
master-info-repository=TABLE
relay-log-info-repository=TABLE #此两项为打开从服务器崩溃二进制日志功能,信息记录在事物表而不是保存在文件
sync-master-info=1 #值为1确保信息不会丢失
slave-parallel-threads=2 #同时启动多少个复制线程,最多与要复制的数据库数量相等即可
binlog-checksum=CRC32 #效验码
master-verify-checksum=1 #启动主服务器效验
slave-sql-verify-checksum=1 #启动从服务器效验
binlog-rows-query-log-events=1 #用于在二进制日志详细记录事件相关的信息,可降低故障排除的复杂度;
report-port=3306 #请求的主机端口
report-host=node4.a.com #请求的主机名,必须是主机名全称且DNS可解析
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
验证: 查看当前主机的二进制文件进度:
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000008 | 446 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
查看当前的二进制文件详细的信息:
mysql> show binlog events in ‘master-bin.000008‘;
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
| master-bin.000008 | 4 | Format_desc | 100 | 248 | Server ver: 10.0.10-MariaDB-log, Binlog ver: 4 |
| master-bin.000008 | 248 | Gtid_list | 100 | 277 | [] |
| master-bin.000008 | 277 | Binlog_checkpoint | 100 | 321 | master-bin.000008 |
| master-bin.000008 | 321 | Gtid | 100 | 363 | GTID 0-100-1 |
| master-bin.000008 | 363 | Query | 100 | 446 | create database sd |
+-------------------+-----+-------------------+-----------+-------------+------------------------------------------------+
5 rows in set (0.00 sec)
2、创建复制账号并授权:
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘jack‘@‘192.168.%.%‘ IDENTIFIED BY ‘123456‘;
mysql> FLUSH PRIVILEGES;
Slave:
[[email protected] ~]# grep -v ‘#‘ /etc/mysql/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = ON
datadir = /data/mydata
log-bin=/data/binlogs/master-bin
server-id= 1
以下为重点配置部分:
binlog-format=ROW
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-threads=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=node5.a.com
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
连接到主服务器:
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.10.204‘,MASTER_USER=‘jack‘,MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘master-bin.000013‘,MASTER_LOG_POS=379;
mysql> start slave;
然后确认复制进程是否已经成功启动:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.204
Master_User: jack
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000013
Read_Master_Log_Pos: 725
Relay_Log_File: node5-relay-bin.000002
Relay_Log_Pos: 714
Relay_Master_Log_File: master-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
同步成功后将从进程关闭:
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.10.204‘,MASTER_USER=‘jack‘,MASTER_PASSWORD=‘123456‘,MASTER_USE_GTID=current_pos;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.204
Master_User: jack
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000013
Read_Master_Log_Pos: 725
Relay_Log_File: node5-relay-bin.000002
Relay_Log_Pos: 714
Relay_Master_Log_File: master-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
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: 725
Relay_Log_Space: 1015
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: 100
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Current_Pos
Gtid_IO_Pos: 0-100-4
已经启动完成: