PXC数据库集群服务

PXC 全名 Percona XtraDB Cluster
是基于Galera的nysql高可用集群解决方案.
Galera Cluster 是 Codership公司开发的一套免费开源的高可用方案
PXC集群主要由两部分组成:Percona Server with XtraDB和Write Replication patches(同步,多主复制插件)

PXC的特点
数据强一致性,无同步拖延
没有主从切换操作,无需使用虚拟IP
支持InnoDB存储引擎
多线程复制,部署使用简单
支持节点自动加入,无需手动拷贝数据

与PXC服务相关的端口
3306 数据库服务端口
4444 SST 端口
4567 集群通讯端口
4568 IST 端口

SST 全量同步(State Snapshot Transfer)
IST 增量同步(Incremental State Transfer)

我们实现数据库集群需要至少三台服务器(不需要提前安装mysql数据库)
pxcnode100 192.168.4.100
pxcnode101 192.168.4.101
pxcnode102 192.168.4.102

1.实现三台主机的相互通信(三台数据库服务器)
在/etc/hosts 下插入一下数据 ip 对应主机名

[root@pxcnode100 ~]# vim /etc/hosts
192.168.4.100  pxcnode100
192.168.4.101  pxcnode101
192.168.4.102  pxcnode102
:wq

一丶部署PXC服务
1.准备安装包,以及依赖包(三台数据库服务器)

libev-4.15-1.el6.rf.x86_64.rpm
percona-release-0.1-4.noarch.rpm
percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm    在线热备程序
Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar    集群服务程序
qpress-1.1-14.11.x86_64.rpm              递归压缩程序

2.安装PXC服务(三台数据库服务器)

[root@pxcnode100 PXC]# tar -xf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
[root@pxcnode100 PXC]# ls
libev-4.15-1.el6.rf.x86_64.rpm
percona-release-0.1-4.noarch.rpm
percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm
Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
Percona-XtraDB-Cluster-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-57-debuginfo-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-client-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-devel-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-full-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-garbd-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-server-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-shared-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-shared-compat-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-test-57-5.7.25-31.35.1.el7.x86_64.rpm
qpress-1.1-14.11.x86_64.rpm
[root@pxcnode100 PXC]# yum -y install *.rpm

2.修改配置文件
配置文件在

[root@pxcnode101 PXC]# vim /etc/percona-xtradb-cluster.conf.d/

相关配置文件用途

[root@pxcnode101 PXC]# ls /etc/percona-xtradb-cluster.conf.d/
mysqld.cnf                  数据库服务运行参数
mysqld_safe.cnf          
wsrep.cnf                   PXC集群配置文件

2.1修改mysqld.cnf配置文件(修改三台服务器)
server-id 不允许重复 其他的选项按默认配置
pxcnode100服务器

[root@pxcnode100 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
[mysqld]
server-id=100                                            //server-id  三台数据库服务不允许重复
datadir=/var/lib/mysql                                //数据库目录
socket=/var/lib/mysql/mysql.sock                //socket文件
log-error=/var/log/mysqld.log                      //日志文件
pid-file=/var/run/mysqld/mysqld.pid            //pid文件
log-bin                                                   //启用binlog日志
log_slave_updates                                  //启用链式复制
expire_logs_days=7                                //日志文件保留天数

pxcnode101服务器

[root@pxcnode101 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
server-id=101

pxcnode102服务器

[root@pxcnode102 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
server-id=102

2.2修改mysqld_safe.cnf配置文件(三台服务器)
按默认配置就可以

[root@pxcnode100 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf 
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid           //pid文件位置及名称
socket   = /var/lib/mysql/mysql.sock            //socket文件位置及名称
nice     = 0

2.3修改wsrep.cnf 戏群配置文件(修改三台服务器)
wsrep_Cluster_address=gcomm:// 添加集群成员
wsrep_node_address= 本机ip地址
wsrep_cluster_name= 集群名称 三台服务器需一致
wsrep_node_name= 本机主机名
wsrep_sst_auth=“sstuser:” SST数据同步授权用户

pxcnode100服务器

[root@pxcnode100 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://192.168.4.100,192.168.4.102,192.168.4.100
wsrep_node_address=192.168.4.100
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxcnode100
wsrep_sst_auth="sstuser:langmanaotian"

pxcnode101服务器

[root@pxcnode101 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
wsrep_cluster_address=gcomm://192.168.4.100,192.168.4.102,192.168.4.101
wsrep_node_address=192.168.4.101
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxcnode101
wsrep_sst_auth="sstuser:langmanaotian"

pxcnode102服务器

[root@pxcnode102 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf 
wsrep_cluster_address=gcomm://192.168.4.100,192.168.4.101,192.168.4.102
wsrep_node_address=192.168.4.102
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxcnode102
wsrep_sst_auth="sstuser:langmanaotian"

二丶启动PXC
1.pxcnode100服务器启动集群服务,101 102 服务器启动mysql服务
pxcnode100
初始密码在/var/log/mysqld.log里

[root@pxcnode100 PXC]# systemctl start [email protected]
[root@pxcnode100 PXC]# grep password /var/log/mysqld.log 
2019-09-10T13:31:26.584802Z 1 [Note] A temporary password is generated for root@localhost: ?o=B3W.pT%Uy
[root@pxcnode100 PXC]# mysql -u root -p'?o=B3W.pT%Uy'

修改密码添加权限,权限密码应与配置文件中的一致

mysql> alter user root@"localhost" identified by "langmanaotian";
Query OK, 0 rows affected (0.20 sec)

mysql> exit
Bye
[root@pxcnode100 PXC]# mysql -u root -p'langmanaotian'
mysql> 
mysql> grant reload, lock tables, replication client, process on *.* to sstuser@"localhost" identified by "langmanaotian";
Query OK, 0 rows affected, 1 warning (0.22 sec)

pxcnode101,pxcnode102启动mysql服务

[root@pxcnode101 PXC]# systemctl start mysql
[root@pxcnode102 PXC]# systemctl start mysql

三丶测试配置
1.登录mysql
在第一台pxcnode100集群服务器上授权过,所以pxcnode101,pxcnode102密码都是授权密码

[root@pxcnode101 ~]# mysql -u root -p'langmanaotian'
[root@pxcnode102 ~]# mysql -u root -p'langmanaotian'

2.查看集群信息
wsrep_incoming_addresses 成员列表
wsrep_sluster_size 集群服务器台数
wsrep_cluster_status primary 集群状态
wsrep_connected on 连接状态
wsrep_ready on 服务状态

mysql> show status like "%wsrep%";
+----------------------------------+----------------------------------------------------------+
| Variable_name                    | Value                                                    |
+----------------------------------+----------------------------------------------------------+
| wsrep_local_state_uuid           | 63c19395-d3cf-11e9-82ab-2b663822e08d                     |
| wsrep_protocol_version           | 9                                                        |
| wsrep_last_applied               | 2                                                        |
| wsrep_last_committed             | 2                                                        |
| 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             | 574                                                      |
| 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      | [ 173, 173 ]                                             |
| wsrep_flow_control_interval_low  | 173                                                      |
| wsrep_flow_control_interval_high | 173                                                      |
| wsrep_flow_control_status        | OFF                                                      |
| wsrep_cert_deps_distance         | 0.000000                                                 |
| wsrep_apply_oooe                 | 0.000000                                                 |
| wsrep_apply_oool                 | 0.000000                                                 |
| wsrep_apply_window               | 0.000000                                                 |
| wsrep_commit_oooe                | 0.000000                                                 |
| wsrep_commit_oool                | 0.000000                                                 |
| wsrep_commit_window              | 0.000000                                                 |
| wsrep_local_state                | 4                                                        |
| wsrep_local_state_comment        | Synced                                                   |
| wsrep_cert_index_size            | 0                                                        |
| wsrep_cert_bucket_count          | 22                                                       |
| wsrep_gcache_pool_size           | 1320                                                     |
| wsrep_causal_reads               | 0                                                        |
| wsrep_cert_interval              | 0.000000                                                 |
| wsrep_open_transactions          | 0                                                        |
| wsrep_open_connections           | 0                                                        |
| 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.4.100:3306,192.168.4.101:3306,192.168.4.102:3306 |
| wsrep_cluster_weight             | 3                                                        |
| 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                 | 63baa892-d3cf-11e9-941a-22c142216d11                     |
| wsrep_cluster_conf_id            | 0                                                        |
| wsrep_cluster_size               | 3                                                        |
| wsrep_cluster_state_uuid         | 63c19395-d3cf-11e9-82ab-2b663822e08d                     |
| wsrep_cluster_status             | Primary                                                  |
| wsrep_connected                  | ON                                                       |
| wsrep_local_bf_aborts            | 0                                                        |
| wsrep_local_index                | 0                                                        |
| wsrep_provider_name              | Galera                                                   |
| wsrep_provider_vendor            | Codership Oy                         |
| wsrep_provider_version           | 3.35(rddf9876)                                           |
| wsrep_ready                      | ON                                                       |
+----------------------------------+----------------------------------------------------------+
71 rows in set (0.11 sec)
mysql> 

3.测试准备
pxcnode100上创建xt库ry表,id设置成自增长,第一条数据可以看到id是1,第二条数据id变成4了,是因为我们一共有三台数据库服务,所以第二条数据就id等于4

mysql> create database  xt;
Query OK, 1 row affected (0.30 sec)

mysql> create table xt.ry(
    -> id  int primary key  auto_increment,
    -> name  char(10));
Query OK, 0 rows affected (1.80 sec)

mysql> insert into xt.ry(name) values('yxt');
Query OK, 1 row affected (0.09 sec)

mysql> insert into xt.ry(name) values('zwy');
Query OK, 1 row affected (0.15 sec)

mysql> select * from xt.ry;
+----+------+
| id | name |
+----+------+
|  1 | yxt  |
|  4 | zwy  |
+----+------+
2 rows in set (0.00 sec)

4.查看其他主机
pxcnode101跟pxcnode102数据一样

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xt                 |
+--------------------+
5 rows in set (0.03 sec)

mysql> select * from xt.ry;
+----+------+
| id | name |
+----+------+
|  1 | yxt  |
|  4 | zwy  |
+----+------+
2 rows in set (0.00 sec)

mysql> 

5.测试集群配置
在任意一台主机上添加授权用户
在客户端使用授权用户链接任意数据库都可以存储数据,且可以查看到相同的数据
建表时,必须有主键字段
5.1用户授权

mysql> grant all on xt.* to admin@"%" identified by "langmanaotian"
    -> ;
Query OK, 0 rows affected, 1 warning (0.26 sec)

5.2客户端登录

[root@localhost ~]# mysql -h192.168.4.100   -u admin -p'langmanaotian'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xt                 |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from xt.ry;
+----+------+
| id | name |
+----+------+
|  1 | yxt  |
|  4 | zwy  |
+----+------+
2 rows in set (0.00 sec)

mysql> create table xt.yu(name char(5), age int primary key);
Query OK, 0 rows affected (0.89 sec)

mysql> desc xt.yu;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name  | char(5) | YES  |     | NULL    |       |
| age   | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

5.3建库时不设置主键

mysql> create table xt.y(age int );
Query OK, 0 rows affected (1.25 sec)

mysql> desc xt.y;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into xt.y values(12)
    -> ;
ERROR 1105 (HY000): Percona-XtraDB-Cluster prohibits use of DML command on a table (xt.y) without an explicit primary key with pxc_strict_mode = ENFORCING or MASTER

你可能感兴趣的:(PXC数据库集群服务)