1.概念:

    Percona Xtradb Cluster,简称PXC。是基于Galera插件的MySQL集群。

    76ca70aa2cc9bd6245ae4088cfd05a3d.png-wh_

    详细原理请移步我的另外文章:理解PXC集群原理

    

2.搭建

    官方手册地址:https://www.percona.com/doc/percona-xtradb-cluster/LATEST/index.html


     环境:PXC101 IP:192.168.1.101 端口:3306  版本5.7 CentOS7

           PXC102 IP:192.168.1.102 端口:3306  版本5.7 CentOS7

           PXC103 IP:192.168.1.103 端口:3306  版本5.7 CentOS7


           1.关闭系统防火墙

1
2
      #systemctl stop   firewalld
      #systemctl disable  firewalld       #取消防火墙开机自启动

           2.关闭selinux

1
2
3
      #vim /etc/selinux/config         
        SELINUX=permissive
      #getenforce                  #检查是否关闭

           3.安装依赖,与相关诊断软件            

1
      #yum install lsof socat telnet       #telnet用于检查端口

           4.下载,解压二进制安装包(三个主机都执行):

            (选择二进制安装包,可以避免各种编译或者Yum安装依赖关系,最节省时间,且日后升级方便

             101版本对应REHL系列版本,100对应DEBIAN系列版本,有区别           

1
2
3
4
5
6
7
8
9
      #wget https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-5.7.19-29.22-3/binary/tarball/Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101.tar.gz -P /usr/local/src
      #tar -zxf /usr/local/src/Percona-Xtra(...).tar.gz   -C /usr/local
        
      #创建符号连接,即创建快捷方式,这也是日后方便升级的小技巧
      #ln -s /usr/local/Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101 /usr/local/mysql
        
      #将可执行文件位置关联到路径中,方便像Yum安装的程序一样直接调用
      #echo "PATH=$PATH:/usr/local/mysql/bin"|cat >>/etc/profile
      #source /etc/profile

            5.创建MySQL用户与用户组,权限相关(三个主机都执行):           

1
2
3
      #useradd -d /usr/local/mysql -s /sbin/nologin -U -M mysql
      #mkdir -p /data/mysql/3306/{data,logs,tmp}
      #chown -R mysql.mysql /data/*

            6.创建配置文件(三个主机都执行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
            cat << EOF >/data/mysql/3306/my.cnf
            [client]
             #省略
            [mysql]
             #省略
            [mysqld]
            basedir    = /usr/local/mysql
            datadir    = /data/mysql/3306/data/
            socket    = /data/mysql/3306/tmp/mysql3306.sock
            pid-file = PXC3306.pid
            #省略#
            #log
            log-bin = mysql-bin
            sync_binlog = 1
            innodb_flush_log_at_trx_commit = 1
            #省略#
            #InnoDB
            innodb_file_per_table = 1
            ##InnoDB IO
            innodb_data_file_path = ibdata1:1G:autoextend
            innodb_thread_concurrency = 0
            #replication
            server-id = 1013306                          #IP+PORT
            master_info_repository = TABLE
            relay_log_info_repository = TABLE
            binlog_format = row
            relay_log_recovery = 1
            relay-log-purge = 1
            ##GTID
            gtid_mode = on                                                #必须
            enforce_gtid_consistency = 1                                  #必须
            #省略#
            #PXC MODE
            wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so          #galera插件位置
            wsrep_cluster_name=zrz                                        #集群名称
            wsrep_cluster_address=gcomm://192.168.1.101,192.168.1.102,192.168.1.103   #群集所有主机地址
            wsrep_node_name=PXC101                                        #本节点名称
            wsrep_node_address=192.168.1.101                              #本节点IP
            wsrep_sst_method=xtrabackup-v2                                #写集全量同步传输方式
            wsrep_sst_auth=dba:123456                                     #写集传输验证用户与密码
            pxc_strict_mode=ENFORCING                                     #必须,涉及到集群数据安全
            binlog_format=ROW                                             #必须
            default_storage_engine=InnoDB                                 #必须
            innodb_autoinc_lock_mode=2                                    #自增锁离散分配自增主键
             
            EOF

        先将上述配置文件中的PXC MODE相关配置项注释掉(前面加#)        

1
    #/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3306/my.cnf --initialize --user=mysql &

        获取初始密码并更改root密码,创建全量写集传输用户

1
2
3
4
5
6
7
8
9
10
    #grep  -i password /data/mysql/3306/error.log      
    #mysqld --defaults-file=/data/mysql/3306/my.cnf &  #纯单实例方式启动MySQL
    #mysql -u root -p                                #输入刚刚grep得到的密码                            #回车后输入刚刚查到的密码
    >set password='123456';                               #更改root用户密码
    >flush privileges;                
    >create user dba@'localhost' identified by '123456';  #创建全量写集传输用户
    >GRANT PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'dba'@'localhost';
    >flush privileges;
    >shutdown;                                            #关闭实例
    >exit

        8.启动PXC第一个节点

        先将上一步注释掉的PXC MODE相关配置项重新启用(前面删掉#)        

1
    #/usr/local/mysql/bin/mysqld--defaults-file=/data/mysql/3306/my.cnf --user=mysql --wsrep-new-cluster&

        同时tail -f /data/mysql/3306/error.log会观察到:

        871ec69711c312abbebed05ab72860df.png-wh_

         9.启动第二个第三个节点         

1
2
    #在第二个节点上执行
    #/usr/local/mysql/bin/mysqld--defaults-file=/data/mysql/3306/my.cnf --user=mysql&
1
2
    #在第三个节点上执行
    #/usr/local/mysql/bin/mysqld--defaults-file=/data/mysql/3306/my.cnf --user=mysql&

         这时在第一个节点的错误节点上观测错误日志可以得到

          60f1a856151dae939fe4ac09f036106c.png-wh_     

         在第二个节点或者第三个节点上都可以观测到:

        f2b69d3c32335840a4f46d6f4f2f98d1.png-wh_  

        OK,都启动成功。

3.查看WSREP写集同步

      1.查看写集同步相关参数

       在任意一个已经进入同步状态的实例上执行

        >show global status like "%wsrep%";

        输出如下:

        2819cc00ec2ab16c8288823233c51e32.png-wh_

      2.检查同步情况

        在实例3上创建数据库

        >create database dba;

        在实例1和实例2上都可以观测到:

        >show database;

        532dd4eb704262ede67a1cb539c25aa6.jpg-wh_


    OK,到这里,我们就完成了整个PXC集群的创建

4.注意:

    第一个节点启动时需添加 --wsrep-new-cluster参数。

    而节点加入已经启动的集群,则不需要这个参数,按照正常启动方式即可。

    更多疑难故障解答,请浏览我的下个帖子:PXC搭建故障疑难解答