PXC集群添加新节点

本篇文章是在二进制包方式安装 PXC 集群 这篇博客的基础上操作的。
PXC 集群中已经存在 db-node1/2/3 这 3 个节点,我们把 db-node4 节点作为新节点并添加到 PXC 集群中。
PXC 添加新节点的方式有两种:一是使用 SST(State Snapshot Transfer)全量传输方式,二是使用 IST(Incremental State Transfer )利用 MySQL 复制和备份的特性增量传输数据方式添加新节点。我们选择使用 IST 方式添加新节点。

1.环境信息介绍

版本信息:

名称版本
系统 CentOS Linux release 7.2.1511 (Core)
Percona-XtraDB-Cluster 5.7.21

主机信息:

主机名IP
db-node1 192.168.199.230
db-node2 192.168.199.231
db-node3 192.168.199.131
db-node4 192.168.199.212

2.安装依赖

yum install -y perl-DBD-MySQL perl-IO-Socket-SSL socat nc  percona-xtrabackup-24.x86_64

3.下载 PXC

# wget -P /usr/local/ https://www.percona.com/downloads/Percona-XtraDB-Cluster-57/Percona-XtraDB-Cluster-5.7.21-29.26/binary/tarball/Percona-XtraDB-Cluster-5.7.21-rel20-29.26.1.Linux.x86_64.ssl101.tar.gz

解压并创建软链接:

# tar zxf Percona-XtraDB-Cluster-5.7.21-rel20-29.26.1.Linux.x86_64.ssl101.tar.gz -C /opt/mysql/
# ln -sv /opt/mysql/Percona-XtraDB-Cluster-5.7.21-rel20-29.26.1.Linux.x86_64.ssl101 /usr/local/mysql

4.创建数据库相关目录

# mkdir -pv /data/mysql/mysql3306/{data,logs,tmp} 

修改权限:

# chown -R mysql.mysql /usr/local/mysql/
# chown -R mysql.mysql /data

5.创建配置文件

可以从其他节点拷贝过来一份配置文件,记得修改 server-id 参数,里面不要包含 PXC 的配置信息

# cat > /etc/my.cnf << EOF
###### base ######
#my.cnf
[client]
port = 3306
socket = /tmp/mysql3306.sock

[mysql]
prompt="\\u@\\h:\\p [\\d]> " 
no-auto-rehash

[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
tmpdir = /data/mysql/mysql3306/tmp
port = 3306
socket = /tmp/mysql3306.sock
event_scheduler = 0

#wsrep_debug=on
#wsrep_provider='debug=on'

#timeout
interactive_timeout = 300
wait_timeout = 300

#character set
character-set-server = utf8

open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
lower_case_table_names =1

###### GTID ######
gtid-mode = on
enforce-gtid-consistency=1

###### symi replication ######
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
#rpl_semi_sync_slave_enabled=1

####### slow log ######
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1

####### binlog ######
binlog_format = row
server-id = 2123306
log-bin = /data/mysql/mysql3306/logs/mysql-bin
max_binlog_size = 256M
sync_binlog = 0
expire_logs_days = 10
#procedure 
log_bin_trust_function_creators=1

####### relay log ######
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay_log_recovery = 1
relay-log=relay-bin
relay-log-index = relay-bin.index
log_slave_updates = ON
#slave-skip-errors=1032,1053,1062
#skip-grant-tables

####### buffers & cache ######
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 200
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M

#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

#innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT

6.初始化数据库

# cd /usr/local/mysql
# ./bin/mysqld --defaults-file=/etc/my.cnf   --initialize

二、创建新旧节点主从关系

我们从 PXC 集群中先选择一个节点做 master,这里选择 db-node3 节点做为 master。

1.创建复制账号

在 db-node3 上创建复制账号

mysql> grant  replication  slave  on *.*  to 'repl'@'%'  identified   by  'unixfbi';

2.备份数据

在 db-node3 上执行

# innobackupex --defaults-file=/etc/my.cnf --user=root  --password=unixfbi /data/

3.拷贝备份文件

拷贝到 db-node4 上

# scp -r /data/2018-03-26_17-51-52 root@192.168.199.212:/data/

4.恢复备份

在 db-node4 上执行

# innobackupex --apply-log /data/2018-03-26_17-51-52/     
# innobackupex --copy-back /data/2018-03-26_17-51-52/  
# chown -R mysql.mysql /data/mysql/mysql3306/data/

启动数据库:

# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf  &

5.创建主从关系

root@localhost:mysql3306.sock [(none)]> CHANGE MASTER TO 
MASTER_HOST='192.168.199.131',
MASTER_USER='repl',
MASTER_PASSWORD='unixfbi',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;

6.查看复制状态

root@localhost:mysql3306.sock [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.199.131
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 740
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 913
        Relay_Master_Log_File: mysql-bin.000007
             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: 740
              Relay_Log_Space: 1114
              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: 1313306
                  Master_UUID: 72b23ebf-2b53-11e8-9413-2c27d72e9d73
             Master_Info_File: /data/mysql/mysql3306/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 959932a2-d46a-ee17-6277-717d01a0abd6:12-13
            Executed_Gtid_Set: 959932a2-d46a-ee17-6277-717d01a0abd6:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

可以看出复制是成功的。

三、开始添加新节点到 PXC 集群

1.停止复制并确认 binlog 位置

root@localhost:mysql3306.sock [(none)]> stop slave;
root@localhost:mysql3306.sock [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.199.131
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 740
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 913
        Relay_Master_Log_File: mysql-bin.000007
             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: 740
              Relay_Log_Space: 1114
              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: 1313306
                  Master_UUID: 72b23ebf-2b53-11e8-9413-2c27d72e9d73
             Master_Info_File: /data/mysql/mysql3306/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: 959932a2-d46a-ee17-6277-717d01a0abd6:12-13
            Executed_Gtid_Set: 959932a2-d46a-ee17-6277-717d01a0abd6:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

根据以上状态信息,可以知道已经执行到的日志坐标是:

 Relay_Master_Log_File: mysql-bin.000007
 Exec_Master_Log_Pos: 740

2.停止 Slave 实例

# pkill mysql 
# ps aux|grep mysql

3.查找 Xid 信息

我们可以在 db-node3 节点上根据同步的日志文件位置查找 Xid,这个 xid 就是新节点在 gcache 中需要从什么位置开始同步数据:

# mysqlbinlog -v    /data/mysql/mysql3306/logs/mysql-bin.000007 |grep 740|grep Xid
#180326 18:35:08 server id 1313306  end_log_pos 740 CRC32 0x6c6fc95e    Xid = 13

可以看出,在主库 db-node3 上位置 740 对应的 Xid 为 13。

在 db-node3 节点上,查看这个 Xid 是否有效

root@localhost [(none)]>show status like '%wsrep_local_cached_downto%';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| wsrep_local_cached_downto | 10    |
+---------------------------+-------+
1 row in set (0.00 sec)

发现是从 10 开始,说明 Xid=13 是有效的。

4.拷贝 grastate.dat 文件

把 db-node3 上的 grastate.dat 文件拷贝到 db-node4 节点上
我们先查看一下这个文件的内容:

# cat /data/mysql/mysql3306/data/grastate.dat 
# GALERA saved state
version: 2.1
uuid:    6a66cd5d-2b95-11e8-9d88-8e82fe5f5429
seqno:   -1
safe_to_bootstrap: 0

seqno: -1 表示这个节点已经在集群中。
把这个文件复制到 db-node4 节点的 datadir 目录下,记得修改文件用户属主:

# scp grastate.dat root@192.168.199.212:/data/mysql/mysql3306/data/

在 db-node4 上修改属主:

# chown mysql.mysql /data/mysql/mysql3306/data/grastate.dat 

5.修改 grastate.dat 文件

修改新节点 db-node4 上/data/mysql/mysql3306/data/grastate.dat 文件中的 seqno 为我们前面找到的 Xid 值 13

6.新节点配置 PXC 参数

在新节点 db-node4 上/etc/my.cnf 文件中[mysqld]段下添加如下配置参数

# PXC
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
wsrep_cluster_name=pxc-cluster
wsrep_cluster_address=gcomm://192.168.199.230,192.168.199.231,192.168.199.131,192.168.199.212  #所有节点 IP
wsrep_node_name=pxc4
wsrep_node_address=192.168.199.212
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sstuser:unixfbi
pxc_strict_mode=ENFORCING
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2

wsrep_cluster_address 参数把新旧节点 IP 都配置上。

7.启动新节点

启动 db-node4 节点数据库实例

# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf  &

8.查看新节点 PXC 状态

root@localhost:mysql3306.sock [(none)]> show status like 'wsrep%';
+----------------------------------+-------------------------------------------------------------------------------------+
| Variable_name                    | Value                                                                               |
+----------------------------------+-------------------------------------------------------------------------------------+
| wsrep_local_state_uuid           | 6a66cd5d-2b95-11e8-9d88-8e82fe5f5429                                                |
| wsrep_protocol_version           | 8                                                                                   |
| wsrep_last_applied               | 14                                                                                  |
| wsrep_last_committed             | 14                                                                                  |
| wsrep_replicated                 | 0                                                                                   |
| wsrep_replicated_bytes           | 0                                                                                   |
| wsrep_repl_keys                  | 0                                                                                   |
| wsrep_repl_keys_bytes            | 0                                                                                   |
| wsrep_repl_data_bytes            | 0                                                                                   |
| wsrep_repl_other_bytes           | 0                                                                                   |
| wsrep_received                   | 3                                                                                   |
| wsrep_received_bytes             | 364                                                                                 |
| wsrep_local_commits              | 0                                                                                   |
| wsrep_local_cert_failures        | 0                                                                                   |
| wsrep_local_replays              | 0                                                                                   |
| wsrep_local_send_queue           | 0                                                                                   |
| wsrep_local_send_queue_max       | 1                                                                                   |
| wsrep_local_send_queue_min       | 0                                                                                   |
| wsrep_local_send_queue_avg       | 0.000000                                                                            |
| wsrep_local_recv_queue           | 0                                                                                   |
| wsrep_local_recv_queue_max       | 1                                                                                   |
| wsrep_local_recv_queue_min       | 0                                                                                   |
| wsrep_local_recv_queue_avg       | 0.000000                                                                            |
| wsrep_local_cached_downto        | 0                                                                                   |
| wsrep_flow_control_paused_ns     | 0                                                                                   |
| wsrep_flow_control_paused        | 0.000000                                                                            |
| wsrep_flow_control_sent          | 0                                                                                   |
| wsrep_flow_control_recv          | 0                                                                                   |
| wsrep_flow_control_interval      | [ 200, 200 ]                                                                        |
| wsrep_flow_control_interval_low  | 200                                                                                 |
| wsrep_flow_control_interval_high | 200                                                                                 |
| wsrep_flow_control_status        | OFF                                                                                 |
| wsrep_cert_deps_distance         | 0.000000                                                                            |
| wsrep_apply_oooe                 | 0.000000                                                                            |
| wsrep_apply_oool                 | 0.000000                                                                            |
| wsrep_apply_window               | 1.000000                                                                            |
| wsrep_commit_oooe                | 0.000000                                                                            |
| wsrep_commit_oool                | 0.000000                                                                            |
| wsrep_commit_window              | 1.000000                                                                            |
| wsrep_local_state                | 4                                                                                   |
| wsrep_local_state_comment        | Synced                                                                              |
| wsrep_cert_index_size            | 0                                                                                   |
| wsrep_cert_bucket_count          | 22                                                                                  |
| wsrep_gcache_pool_size           | 1528                                                                                |
| wsrep_causal_reads               | 0                                                                                   |
| wsrep_cert_interval              | 0.000000                                                                            |
| wsrep_ist_receive_status         |                                                                                     |
| wsrep_ist_receive_seqno_start    | 0                                                                                   |
| wsrep_ist_receive_seqno_current  | 0                                                                                   |
| wsrep_ist_receive_seqno_end      | 0                                                                                   |
| wsrep_incoming_addresses         | 192.168.199.230:3306,192.168.199.231:3306,192.168.199.131:3306,192.168.199.212:3306 |
| wsrep_desync_count               | 0                                                                                   |
| wsrep_evs_delayed                |                                                                                     |
| wsrep_evs_evict_list             |                                                                                     |
| wsrep_evs_repl_latency           | 0/0/0/0/0                                                                           |
| wsrep_evs_state                  | OPERATIONAL                                                                         |
| wsrep_gcomm_uuid                 | d3ee4765-316a-11e8-bb72-5f7810a9d7dc                                                |
| wsrep_cluster_conf_id            | 16                                                                                  |
| wsrep_cluster_size               | 4                                                                                   |
| wsrep_cluster_state_uuid         | 6a66cd5d-2b95-11e8-9d88-8e82fe5f5429                                                |
| wsrep_cluster_status             | Primary                                                                             |
| wsrep_connected                  | ON                                                                                  |
| wsrep_local_bf_aborts            | 0                                                                                   |
| wsrep_local_index                | 3                                                                                   |
| wsrep_provider_name              | Galera                                                                              |
| wsrep_provider_vendor            | Codership Oy <info@codership.com>                                                   |
| wsrep_provider_version           | 3.26(r)                                                                             |
| wsrep_ready                      | ON                                                                                  |
+----------------------------------+-------------------------------------------------------------------------------------+
68 rows in set (0.00 sec)

其实在整个 PXC 集群中,上面的信息显示应该是同样的内容。

9.测试新节点

在新节点插入数据:

root@localhost:mysql3306.sock [(none)]> insert into unixfbi.example values(5,'MySQL');
Query OK, 1 row affected (0.00 sec)

在其他旧节点查看:

root@localhost [(none)]>select * from unixfbi.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
|       1 | percona1  |
|       2 | unixfbi   |
|       3 | unixfbi   |
|       4 | unixfbi   |
|       5 | MySQL     |
+---------+-----------+
5 rows in set (0.01 sec)

在其他节点插入数据:

root@localhost [(none)]> insert into unixfbi.example values(6,'China');
Query OK, 1 row affected (0.00 sec)

在新节点查看:

root@localhost:mysql3306.sock [(none)]> select * from unixfbi.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
|       1 | percona1  |
|       2 | unixfbi   |
|       3 | unixfbi   |
|       4 | unixfbi   |
|       5 | MySQL     |
|       6 | China     |
+---------+-----------+
6 rows in set (0.00 sec)

发现数据同步正常。

使用 IST 方式加入 PXC 集群是没有问题的。另外,新节点上的 slave 信息没有用了,可以使用 reset slave all;直接清除;

10.新节点清除 slave 信息

新节点上的 slave 信息没有用了,可以使用 reset slave all;直接清除;

root@localhost:mysql3306.sock [(none)]> reset slave all;
Query OK, 0 rows affected (0.15 sec)

root@localhost:mysql3306.sock [(none)]> show slave status\G
Empty set (0.00 sec)

11.旧节点添加新节点 IP

修改/etc/my.cnf 配置文件中如下配置,把新节点 db-node4 的 IP192.168.199.212 添加上去。

wsrep_cluster_address=gcomm://192.168.199.230,192.168.199.231,192.168.199.131,192.168.199.212  # 所有节点的 IP

参考文档

https://www.percona.com/doc/percona-xtradb-cluster/5.7/index.html
https://www.percona.com/doc/percona-xtradb-cluster/5.7/add-node.html
http://www.cnblogs.com/xiaoboluo768/p/5147993.html
https://blog.csdn.net/lijingkuan/article/details/61938618

posted @ 2018-12-20 17:23  workdsz  阅读(898)  评论(0)    收藏  举报