MySQL高可用PXC(Percona XtraDB Cluster)

概述

PXC介绍
  • 是基于Galera的MySQL高可用集群解决方案

  • Galera Cluster是Codership公司开发的一套免费开源的高可用方案

  • PXC集群主要由两部分组成:Percona Server with XtraDB和Write Set Replication patches(同步、多主复制插件)

  • 官网http://galeracluster.com

PXC特点
  • 数据强一致性、无同步延迟

  • 没有主从切换操作,无需使用虚拟IP

  • 支持InnoDB存储引擎

  • 多线程复制

  • 部署使用简单

  • 支持节点自动加入,无需手动拷贝数据

相应端口
  • 3306:数据库服务端口

  • 4444:SST端口

  • 4567:集群通信端口

  • 4568:IST端口

  • SST:State Snapshot Transfer 全量同步

  • IST:Incremental State Transfer 增量同步

部署PXC

服务器角色
  • 三台相互独立的mysql服务器:192.168.1.11、192.168.1.12、192.168.1.13
初始环境准备
  • 配置服务器的名称解析(三台服务器分别执行以下命令)
[root@mysql1 ~]# for i in {1..3}
> do
> echo -e "192.168.1.1$i\tmysql$i" >> /etc/hosts
> done
  • 准备yum源

  • 在三台服务器上面分别安装软件包

[root@mysql1 ~]# yum clean all
[root@mysql1 ~]# yum remove -y mysql-community-*
[root@mysql1 ~]# yum install -y qpress-1.1-14.11 Percona-XtraDB-Cluster-*
配置服务
  • 分别修改3台服务器的mysqld.cnf文件(server-id修改为不同数值)
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
... ...
[mysqld]
server-id=11
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7
... ...
  • 分别修改3台服务器的mysqld_safe.cnf (使用默认配置即可)
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket   = /var/lib/mysql/mysql.sock
nice     = 0
  • 分别修改3台服务器的wsrep.cnf(wsrep_node_address 需要更改为本节点IP)
[root@mysql1 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
# 修改以下内容:
wsrep_cluster_address=gcomm://192.168.1.11,192.168.1.12,192.168.1.13  # 集群成员
wsrep_node_address=192.168.1.11   # 本节点IP地址
wsrep_cluster_name=pxc-cluster    # 集群名
wsrep_node_name=mysql1            # 本节点名
wsrep_sst_auth="sstuser:[email protected]"   # SST数据同步授权用户及密码
启动服务
  • 启动集群服务
# 首次启动服务时间比较长
[root@mysql1 ~]# systemctl  start [email protected]
[root@mysql1 ~]# grep password /var/log/mysqld.log
[root@mysql1 ~]# mysql -uroot -p'!Yo.e(qv:0pH'
mysql> alter user  root@'localhost' identified by '[email protected]';
mysql> grant reload, lock tables,replication client,process on *.*  to
    -> sstuser@'localhost' identified by '[email protected]';
Query OK, 0 rows affected, 1 warning (0.20 sec)
  • 启动其他节点,其他节点将会自动同步第一台服务器的数据(授权用户等)
[root@mysql2 ~]# systemctl start mysql
[root@mysql2 ~]# netstat -utnlp  | grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN      18875/mysqld
[root@mysql2 ~]# netstat -utnlp  | grep :4567
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      18875/mysqld

[root@mysql3 ~]# systemctl  start mysql
[root@mysql3 ~]# netstat -utnlp  | grep :3306
tcp6       0      0 :::3306                 :::*                    LISTEN      18465/mysqld
[root@mysql3 ~]# netstat -utnlp  | grep :4567
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      18465/mysqld

测试配置

查看集群信息
  • 在任意数据库服务器查看信息
[root@mysql2 ~]# mysql -uroot [email protected]
mysql> show status like "%wsrep%";  # 找到以下信息
| wsrep_incoming_addresses         | 192.168.1.11:3306,192.168.1.12:3306,192.168.1.13:3306 |
| wsrep_cluster_weight             | 3                                                     |
| wsrep_cluster_status             | Primary                                               |
| wsrep_connected                  | ON                                                    |
| wsrep_ready                      | ON                                                    |
  • 访问集群,在任意数据库服务器存取数据
mysql> grant all on db1.* to dbuser1@'%' identified by '[email protected]';
Query OK, 0 rows affected, 1 warning (0.48 sec)

mysql> show grants for dbuser1;
+--------------------------------------------------+
| Grants for dbuser1@%                             |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'dbuser1'@'%'              |
| GRANT ALL PRIVILEGES ON `db1`.* TO 'dbuser1'@'%' |
+--------------------------------------------------+
2 rows in set (0.00 sec)
  • 客户端连接集群任意数据库服务器存取数据
[root@node10 ~]# mysql -h192.168.1.11 -udbuser1 [email protected]
mysql> create database db1 default charset utf8mb4;
Query OK, 1 row affected (0.57 sec)

mysql> create table db1.students(id int primary key auto_increment, name varchar(20));
Query OK, 0 rows affected (0.92 sec)

mysql> insert into db1.students(name) values ('tom');
Query OK, 1 row affected (0.15 sec)
  • 客户端连接各数据库服务器查看数据
[root@node10 ~]# mysql -h192.168.1.12 -udbuser1 [email protected]
mysql> select * from db1.students;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
+----+------+
1 row in set (0.00 sec)

[root@node10 ~]# mysql -h192.168.1.13 -udbuser1 [email protected]
mysql> select * from db1.students;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
+----+------+
1 row in set (0.00 sec)
测试故障自动恢复
  • 停止3台服务器的任意一台主机的数据库服务都不会影响数据的存取。
[root@mysql1 ~]# systemctl stop [email protected]
[root@mysql2 ~]# mysql -uroot [email protected]
mysql> insert into db1.students(name) values('jerry');
Query OK, 1 row affected (0.20 sec)

mysql> select * from db1.students;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  5 | jerry |
+----+-------+
2 rows in set (0.00 sec)
  • 启动停止的mysql服务器,数据将会自动同步
[root@mysql1 ~]# mysql -uroot [email protected]
mysql> select * from db1.students;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  5 | jerry |
+----+-------+
2 rows in set (0.00 sec)
重新启动全部关闭后的mysql
# 查看所有的mysql服务器,找到具有最高seqno的节点,把safe_to_boostrap的值改为1
[root@mysql3 ~]# cat /var/lib/mysql/grastate.dat 
# GALERA saved state
version: 2.1
uuid:    a3b378ea-ce50-11eb-b624-0780367f3ddd
seqno:   7
safe_to_bootstrap: 1
[root@mysql3 ~]# systemctl start [email protected]

# 启动其他主机的mysql服务
[root@mysql1 ~]# systemctl start mysql
[root@mysql2 ~]# systemctl start mysql

你可能感兴趣的:(MySQL高可用PXC(Percona XtraDB Cluster))