使用percona xtradb cluster的IST(Incremental State Transfer)特性添加新节点,防止新节点加入时使用SST(State SnapShop Transfer)传输全量数据
环境:两台虚拟机
192.168.0.48 node1
192.168.0.49 新加入节点
注意事项:测试环境建议关掉iptables,selinux
1.两个节点都先下载并安装好xtrabackup
shell > rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm #安装epel源
shell > yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes socat nc libaio rsync -y #安装需要的依赖包
shell > wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.2.11/binary/redhat/6/x86_64/percona-xtrabackup-2.2.11-1.el6.x86_64.rpm #下载xtrabackup
shell > rpm -ivh percona-xtrabackup-2.2.11-1.el6.x86_64.rpm
2.下载安装Percona-XtraDB-Cluster
shell > wget https://www.percona.com/downloads/Percona-XtraDB-Cluster-56/Percona-XtraDB-Cluster-5.6.24-25.11/binary/tarball/Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz #下载PXC
3.先把两个节点的PXC都按照安装官方mysql的步骤安装上去并初始化好实例
shell > tar xf Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64.tar.gz -C /usr/local/services/
shell > cd /usr/local/services/
shell > ln -s Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64/ mysql
shell > cd mysql
shell > groupadd mysql
shell > useradd -r -g mysql mysql
shell > chown -R mysql .
shell > chgrp -R mysql .
shell > cp support-files/my-default.cnf /etc/my.cnf
shell > mkdir -p /data/mysql/data
shell >
shell > ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/services/mysql --datadir=/data/mysql/data/
#如果在初始化的时候报libssl.so.6和libcrypto.so.6两个动态库文件不存在的,就做下链接:
shell > ln -s /usr/lib64/libssl.so /usr/lib64/libssl.so.6
shell > ln -s /usr/lib64/libcrypto.so /usr/lib64/libcrypto.so.6
shell > chown -R root .
shell > cp support-files/mysql.server /etc/init.d/mysqld
shell > chmod 755 /etc/init.d/mysqld
shell > vim /etc/init.d/mysqld
在命令模式下修改下basedir的路径:
% s/usr\/local\/Percona-XtraDB-Cluster-5.6.24-rel72.2-25.11..Linux.x86_64/usr\/local\/services\/mysql/g
4.配置节点1的PXC参数:
/etc/my.cnf添加如下内容:
[mysqld]
basedir = /usr/local/services/mysql
datadir = /data/mysql/data
binlog_format=ROW #binlog格式必须为row
default_storage_engine=InnoDB #暂时不支持其他存储引擎,只支持innodb,当然可以支持myisam,需要另外参数打开
innodb_autoinc_lock_mode=2 #自增锁的优化
log_bin=mysql-bin
server-id=483306
#并在[mysqld]段落添如下PXC相关参数:
wsrep_provider=/usr/local/services/mysql/lib/libgalera_smm.so #库文件
wsrep_cluster_address=gcomm://192.168.0.48,192.168.0.49 #节点中所有ip
wsrep_node_address=192.168.0.48 #节点的ip
wsrep_slave_threads=2 #开启的复制线程数,cpu核数*2
wsrep_cluster_name=pxc-xiaoboluo #集群名字
wsrep_sst_auth=sst:xiaoboluo #sst模式需要的用户名和密码
wsrep_sst_method=xtrabackup-v2 #采用什么方式复制数据。还支持mysqldump,rsync
5.启动,进行授权操作,对于第一个节点必须以特殊方式启动,如下:
查看启动选项:shell > /etc/init.d/mysqld --help
Usage: mysql {start|stop|restart|restart-bootstrap|reload|force-reload|status|bootstrap-pxc} [ MySQL (Percona XtraDB Cluster) options ]
启动:
shell > /etc/init.d/mysqld bootstrap-pxc
Bootstrapping PXC (Percona XtraDB Cluster) SUCCESS! MySQL (Percona XtraDB Cluster) running (37791)
进行查看,可以发现启动两个端口:
shell > netstat -ntupl |grep mysqld
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 37791/mysqld
tcp 0 0 :::3306 :::* LISTEN 37791/mysqld
6.对复制帐号进行授权,并修改root密码,推荐使用grant方式
mysql登录:
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON * . * TO 'sst'@'localhost' IDENTIFIED BY 'xiaoboluo';
Query OK, 0 rows affected (0.05 sec)
mysql> grant all on *.* to root@'192.168.0.%' identified by 'password' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by 'password' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
7.到这里我们的第一个节点就搞定了,把新节点用传统复制的方式添加进集群是重点(如果是集群是新搭建的,或者说直接使用SST的方式加入新节点,那么新节点的配置就直接按照前面的主节点配置来就可以了,只是把wsrep_node_address改成对应节点的IP即可,而对于已经运行了一段事件的集群,新加入节点使用SST传送全量数据的方式同步的代价比较高,所以下面讨论一个IST方式加入新节点同步数据的方式):
1)、在node1上创建一个复制帐号:
mysql> grant replication slave on *.* to 'repl'@'192.168.0.%' identified by 'repl';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2)、在node1上导出全量备份数据,为了方便使用mysqldump来导出备份,并scp到新节点上:
shell > mysqldump --single-transaction --master-data=2 -u root -p'password' -A > db.sql
查看到db.sql文件中的change master语句,后边要使用:
shell > grep '\-\- CHANGE MASTER' db.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120;
shell > scp db.sql 192.168.0.49:/tmp
3)、在新节点上先添加如下配置参数到/etc/my.cnf中,先不要添加PXC相关的配置项:
basedir = /usr/local/services/mysql
datadir = /data/mysql/data
binlog_format=ROW #binlog格式必须为row
default_storage_engine=InnoDB #暂时不支持其他存储引擎,只支持innodb,当然可以支持myisam,需要另外参数打开
innodb_autoinc_lock_mode=2 #自增锁的优化
log_bin=mysql-bin
server-id=493306
4)、把新节点的实例启动起来,修改root密码,并导入db.sql:
shell > service mysqld start
Starting MySQL (Percona XtraDB Cluster).. SUCCESS!
mysql> grant all on *.* to root@'192.168.0.%' identified by 'password' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by 'password' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql > source /tmp/db.sql;
5)、使用change master语句开启复制(把前面db.sql文件中的change master语句补全,并在新节点上执行):
mysql > CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_HOST='192.168.0.48',MASTER_PORT=3306;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.48
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: test_web2-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
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: 120
Relay_Log_Space: 460
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: 483306
Master_UUID: bbba431d-bff5-11e5-b143-000c291c9bd0
Master_Info_File: /data/mysql/data/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:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
6)、同步正常之后,到主库上去创建一个库,表,并写入几条测试数据,然后到从库上看看同步成功没有(因为这里是测试环境,node1节点上没有数据,所以需要搞点测试数据,如果线上环境这步骤可以省略,直接在上一步骤同步建立完之后stop slave,再看一下show slave status记下Relay_Master_Log_File和Exec_Master_Log_Pos的二进制日志坐标即可进入跳过这个步骤)
在node1上创建测试数据
mysql> create database xiaoboluo;
Query OK, 1 row affected (0.02 sec)
mysql> use xiaoboluo
Database changed
mysql> create table test_xiaoboluo(id int unsigned not null auto_increment primary key,test varchar(20));
Query OK, 0 rows affected (0.43 sec)
mysql> insert into test_xiaoboluo(test) values('test1'),('test2'),('test3'),('test4');
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test_xiaoboluo;
+----+-------+
| id | test |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
+----+-------+
4 rows in set (0.00 sec)
mysql>
到新节点上去查看下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| xiaoboluo |
+--------------------+
5 rows in set (0.00 sec)
mysql> use xiaoboluo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test_xiaoboluo;
+----+-------+
| id | test |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
+----+-------+
4 rows in set (0.00 sec)
7)、在新节点上停掉同步,并查看show slave status\G中的Relay_Master_Log_File和Exec_Master_Log_Pos的二进制日志坐标,记下它后边有用,然后停掉新节点的mysqld实例:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.48
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 662
Relay_Log_File: test_web2-relay-bin.000002
Relay_Log_Pos: 825
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 662
Relay_Log_Space: 1002
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: NULL
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: 483306
Master_UUID: bbba431d-bff5-11e5-b143-000c291c9bd0
Master_Info_File: /data/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
已经执行到的日志坐标是:
Relay_Master_Log_File: mysql-bin.000001
Exec_Master_Log_Pos: 662
停掉mysqld实例:
shell > service mysqld stop
Shutting down MySQL (Percona XtraDB Cluster).. SUCCESS!
8)、到node1节点上flush logs以下,然后使用新节点上找到的同步坐标查找xid,这个xid就是新节点在gcache中需要从什么位置开始同步数据:
shell > cd /data/mysql/data/
shell > mysqlbinlog -v mysql-bin.000001 |grep -i xid
#160121 13:11:33 server id 483306 end_log_pos 662 CRC32 0x9e460154 Xid = 8
可以看到,在主库上对应二进制日志坐标的xid=8,记下这个数字,后边有用,顺序在node1上查看下这个xid是否在gcache缓存中有效:
mysql> show status like '%wsrep_local_cached_downto%';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| wsrep_local_cached_downto | 6 |
+---------------------------+-------+
1 row in set (0.00 sec)
发现从6开始,说明xid为8有效,继续后面的步骤
9)、查看node1上的grastate.dat文件:
shell > cat grastate.dat
# GALERA saved state
version: 2.1
uuid: bd355b13-bff5-11e5-bed5-0f40dadab349
seqno: -1
cert_index:
seqno为-1就表示这个节点已经在集群中,把这个文件复制到新节点的datadir目录下,并修改为mysql用户属主:
shell > scp grastate.dat 192.168.0.49:/data/mysql/data/
shell > chown mysql.mysql /data/mysql/data/grastate.dat
10)、修改新节点上/data/mysql/data/grastate.dat文件中的seqno为前面找到的xid:
shell > cat /data/mysql/data/grastate.dat
# GALERA saved state
version: 2.1
uuid: bd355b13-bff5-11e5-bed5-0f40dadab349
seqno: 8
cert_index:
11)、此时把PXC参数全部加进新节点的my.cnf中:
#并在[mysqld]段落添如下PXC相关参数:
wsrep_provider=/usr/local/services/mysql/lib/libgalera_smm.so #库文件
wsrep_cluster_address=gcomm://192.168.0.48,192.168.0.49 #节点中所有ip
wsrep_node_address=192.168.0.49 #节点的ip
wsrep_slave_threads=2 #开启的复制线程数,cpu核数*2
wsrep_cluster_name=pxc-xiaoboluo #集群名字
wsrep_sst_auth=sst:xiaoboluo #sst模式需要的用户名和密码
wsrep_sst_method=xtrabackup-v2 #采用什么方式复制数据。还支持mysqldump,rsync
12)、按照常规启动新节点,然后查看错误日志是否有错误,没有错误就在集群中的两个节点上各写一条数据, 验证下数据是否都能相互同步成功
shell > service mysqld start
Starting MySQL (Percona XtraDB Cluster)........... SUCCESS!
在node1节点的xiaoboluo库test_xiaoboluo表中写入一行数据:
mysql> insert into test_xiaoboluo(test) values('test5');
Query OK, 1 row affected (0.00 sec)
在新节点上查询数据:
mysql> select * from test_xiaoboluo;
+----+-------+
| id | test |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
+----+-------+
5 rows in set (0.00 sec)
已经同步过来了,在新节点上插入一行数据:
mysql> insert into test_xiaoboluo(test) values('test6');
Query OK, 1 row affected (0.03 sec)
然后去Node1上查询:
mysql> select * from test_xiaoboluo;
+----+-------+
| id | test |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
+----+-------+
6 rows in set (0.00 sec)
也已经同步过来了,至此,使用IST方式加入PXC新节点的方式目的达到。且同步瞬间已经正常了,新节点上的slave信息可以不需要了,直接stop slave;reset slave all即可。