mysql集群-基于GTID的主从复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql集群-基于GTID的主从复制相关的知识,希望对你有一定的参考价值。
mysql集群采用MHA+mycat+mysql主从复制+mycat-web监控构成由于篇幅过长 将分步骤发布
这里因为我电脑内存限制 没有做mycat的高可用 可以使用HAporxy+Keppalive实现mycat的高可用这样集群中就不存在单点故障
简介
基于GTID的复制是mysql 5.6后新增的复制方式.
GTID (global transaction identifier) 即全局事务ID, 保证了在每个在主库上提交的事务在集群中有一个唯一的ID.
在原来基于日志的复制中, 从库需要告知主库要从哪个偏移量进行增量同步, 如果指定错误会造成数据的遗漏, 从而造成数据的不一致.
而基于GTID的复制中, 从库会告知主库已经执行的事务的GTID的值, 然后主库会将所有未执行的事务的GTID的列表返回给从库. 并且可以保证同一个事务只在指定的从库执行一次.
1.mysql配置基于gtid主从复制
安装脚本 id=`ifconfig eth0 | awk -F "[ :]+" 'NR==2 {print $4}' | cut -d . -f 4` useradd -s /sbin/nologin mysql -M cd /application/tools/ tar xf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz ln -s /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/ /application/mysql && echo success cd /application mkdir /application/mysql/{log,data,tmp} -p chown -R mysql:mysql mysql cd /application/mysql chown -R mysql:mysql ./* cat << EOF >/etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock [mysqld] server_id= $id port = 3306 user = mysql character-set-server = utf8mb4 default_storage_engine = innodb log_timestamps = SYSTEM socket = /tmp/mysql.sock basedir = /application/mysql datadir = /application/mysql/data pid-file = /application/mysql/data/mysql.pid max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 #####====================================[innodb]============================== innodb_buffer_pool_size = 1024M innodb_file_per_table = 1 innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_purge_threads = 2 innodb_flush_log_at_trx_commit = 1 innodb_log_file_size = 512M innodb_log_files_in_group = 2 innodb_log_buffer_size = 16M innodb_max_dirty_pages_pct = 80 innodb_lock_wait_timeout = 30 innodb_data_file_path=ibdata1:1024M:autoextend innodb_undo_tablespaces=3 ######====================================[log]============================== log_error = /application/mysql/log/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /application/mysql/log/mysql-slow.log sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES log-bin=mysql-bin ##################slave设置参数################## relay_log_purge=0 ######====================================[gtid]============================ gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 EOF cd /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/bin ./mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp && echo success cp /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysql chkconfig --add mysql chkconfig mysql on service mysql start cat << EOF >>/etc/profile #for mysql PATH=$PATH:/application/mysql/bin EOF source /etc/profile && echo success password=`grep -i password /application/mysql/log/mysql-error.log | awk -F '[ :]+' '{print $14}' | head -1` mysqladmin -uroot -p$password password 123456
创建复制用户 在master1中创建一个从机中可以登录的MySQL用户
GRANT REPLICATION SLAVE ON *.* TO 'mysql01'@'172.16.1.%' IDENTIFIED BY 'mysql01'; FLUSH PRIVILEGES; master2 CHANGE MASTER TO MASTER_HOST='172.16.1.103', MASTER_USER='mysql01', MASTER_PASSWORD='mysql01', MASTER_AUTO_POSITION=1; slave CHANGE MASTER TO MASTER_HOST='172.16.1.103', MASTER_USER='mysql01', MASTER_PASSWORD='mysql01', MASTER_AUTO_POSITION=1;
开启复制
master2 START SLAVE; slave START SLAVE;
查看主从复制是否配置成功
master2 SHOW SLAVE STATUSG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.1.103 Master_User: mysql01 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1200 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 472 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes slave SHOW SLAVE STATUSG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.1.103 Master_User: mysql01 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1200 Relay_Log_File: slave-relay-bin.000003 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
以上是关于mysql集群-基于GTID的主从复制的主要内容,如果未能解决你的问题,请参考以下文章