您的位置:首页 > 数据库 > MariaDB

MariaDB基础实验四——高可用方案MHA、Galere Cluster、 PXC集群部署

2020-01-14 23:25 2436 查看

高可用方案MHA

基本介绍和实验环境

围绕着主从复制来实现数据库高可用,但是如果是 MariaDB 的主服务的服务宕机了,整个主从复制架构
也就失去了高可用的作用。现在我们使用 一主两从 MariaDB 主从复制架构,希望如果主节点的 MariaDB 服务宕机了,可以
快速的将 主节点的二进制日志复制到 和 主节点同步数据差距最小的服务上,然后将它设置为主节点,另一个从服务将自己
的主节点配置成新称为主节点的服务器,这时便可以达到快速修复的数据库服务的目的,但是人为收到发现和手动修复效率
太低,我们期望通过软件来快速完成上述工作内容, 这时候 MHA 工具就诞生了。下面我们演示一下 MHA 高可用 一主两从
的 MariaDB 主从复制架构。

实验中关闭防火墙以及selinux
准备三台 新安装的 CentOS7服务器,并使用 yum 安装 MariaDB10.2.29 ,主机名 分别为 centos7-7, centos7-17, centos7-27,
IP 地址分别为 192.168.32.7 192.168.32.17 192.168.32.27

vim /etc/yum.repos.d/mariadb.repo

[mariadb]
name=MariaDB
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

yum install MariaDB-server -y

一主多从搭建

配置centos7-7服务器,使其成为 MariaDB 的主节点:

[root@centos7-7 ~]#vim /etc/my.cnf.d/server.cnf

[mysqld]
server-id=7
log_bin
skip_name_resolve=1

[root@centos7-7 ~]#
[root@centos7-7 ~]#systemctl start mariadb
[root@centos7-7 ~]#ss -ntl
State      Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port
LISTEN     0      128                                 *:111                                             *:*
LISTEN     0      128                                 *:48307                                           *:*
LISTEN     0      5                       192.168.122.1:53                                              *:*
LISTEN     0      128                                 *:22                                              *:*
LISTEN     0      128                         127.0.0.1:631                                             *:*
LISTEN     0      100                         127.0.0.1:25                                              *:*
LISTEN     0      128                         127.0.0.1:6010                                            *:*
LISTEN     0      80                                 :::3306                                           :::*
LISTEN     0      128                                :::111                                            :::*
LISTEN     0      128                                :::22                                             :::*
LISTEN     0      128                               ::1:631                                            :::*
LISTEN     0      128                                :::56312                                          :::*
LISTEN     0      100                               ::1:25                                             :::*
LISTEN     0      128                               ::1:6010                                           :::*
[root@centos7-7 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.29-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| centos7-7-bin.000001 |       332 |
+----------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to mhauser@"192.168.32.%" identified by "centos";
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to  "kaivi"@"192.168.32.%" identified by 'centos';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+---------+--------------+-------------------------------------------+
| user    | host         | password                                  |
+---------+--------------+-------------------------------------------+
| root    | localhost    |                                           |
| root    | centos7-7    |                                           |
| root    | 127.0.0.1    |                                           |
| root    | ::1          |                                           |
|         | localhost    |                                           |
|         | centos7-7    |                                           |
| mhauser | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
| kaivi   | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
+---------+--------------+-------------------------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> show master logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| centos7-7-bin.000001 |       733 |
+----------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]>

配置centos7-17服务器,使其成为 MariaDB 的第一个从节点:

[root@centos7-17 ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=17
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1

[root@centos7-17 ~]#systemctl start mariadb

[root@centos7-17 ~]#ss -ntl
State      Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port
LISTEN     0      128                                 *:22                                              *:*
LISTEN     0      100                         127.0.0.1:25                                              *:*
LISTEN     0      80                                 :::3306                                           :::*
LISTEN     0      128                                :::22                                             :::*
LISTEN     0      100                               ::1:25                                             :::*

[root@centos7-17 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.29-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO
->   MASTER_HOST='192.168.32.7',
->   MASTER_USER='kaivi',
->   MASTER_PASSWORD='centos',
->   MASTER_PORT=3306,
->   MASTER_LOG_FILE='centos7-7-bin.000001',
->   MASTER_LOG_POS=332;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.7
Master_User: kaivi
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: centos7-7-bin.000001
Read_Master_Log_Pos: 733
Relay_Log_File: centos7K-relay-bin.000002
Relay_Log_Pos: 960
Relay_Master_Log_File: centos7-7-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: 733
Relay_Log_Space: 1272
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: 7
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
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
1 row in set (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+---------+--------------+-------------------------------------------+
| user    | host         | password                                  |
+---------+--------------+-------------------------------------------+
| root    | localhost    |                                           |
| root    | centos7k     |                                           |
| root    | 127.0.0.1    |                                           |
| root    | ::1          |                                           |
|         | localhost    |                                           |
|         | centos7k     |                                           |
| mhauser | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
| kaivi   | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
+---------+--------------+-------------------------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]>

配置centos7-27服务器,使其成为 MariaDB 的第二个从节点:

[root@centos7-27 ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=27
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1

[root@centos-27 ~]#systemctl start mariadb
[root@centos-27 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      80                           :::3306                                     :::*
LISTEN      0      128                          :::22                                       :::*
LISTEN      0      100                         ::1:25                                       :::*
[root@centos-27 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.29-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO
->   MASTER_HOST='192.168.32.7',
->   MASTER_USER='kaivi',
->   MASTER_PASSWORD='centos',
->   MASTER_PORT=3306,
->   MASTER_LOG_FILE='centos7-7-bin.000001',
->   MASTER_LOG_POS=332;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.7
Master_User: kaivi
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: centos7-7-bin.000001
Read_Master_Log_Pos: 733
Relay_Log_File: centos7K-relay-bin.000002
Relay_Log_Pos: 960
Relay_Master_Log_File: centos7-7-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: 733
Relay_Log_Space: 1272
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: 7
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
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
1 row in set (0.00 sec)

MariaDB [(none)]> select user,host,password from mysql.user;
+---------+--------------+-------------------------------------------+
| user    | host         | password                                  |
+---------+--------------+-------------------------------------------+
| root    | localhost    |                                           |
| root    | centos7k     |                                           |
| root    | 127.0.0.1    |                                           |
| root    | ::1          |                                           |
|         | localhost    |                                           |
|         | centos7k     |                                           |
| mhauser | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
| kaivi   | 192.168.32.% | *128977E278358FF80A246B5046F51043A2B1FCED |
+---------+--------------+-------------------------------------------+
8 rows in set (0.01 sec)

MariaDB [(none)]>

至此,我们完成了 MariaDB 一主两从的架构大家,下面我们开始配置安装 MHA。

SSH免密设置

准备一台新的 CentOS6作为HMA,ip 地址为 192.168.32.6,创建 ssh 秘钥,实现4台服务器免密登录

[root@kaivi6 ~]#ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:82:DF:CB
inet addr:192.168.32.6  Bcast:192.168.32.255  Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe82:dfcb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:6426 errors:0 dropped:0 overruns:0 frame:0
TX packets:1678 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8467596 (8.0 MiB)  TX bytes:197581 (192.9 KiB)

[root@kaivi6 ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
98:8d:2a:8f:36:9e:5b:9d:45:42:71:03:a8:7a:73:9c root@kaivi6
The keys randomart image is:
+--[ RSA 2048]----+
|     .+oo        |
|    .. . .       |
|   .  . .        |
|  .    B         |
| . . .+ S        |
|. o Eo o         |
| ..oo o          |
|  o*             |
| o=o.            |
+-----------------+
[root@kaivi6 ~]#cd .ssh/
[root@kaivi6 .ssh]#ll
total 16
-rw------- 1 root root  396 Nov 30 15:21 authorized_keys
-rw------- 1 root root 1675 Nov 30 15:36 id_rsa
-rw-r--r-- 1 root root  393 Nov 30 15:36 id_rsa.pub
-rw-r--r-- 1 root root 1486 Nov 30 15:28 known_hosts

[root@kaivi6 .ssh]#ssh-copy-id root@192.168.32.6      #拷贝到本机
root@192.168.32.6 s password:
Now try logging into the machine, with "ssh 'root@192.168.32.6'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@kaivi6 .ssh]#ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

[root@kaivi6 .ssh]#scp -rp .ssh 192.168.32.7:/root/  #拷贝主服务器
root@192.168.32.7's password:
[root@kaivi6 .ssh]#cd
[root@kaivi6 ~]#scp -rp .ssh 192.168.32.7:/root/
root@192.168.32.7's password:
authorized_keys                                                                  100%  789     0.8KB/s   00:00
known_hosts                                                                      100% 1486     1.5KB/s   00:00
id_rsa.pub                                                                       100%  393     0.4KB/s   00:00
id_rsa                                                                           100% 1675     1.6KB/s   00:00
[root@kaivi6 ~]#scp -rp .ssh 192.168.32.17:/root/    #拷贝从服务器1
root@192.168.32.17  s password:
authorized_keys                                                                  100%  789     0.8KB/s   00:00
known_hosts                                                                      100% 1881     1.8KB/s   00:00
id_rsa.pub                                                                       100%  393     0.4KB/s   00:00
id_rsa                                                                           100% 1675     1.6KB/s   00:00

[root@kaivi6 ~]#scp -rp .ssh 192.168.32.27:/root/    #拷贝从服务器2
RSA key fingerprint is 92:f6:80:29:79:e9:0d:5e:5d:ca:16:31:87:d3:f2:d2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.32.27' (RSA) to the list of known hosts.
root@192.168.32.27's password:
authorized_keys                                                                  100%  789     0.8KB/s   00:00
known_hosts                                                                      100% 2276     2.2KB/s   00:00
id_rsa.pub                                                                       100%  393     0.4KB/s   00:00
id_rsa                                                                           100% 1675     1.6KB/s   00:00

[root@kaivi6 ~]#ssh 192.168.32.7
Last failed login: Sat Nov 30 15:51:28 CST 2019 from 192.168.32.6 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Sat Nov 30 15:51:15 2019 from 192.168.32.6
[root@centos7-7 ~]#ssh 192.168.32.17
Last login: Sat Nov 30 15:37:24 2019 from 192.168.32.8
[root@centos7-17 ~]#ssh 192.168.32.27
Last login: Sat Nov 30 15:51:53 2019 from 192.168.32.7
[root@centos7-27 ~]#ssh 192.168.32.6
Last login: Sat Nov 30 15:34:17 2019 from 192.168.32.1
[root@kaivi6 ~]#exit
logout
Connection to 192.168.32.6 closed.
[root@centos7-27 ~]#exit
logout
Connection to 192.168.32.27 closed.
[root@centos7-17 ~]#exit
logout
Connection to 192.168.32.17 closed.
[root@centos7-7 ~]#exit
logout
Connection to 192.168.32.7 closed.
[root@kaivi6 ~]#

安装HMA的rpm包

在192.168.32.6服务器上配置 epel 源,并且安装 HMA manager 和 node 包,并将 node 包分别拷贝到 3台 MariaDB 中

[root@kaivi6 ~]#ls
abc.txt          Downloads           max_min.sh                               Public
anaconda-ks.cfg  f1.txt              mha4mysql-manager-0.56-0.el6.noarch.rpm  script
awktest.txt      install.log         mha4mysql-node-0.56-0.el6.noarch.rpm     Templates
Desktop          install.log.syslog  Music                                    Videos
Documents        likai.awk           Pictures
[root@kaivi6 ~]#
[root@kaivi6 ~]#yum install -y ./*.rpm

Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Examining ./mha4mysql-manager-0.56-0.el6.noarch.rpm: mha4mysql-manager-0.56-0.el6.noarch
Marking ./mha4mysql-manager-0.56-0.el6.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
Examining ./mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking ./mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-manager.noarch 0:0.56-0.el6 will be installed
--> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch
--> Processing Dependency: perl(Log::Dispatch::File) for package: mha4mysql-manager-0.56-0.el6.noarch
.......(省略)
---> Package perl-Mail-Sendmail.noarch 0:0.79-12.el6 will be installed
---> Package perl-MailTools.noarch 0:2.04-4.el6 will be installed
---> Package perl-MIME-Types.noarch 0:1.28-2.el6 will be installed
---> Package perl-TimeDate.noarch 1:1.16-13.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
Package                        Arch        Version                Repository                                 Size
===================================================================================================================
Installing:
mha4mysql-manager              noarch      0.56-0.el6             /mha4mysql-manager-0.56-0.el6.noarch      325 k
mha4mysql-node                 noarch      0.56-0.el6             /mha4mysql-node-0.56-0.el6.noarch         102 k
Installing for dependencies:
perl-Config-Tiny               noarch      2.12-7.1.el6           centos6                                    23 k
perl-DBD-MySQL                 x86_64      4.013-3.el6            centos6                                   134 k
.......(省略)
perl-MailTools                 noarch      2.04-4.el6             centos6                                   101 k
perl-Parallel-ForkManager      noarch      1.20-1.el6             epel                                       27 k
perl-Params-Validate           x86_64      0.92-3.el6             centos6                                    75 k
perl-Time-HiRes                x86_64      4:1.9721-144.el6       centos6                                    49 k
perl-TimeDate                  noarch      1:1.16-13.el6          centos6                                    37 k

Transaction Summary
===================================================================================================================
Install      16 Package(s)

Total size: 1.8 M
Total download size: 1.4 M
Installed size: 3.5 M
Downloading Packages:
(1/14): perl-Config-Tiny-2.12-7.1.el6.noarch.rpm                                            |  23 kB     00:00
(2/14): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm                                               | 134 kB     00:00
(3/14): perl-DBI-1.609-4.el6.x86_64.rpm                                                     | 705 kB     00:00
(4/14): perl-Email-Date-Format-1.002-5.el6.noarch.rpm                                       |  16 kB     00:00
.......(省略)
(13/14): perl-Time-HiRes-1.9721-144.el6.x86_64.rpm                                          |  49 kB     00:00
(14/14): perl-TimeDate-1.16-13.el6.noarch.rpm                                               |  37 kB     00:00
-------------------------------------------------------------------------------------------------------------------
Total                                                                              828 kB/s | 1.4 MB     00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : perl-DBI-1.609-4.el6.x86_64                                                                    1/16
Installing : perl-DBD-MySQL-4.013-3.el6.x86_64                                                              2/16
Installing : mha4mysql-node-0.56-0.el6.noarch                                                               3/16
Installing : perl-Parallel-ForkManager-1.20-1.el6.noarch                                                    6/16
.......(省略)
Verifying  : perl-Config-Tiny-2.12-7.1.el6.noarch                                                          14/16
Verifying  : perl-Log-Dispatch-2.27-1.el6.noarch                                                           15/16
Verifying  : perl-MIME-Types-1.28-2.el6.noarch                                                             16/16

Installed:
mha4mysql-manager.noarch 0:0.56-0.el6                     mha4mysql-node.noarch 0:0.56-0.el6

Dependency Installed:
perl-Config-Tiny.noarch 0:2.12-7.1.el6                    perl-DBD-MySQL.x86_64 0:4.013-3.el6
perl-DBI.x86_64 0:1.609-4.el6                             perl-Email-Date-Format.noarch 0:1.002-5.el6
perl-Log-Dispatch.noarch 0:2.27-1.el6                     perl-MIME-Lite.noarch 0:3.027-2.el6
perl-MIME-Types.noarch 0:1.28-2.el6                       perl-Mail-Sender.noarch 0:0.8.16-3.el6
perl-Mail-Sendmail.noarch 0:0.79-12.el6                   perl-MailTools.noarch 0:2.04-4.el6
perl-Parallel-ForkManager.noarch 0:1.20-1.el6             perl-Params-Validate.x86_64 0:0.92-3.el6
perl-Time-HiRes.x86_64 4:1.9721-144.el6                   perl-TimeDate.noarch 1:1.16-13.el6

Complete!
[root@kaivi6 ~]#
[root@kaivi6 ~]#scp -rp mha4mysql-node-0.56-0.el6.noarch.rpm 192.168.32.7:/root/
mha4mysql-node-0.56-0.el6.noarch.rpm                                             100%   35KB  35.5KB/s   00:00
[root@kaivi6 ~]#scp -rp mha4mysql-node-0.56-0.el6.noarch.rpm 192.168.32.17:/root/
mha4mysql-node-0.56-0.el6.noarch.rpm                                             100%   35KB  35.5KB/s   00:00
[root@kaivi6 ~]#scp -rp mha4mysql-node-0.56-0.el6.noarch.rpm 192.168.32.27:/root/
mha4mysql-node-0.56-0.el6.noarch.rpm                                             100%   35KB  35.5KB/s   00:00
[root@kaivi6 ~]#

在三台 MariaDB 中分别安装 MHA node 包

在主节点192.168.32.7中操作:

[root@centos7-7 .ssh]#cd
[root@centos7-7 ~]#ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg                  Music     Public     Videos
Desktop          Downloads  mha4mysql-node-0.56-0.el6.noarch.rpm  Pictures  Templates

[root@centos7-7 ~]#yum install mha4mysql-node-0.56-0.el6.noarch.rpm
Loaded plugins: fastestmirror, langpacks
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================
Package                      Arch                 Version                    Repository                                       Size
====================================================================================================================================
Installing:
mha4mysql-node               noarch               0.56-0.el6                 /mha4mysql-node-0.56-0.el6.noarch               102 k

Transaction Summary
====================================================================================================================================
Install  1 Package

Total size: 102 k
Installed size: 102 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mha4mysql-node-0.56-0.el6.noarch                                                                                 1/1
Verifying  : mha4mysql-node-0.56-0.el6.noarch                                                                                 1/1

Installed:
mha4mysql-node.noarch 0:0.56-0.el6

Complete!
[root@centos7-7 ~]#

在从节点1的192.168.32.17中操作:

[root@centos7-17 .ssh]#cd
[root@centos7-17 ~]#ls
anaconda-ks.cfg  init.sh  mha4mysql-node-0.56-0.el6.noarch.rpm

[root@centos7-17 ~]#yum install mha4mysql-node-0.56-0.el6.noarch.rpm
Loaded plugins: fastestmirror
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================
Package                      Arch                 Version                    Repository                                       Size
====================================================================================================================================
Installing:
mha4mysql-node               noarch               0.56-0.el6                 /mha4mysql-node-0.56-0.el6.noarch               102 k

Transaction Summary
====================================================================================================================================
Install  1 Package

Total size: 102 k
Installed size: 102 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mha4mysql-node-0.56-0.el6.noarch                                                                                 1/1
Verifying  : mha4mysql-node-0.56-0.el6.noarch                                                                                 1/1

Installed:
mha4mysql-node.noarch 0:0.56-0.el6

Complete!
[root@centos7-17 ~]#

在从节点2的192.168.32.27中操作:

[root@centos-27 .ssh]#cd
[root@centos-27 ~]#ls
anaconda-ks.cfg  init.sh  mha4mysql-node-0.56-0.el6.noarch.rpm

[root@centos-27 ~]#yum install mha4mysql-node-0.56-0.el6.noarch.rpm
Loaded plugins: fastestmirror
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
Package                  Arch             Version               Repository                                   Size
===================================================================================================================
Installing:
mha4mysql-node           noarch           0.56-0.el6            /mha4mysql-node-0.56-0.el6.noarch           102 k

Transaction Summary
===================================================================================================================
Install  1 Package

Total size: 102 k
Installed size: 102 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mha4mysql-node-0.56-0.el6.noarch                                                                1/1
Verifying  : mha4mysql-node-0.56-0.el6.noarch                                                                1/1

Installed:
mha4mysql-node.noarch 0:0.56-0.el6

Complete!
[root@centos-27 ~]#

配置MHA

配置 MHA 管理节点,启动 MHA 服务
在MHA服务器192.168.32.6中操作:

[root@kaivi6 ~]#mkdir /etc/mha
[root@kaivi6 ~]#
[root@kaivi6 ~]#vim /etc/mha/god.conf
[root@kaivi6 ~]#cat /etc/mha/god.conf
[server default]
user=mhauser
password=centos
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=kaivi
repl_password=centos
ping_interval=1
[server1]
hostname=192.168.32.7
candidate_master=1
[server2]
hostname=192.168.32.17
candidate_master=1
[server3]
hostname=192.168.32.27
candidate_master=1
[root@kaivi6 ~]#
[root@kaivi6 ~]#masterha_check_ssh --conf=/etc/mha/god.conf
Sat Nov 30 16:04:08 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:04:08 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:04:08 2019 - [info] Reading server configuration from /etc/mha/god.conf..
Sat Nov 30 16:04:08 2019 - [info] Starting SSH connection tests..
Sat Nov 30 16:04:09 2019 - [debug]
Sat Nov 30 16:04:08 2019 - [debug]  Connecting via SSH from root@192.168.32.7(192.168.32.7:22) to root@192.168.32.17(192.168.32.17:22)..
Sat Nov 30 16:04:08 2019 - [debug]   ok.
Sat Nov 30 16:04:08 2019 - [debug]  Connecting via SSH from root@192.168.32.7(192.168.32.7:22) to root@192.168.32.27(192.168.32.27:22)..
Sat Nov 30 16:04:08 2019 - [debug]   ok.
Sat Nov 30 16:04:09 2019 - [debug]
Sat Nov 30 16:04:08 2019 - [debug]  Connecting via SSH from root@192.168.32.17(192.168.32.17:22) to root@192.168.32.7(192.168.32.7:22)..
Sat Nov 30 16:04:09 2019 - [debug]   ok.
Sat Nov 30 16:04:09 2019 - [debug]  Connecting via SSH from root@192.168.32.17(192.168.32.17:22) to root@192.168.32.27(192.168.32.27:22)..
Sat Nov 30 16:04:09 2019 - [debug]   ok.
Sat Nov 30 16:04:15 2019 - [debug]
Sat Nov 30 16:04:09 2019 - [debug]  Connecting via SSH from root@192.168.32.27(192.168.32.27:22) to root@192.168.32.7(192.168.32.7:22)..
Sat Nov 30 16:04:09 2019 - [debug]   ok.
Sat Nov 30 16:04:09 2019 - [debug]  Connecting via SSH from root@192.168.32.27(192.168.32.27:22) to root@192.168.32.17(192.168.32.17:22)..
Sat Nov 30 16:04:14 2019 - [debug]   ok.
Sat Nov 30 16:04:15 2019 - [info] All SSH connection tests passed successfully.
[root@kaivi6 ~]#
[root@kaivi6 ~]#
如果不是健康状态,则需要重新新建一个复制线程账号
[root@kaivi6 ~]#masterha_check_repl --conf=/etc/mha/god.conf
Sat Nov 30 16:22:48 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:22:48 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:22:48 2019 - [info] Reading server configuration from /etc/mha/god.conf..
Sat Nov 30 16:22:48 2019 - [info] MHA::MasterMonitor version 0.56.
Sat Nov 30 16:22:49 2019 - [info] GTID failover mode = 0
Sat Nov 30 16:22:49 2019 - [info] Dead Servers:
Sat Nov 30 16:22:49 2019 - [info] Alive Servers:
Sat Nov 30 16:22:49 2019 - [info]   192.168.32.7(192.168.32.7:3306)
Sat Nov 30 16:22:49 2019 - [info]   192.168.32.17(192.168.32.17:3306)
Sat Nov 30 16:22:49 2019 - [info]   192.168.32.27(192.168.32.27:3306)
Sat Nov 30 16:22:49 2019 - [info] Alive Slaves:
Sat Nov 30 16:22:49 2019 - [info]   192.168.32.17(192.168.32.17:3306)  Version=10.2.29-MariaDB-log (oldest major version between slaves) log-bin:enabled
Sat Nov 30 16:22:49 2019 - [info]     Replicating from 192.168.32.7(192.168.32.7:3306)
Sat Nov 30 16:22:49 2019 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Nov 30 16:22:49 2019 - [info]   192.168.32.27(192.168.32.27:3306)  Version=10.2.29-MariaDB-log (oldest major version between slaves) log-bin:enabled
Sat Nov 30 16:22:49 2019 - [info]     Replicating from 192.168.32.7(192.168.32.7:3306)
Sat Nov 30 16:22:49 2019 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Nov 30 16:22:49 2019 - [info] Current Alive Master: 192.168.32.7(192.168.32.7:3306)
Sat Nov 30 16:22:49 2019 - [info] Checking slave configurations..
Sat Nov 30 16:22:49 2019 - [info] Checking replication filtering settings..
Sat Nov 30 16:22:49 2019 - [info]  binlog_do_db= , binlog_ignore_db=
Sat Nov 30 16:22:49 2019 - [info]  Replication filtering check ok.
Sat Nov 30 16:22:49 2019 - [info] GTID (with auto-pos) is not supported
Sat Nov 30 16:22:49 2019 - [info] Starting SSH connection tests..
Sat Nov 30 16:22:56 2019 - [info] All SSH connection tests passed successfully.
Sat Nov 30 16:22:56 2019 - [info] Checking MHA Node version..
Sat Nov 30 16:22:57 2019 - [info]  Version check ok.
Sat Nov 30 16:22:57 2019 - [info] Checking SSH publickey authentication settings on the current master..
Sat Nov 30 16:23:02 2019 - [warning] HealthCheck: Got timeout on checking SSH connection to 192.168.32.7! at /usr/share/perl5/vendor_perl/MHA/HealthCheck.pm line 342.
Sat Nov 30 16:23:02 2019 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Sat Nov 30 16:23:02 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.32.17 --slave_ip=192.168.32.17 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=10.2.29-MariaDB-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Sat Nov 30 16:23:02 2019 - [info]   Connecting to root@192.168.32.17(192.168.32.17:22)..
Creating directory /data/mastermha/app1/.. done.
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to centos7K-relay-bin.000002
Temporary relay log file is /var/lib/mysql/centos7K-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Nov 30 16:23:02 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.32.27 --slave_ip=192.168.32.27 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=10.2.29-MariaDB-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Sat Nov 30 16:23:02 2019 - [info]   Connecting to root@192.168.32.27(192.168.32.27:22)..
Creating directory /data/mastermha/app1/.. done.
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to centos7K-relay-bin.000004
Temporary relay log file is /var/lib/mysql/centos7K-relay-bin.000004
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Nov 30 16:23:07 2019 - [info] Slaves settings check done.
Sat Nov 30 16:23:07 2019 - [info]
192.168.32.7(192.168.32.7:3306) (current master)
+--192.168.32.17(192.168.32.17:3306)
+--192.168.32.27(192.168.32.27:3306)

Sat Nov 30 16:23:07 2019 - [info] Checking replication health on 192.168.32.17..
Sat Nov 30 16:23:07 2019 - [info]  ok.
Sat Nov 30 16:23:07 2019 - [info] Checking replication health on 192.168.32.27..
Sat Nov 30 16:23:07 2019 - [info]  ok.
Sat Nov 30 16:23:07 2019 - [warning] master_ip_failover_script is not defined.
Sat Nov 30 16:23:07 2019 - [warning] shutdown_script is not defined.
Sat Nov 30 16:23:07 2019 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
[root@kaivi6 ~]#masterha_manager --conf=/etc/mha/god.conf
Sat Nov 30 16:23:37 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:23:37 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:23:37 2019 - [info] Reading server configuration from /etc/mha/god.conf..

#由于是前台运行,所以到这里就一直显示后台状态,也说明这个服务只是针对一次性的救援高可用。一次之后需要重新配置。

测试是否搭建成功

在192.168.32.7主服务器 强制关闭 MariaDB 服务。

[root@centos7-7 ~]#ps aux | grep mysqld
mysql      8790  0.0  9.1 1306828 91384 ?       Ssl  14:10   0:02 /usr/sbin/mysqld --basedir=/usr
root      11911  0.0  0.0 112708   976 pts/0    R+   17:02   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#ps aux | grep mysql
mysql      8790  0.0  9.1 1306828 91384 ?       Ssl  14:10   0:02 /usr/sbin/mysqld --basedir=/usr
root      11915  0.0  0.0 112708   976 pts/0    R+   17:02   0:00 grep --color=auto mysql
[root@centos7-7 ~]#ps aux | grep mysqld
mysql      8790  0.0  9.1 1306828 91384 ?       Ssl  14:10   0:02 /usr/sbin/mysqld --basedir=/usr
root      11917  0.0  0.0 112708   976 pts/0    R+   17:02   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#kill -9 8790
[root@centos7-7 ~]#ps aux | grep mysqld
root      11931  0.0  0.0 112708   972 pts/0    R+   17:03   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#kill -9 11931
-bash: kill: (11931) - No such process
[root@centos7-7 ~]#ps aux | grep mysqld
mysql     11954  1.8  8.4 1325388 84188 ?       Ssl  17:03   0:00 /usr/sbin/mysqld --basedir=/usr
root      12000  0.0  0.0 112708   976 pts/0    R+   17:04   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#kill -9 11954
[root@centos7-7 ~]#ps aux | grep mysqld
root      12002  0.0  0.0 112708   976 pts/0    R+   17:04   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#ps aux | grep mysqld
mysql     12027  6.5  8.4 1325088 84276 ?       Ssl  17:04   0:00 /usr/sbin/mysqld --basedir=/usr
root      12065  0.0  0.0 112708   972 pts/0    R+   17:04   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#kill -9 12027
[root@centos7-7 ~]#ps aux | grep mysqld
mysql     12089  0.0  8.2 1325088 82244 ?       Ssl  17:04   0:00 /usr/sbin/mysqld --basedir=/usr
root      12126  0.0  0.0 112708   976 pts/0    R+   17:04   0:00 grep --color=auto mysqld
[root@centos7-7 ~]#kill -9 12089

查看192.168.32.6服务器中 MHA 服务是否有变化:

[root@kaivi6 ~]#masterha_manager --conf=/etc/mha/god.conf
Sat Nov 30 16:23:37 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:23:37 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:23:37 2019 - [info] Reading server configuration from /etc/mha/god.conf..
Sat Nov 30 16:41:47 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:41:47 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:41:47 2019 - [info] Reading server configuration from /etc/mha/god.conf..
[root@kaivi6 ~]#
[root@kaivi6 ~]#masterha_manager --conf=/etc/mha/god.conf
Sat Nov 30 16:45:56 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:45:56 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:45:56 2019 - [info] Reading server configuration from /etc/mha/god.conf..
Creating /data/mastermha/app1 if not exists..    ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to centos7-7-bin.000006
Sat Nov 30 16:46:29 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Nov 30 16:46:29 2019 - [info] Reading application default configuration from /etc/mha/god.conf..
Sat Nov 30 16:46:29 2019 - [info] Reading server configuration from /etc/mha/god.conf..
[root@kaivi6 ~]#
[root@kaivi6 ~]#tail -20 /data/mastermha/app1/manager.log
Sat Nov 30 16:46:55 2019 - [info] Master failover to 192.168.32.17(192.168.32.17:3306) completed successfully.
Sat Nov 30 16:46:55 2019 - [info]

----- Failover Report -----

god: MySQL Master failover 192.168.32.7(192.168.32.7:3306) to 192.168.32.17(192.168.32.17:3306) succeeded

Master 192.168.32.7(192.168.32.7:3306) is down!

Check MHA Manager logs at kaivi6:/data/mastermha/app1/manager.log for details.

Started automated(non-interactive) failover.
The latest slave 192.168.32.17(192.168.32.17:3306) has all relay logs for recovery.
Selected 192.168.32.17(192.168.32.17:3306) as a new master.
192.168.32.17(192.168.32.17:3306): OK: Applying all logs succeeded.
192.168.32.27(192.168.32.27:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.32.27(192.168.32.27:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.32.17(192.168.32.17:3306)
192.168.32.17(192.168.32.17:3306): Resetting slave info succeeded.
Master failover to 192.168.32.17(192.168.32.17:3306) completed successfully.
[root@kaivi6 ~]#

根据 MHA 日志信息,Selected 192.168.32.17(192.168.32.17:3306) as a new master.
MHA 将slave1 提升为 master ,slave2为 slave1的 slave

登录192.168.32.17 的 MariaDB 查看状态

[root@centos7-17 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 33
Server version: 10.2.29-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status;
Empty set (0.00 sec)

MariaDB [(none)]>

登录192.168.32.27 的 MariaDB 查看状态

[root@centos-27 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 10.2.29-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.17            #主服务器的地址发生变化
Master_User: kaivi
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: centos7K-bin.000002
Read_Master_Log_Pos: 345
Relay_Log_File: centos7K-relay-bin.000002
Relay_Log_Pos: 558
Relay_Master_Log_File: centos7K-bin.000002
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: 345
Relay_Log_Space: 870
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: 17
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
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
1 row in set (0.00 sec)

MariaDB [(none)]>

在 192.168.32.17中创建库,验证 192.168.32.27同步是否成功

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| db3                |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> create database ddbb44;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| db3                |
| ddbb44             |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]>

在192.168.32.27查看是否同步成功

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| db3                |
| ddbb44             |    #同步成功
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]>

数据库数据同步成功
由于 MHA 只能保证一次主节点切换,因此修复排查完成后,需要根据新的环境再次配置 MHA 服务,并启动。
之前的 192.168.32.17 由从节点升级成为主节点后,原来的配置文件中还保留 read_only 选项,应当取消,放置服务重启后造成主不可写操作。

[root@centos7-17 ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=17
log-bin
#read_only
relay_log_purge=0
skip_name_resolve=1

MHA 高可用 MariaDB 操作实验完成。

高可用方案Galere Cluster

galera 集群概述与搭建实验

概述

Galera Cluster:集成了Galera插件的MySQL集群,是一种新型的,数据不共享的,高度冗余的高可用
方案,目前Galera Cluster有两个版本,分别是Percona Xtradb Cluster及MariaDB Cluster,Galera本
身是具有多主特性的,即采用multi-master的集群架构,是一个既稳健,又在数据一致性、完整性及高
性能方面有出色表现的高可用解决方案

Galera Cluster特点

Galera Cluster 优势
多主架构:真正的多点读写的集群,在任何时候读写数据,都是最新的
同步复制:集群不同节点之间数据同步,没有延迟,在数据库挂掉之后,数据不会丢失
并发复制:从节点APPLY数据时,支持并行执行,更好的性能
故障切换:在出现数据库故障时,因支持多点写入,切换容易
热插拔:在服务期间,如果数据库挂了,只要监控程序发现的够快,不可服务时间就会非常少。在
节点故障期间,节点本身对集群的影响非常小
自动节点克隆:在新增节点,或者停机维护时,增量数据或者基础数据不需要人工手动备份提供,
Galera Cluster会自动拉取在线节点数据,最终集群会变为一致
对应用透明:集群的维护,对应用程序是透明的

Galera Cluster 缺点
由于DDL 需全局验证通过,则集群性能由集群中最差性能节点决定(一般集群节点配置都是一样
的)
新节点加入或延后较大的节点重新加入需全量拷贝数据(SST,State Snapshot Transfer),作为
donor( 贡献者,如: 同步数据时的提供者)的节点在同步过程中无法提供读写
只支持innodb存储引擎的表

Galera Cluster官方文档:

http://galeracluster.com/documentation-webpages/galera-documentation.pdf
http://galeracluster.com/documentation-webpages/index.html
https://mariadb.com/kb/en/library/getting-started-with-mariadb-galera-cluster/

搭建实验

在 galera 模式下,多台 MariaDB对外提供服务,每一个服务器都是主节点,当数据操作在每一个 MariaDB 节点中都可以完成更新时,才响应客户请求。 通过这种方式确保数据同时又多份,可以有效防止单个 MariaDB 宕机。

MariaDB galera 官方站点:

下面我们演示一下三个服务器的 MariaDB 高可用方案, 我们准备三台刚安装好的 CentOS 8服务器,主机名分别为centos8-8 ,centos8-18, centos8-28, ip 地址分别为192.168.32.8,192.168.32.18,192.168.32.28

在centos8中自带的光盘仓库就有该软件包。直接安装 MariaDB-Galera-server ,配置一下 Galera 的选项,这里要卸载mariadb-server包,可能会产生冲突。最后在任意一台集群中的服务器使用 service mysql start --wsrep-new-cluster 启动服务即可
在实验前全部主机都要关闭防火墙以及selinux

主机对应IP:

centos8-8 :192.168.32.8
centos8-18 :192.168.32.18
centos8-28:192.168.32.28

在每台主机上面都安装mariadb-server-galera这个软件包。这里三个主机都要进行安装。

dnf install mariadb-server-galera -y

在centos8-8(192.168.32.8)主机中操作

[root@centos8-8 ~]#vim /etc/my.cnf.d/galera.cnf

# Logical cluster name. Should be the same for all nodes.#集群名字
wsrep_cluster_name="kaivi_wsrep_cluster"

# Group communication system handle
#wsrep_cluster_address="dummy://"
wsrep_cluster_address="gcomm://192.168.32.8,192.168.32.18,192.168.32.28"   #集群的主机

# Human-readable node name (non-unique). Hostname by default.
#wsrep_node_name=
wsrep_node_name=node1                             #集群的节点号

# listening ports and so on. Default: address of the first network interface.
#wsrep_node_address=
wsrep_node_address=192.168.32.8                  #集群的本主机

和 centos8-8一样,我们在其他2台主机上进行相同设置即可
在centos8-18(192.168.32.18)主机中操作

[root@centos8-18 ~]#vim /etc/my.cnf.d/galera.cnf
# Logical cluster name. Should be the same for all nodes.#集群名字
wsrep_cluster_name="kaivi_wsrep_cluster"

# Group communication system handle
#wsrep_cluster_address="dummy://"
wsrep_cluster_address="gcomm://192.168.32.8,192.168.32.18,192.168.32.28"   #集群的主机

# Human-readable node name (non-unique). Hostname by default.
#wsrep_node_name=
wsrep_node_name=node2                            #集群的节点号

# listening ports and so on. Default: address of the first network interface.
#wsrep_node_address=
wsrep_node_address=192.168.32.18                  #集群的本主机

在centos8-28(192.168.32.28)主机中操作

[root@centos8-28 ~]#vim /etc/my.cnf.d/galera.cnf
# Logical cluster name. Should be the same for all nodes.#集群名字
wsrep_cluster_name="kaivi_wsrep_cluster"

# Group communication system handle
#wsrep_cluster_address="dummy://"
wsrep_cluster_address="gcomm://192.168.32.8,192.168.32.18,192.168.32.28"   #集群的主机

# Human-readable node name (non-unique). Hostname by default.
#wsrep_node_name=
wsrep_node_name=node3                            #集群的节点号

# listening ports and so on. Default: address of the first network interface.
#wsrep_node_address=
wsrep_node_address=192.168.32.28                  #集群的本主机

第一个节点启动和其他都不一样。第一个只需要在任意一台集群中的服务器使用galera_new_cluster 启动服务而其他的主机只需要使用 systemctl start mariadb 启动即可

这里选用centos8-8为第一个启动节点。
在centos8-8(192.168.32.8)主机中操作

[root@centos8-8 ~]#galera_new_cluster
[root@centos8-8 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              32                      192.168.122.1:53                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:4567                       0.0.0.0:*
LISTEN        0              5                           127.0.0.1:631                        0.0.0.0:*
LISTEN        0              128                         127.0.0.1:6010                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:37603                      0.0.0.0:*
LISTEN        0              80                            0.0.0.0:3306                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:111                        0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*
LISTEN        0              5                               [::1]:631                           [::]:*
LISTEN        0              128                             [::1]:6010                          [::]:*
LISTEN        0              128                              [::]:40131                         [::]:*
LISTEN        0              128                              [::]:111                           [::]:*

[root@centos8-8 ~]#mysql    #这里区别于centos7,7中登入第一次需要看临时日志密码登入,强制要求第一次修改密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.11-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)

MariaDB [(none)]>

MariaDB [(none)]> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     | #其他节点还没有启动,所以暂时这里是1
+--------------------+-------+
1 row in set (0.001 sec)

除了第一个节点启动不同之外,其他的节点启动直接用 systemctl start mariadb 启动即可
在centos8-18(192.168.32.18)主机中操作

[root@centos8-18 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              32                      192.168.122.1:53                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              5                           127.0.0.1:631                        0.0.0.0:*
LISTEN        0              128                         127.0.0.1:6011                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:47467                      0.0.0.0:*
LISTEN        0              128                           0.0.0.0:111                        0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*
LISTEN        0              5                               [::1]:631                           [::]:*
LISTEN        0              128                             [::1]:6011                          [::]:*
LISTEN        0              128                              [::]:35375                         [::]:*
LISTEN        0              128                              [::]:111                           [::]:*
[root@centos8-18 ~]#systemctl start mariadb

[root@centos8-18 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              32                      192.168.122.1:53                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:4567                       0.0.0.0:*
LISTEN        0              5                           127.0.0.1:631                        0.0.0.0:*
LISTEN        0              128                         127.0.0.1:6011                       0.0.0.0:*
LISTEN        0              80                            0.0.0.0:3306                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:47467                      0.0.0.0:*
LISTEN        0              128                           0.0.0.0:111                        0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*
LISTEN        0              5                               [::1]:631                           [::]:*
LISTEN        0              128                             [::1]:6011                          [::]:*
LISTEN        0              128                              [::]:35375                         [::]:*
LISTEN        0              128                              [::]:111                           [::]:*
[root@centos8-18 ~]#

在centos8-28(192.168.32.28)主机中操作

[root@centos8-28 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              32                      192.168.122.1:53                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              5                           127.0.0.1:631                        0.0.0.0:*
LISTEN        0              128                         127.0.0.1:6010                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:111                        0.0.0.0:*
LISTEN        0              128                           0.0.0.0:42707                      0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*
LISTEN        0              5                               [::1]:631                           [::]:*
LISTEN        0              128                             [::1]:6010                          [::]:*
LISTEN        0              128                              [::]:35053                         [::]:*
LISTEN        0              128                              [::]:111                           [::]:*

[root@centos8-28 ~]#systemctl start mariadb
[root@centos8-28 ~]#
[root@centos8-28 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              32                      192.168.122.1:53                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:4567                       0.0.0.0:*
LISTEN        0              5                           127.0.0.1:631                        0.0.0.0:*
LISTEN        0              128                         127.0.0.1:6010                       0.0.0.0:*
LISTEN        0              80                            0.0.0.0:3306                       0.0.0.0:*
LISTEN        0              128                           0.0.0.0:111                        0.0.0.0:*
LISTEN        0              128                           0.0.0.0:42707                      0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*
LISTEN        0              5                               [::1]:631                           [::]:*
LISTEN        0              128                             [::1]:6010                          [::]:*
LISTEN        0              128                              [::]:35053                         [::]:*
LISTEN        0              128                              [::]:111                           [::]:*
[root@centos8-28 ~]

在centos8-8中查看一下节点状态,以及新建一个数据库,在其他节点分别查看是否同步
在centos8-8(192.168.32.8)主机中操作

MariaDB [(none)]> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 2     |  #第二个节点已经建立连接
+--------------------+-------+
1 row in set (0.001 sec)

MariaDB [(none)]> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |  #第三个节点已经建立连接
+--------------------+-------+
1 row in set (0.000 sec)

MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> create database db1;     #创建新的数据库db1
Query OK, 1 row affected (0.003 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

MariaDB [(none)]>

在centos8-18(192.168.32.18)主机中操作

[root@centos8-18 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.11-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]>

在centos8-28(192.168.32.28)主机中操作

[root@centos8-28 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.11-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

MariaDB [(none)]>

同步成功。自此,简单的Galere Cluster集群搭建成功。

扩展1:centos7中Galere Cluster集群搭建小结

#参考仓库:http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-5.5.63/yum/centos7-amd64/

yum install MariaDB-Galera-server

vim /etc/my.cnf.d/server.cnf
[galera]
wsrep_provider = /usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.32.7,192.168.32.17,192.168.32.27"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
#下面配置可选项
wsrep_cluster_name = 'mycluster' 默认my_wsrep_cluster
wsrep_node_name = 'node1'
wsrep_node_address = '192.168.32.7’
#首次启动时,需要初始化集群,在其中一个节点上执行命令
/etc/init.d/mysql start --wsrep-new-cluster
#而后正常启动其它节点
service mysql start
#查看集群中相关系统变量和状态变量
SHOW VARIABLES LIKE 'wsrep_%';
SHOW STATUS LIKE 'wsrep_%';
SHOW STATUS LIKE 'wsrep_cluster_size';

扩展2:Galere Cluster集群搭建新增节点

新增扩展节点主机位centos8-38:192.168.32.38
安装mariadb-server-galera这个软件包。前提也是防火墙关闭和selinux关闭。

dnf install mariadb-server-galera -y

在centos8-8(192.168.32.8)主机中操作,看下现在的节点状态

MariaDB [(none)]> show status like 'wsrep_cluster_size'\G
*************************** 1. row ***************************
Variable_name: wsrep_cluster_size
Value: 3                       #现在的节点个数还是3个,因为还没有新增节点
1 row in set (0.005 sec)
MariaDB [(none)]>

在centos8-38(192.168.32.38)主机中操作

[root@centos8-38 ~]#vim /etc/my.cnf.d/galera.cnf
# Logical cluster name. Should be the same for all nodes.#集群名字
wsrep_cluster_name="kaivi_wsrep_cluster"

# Group communication system handle
#wsrep_cluster_address="dummy://"
wsrep_cluster_address="gcomm://192.168.32.8,192.168.32.18,192.168.32.28,192.168.32.38"   #增加了新节点主机ip

# Human-readable node name (non-unique). Hostname by default.
#wsrep_node_name=
wsrep_node_name=node4                          #集群的节点号

# listening ports and so on. Default: address of the first network interface.
#wsrep_node_address=
wsrep_node_address=192.168.32.38               #集群的本主机

[root@centos8-38 ~]#systemctl start mariadb   启动数据库服务
[root@centos8-38 ~]#ss -ntl
State         Recv-Q         Send-Q                  Local Address:Port                  Peer Address:Port
LISTEN        0              128                           0.0.0.0:22                         0.0.0.0:*
LISTEN        0              128                           0.0.0.0:4567                       0.0.0.0:*
LISTEN        0              80                            0.0.0.0:3306                       0.0.0.0:*
LISTEN        0              128                              [::]:22                            [::]:*

在centos8-8(192.168.32.8)主机中操作,看下现在的节点状态

MariaDB [(none)]> show status like 'wsrep_cluster_size'\G
*************************** 1. row ***************************
Variable_name: wsrep_cluster_size
Value: 4                  #节点数已经增加到4个。
1 row in set (0.000 sec)

MariaDB [(none)]>
创建一个新库看是是否全部备份成功:

MariaDB [(none)]> create database dbkaivi;
Query OK, 1 row affected (0.010 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| dbkaivi            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

在centos8-38(192.168.32.38)主机中操作

[root@centos8-38 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.11-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| dbkaivi            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

MariaDB [(none)]>

到现在为止,增加新的节点只在节点服务器配置/etc/my.cnf.d/galera.cnf文件,在主节点服务器和其他节点服务器中的配置文件中wsrep_cluster_address="gcomm:// 这行是没有新增主机的ip地址的,但是也能正常的加入到集群。所以在正常生产中,假如了新节点之后,需要在其他的节点的配置文件中手动加上新增节点的ip地址。这里省略。

PXC集群部署

PXC基本概述

Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法。集群是有节点组成的,推荐配置至少3个节点,但是也可以运行在2个节点上。

PXC原理描述:
分布式系统的CAP理论:
C:一致性,所有的节点数据一致
A:可用性,一个或者多个节点失效,不影响服务请求
P:分区容忍性,节点间的连接失效,仍然可以处理请求
其实,任何一个分布式系统,需要满足这三个中的两个。

PXC的优点:
1)服务高可用;
2)数据同步复制(并发复制),几乎无延迟;
3)多个可同时读写节点,可实现写扩展,不过最好事先进行分库分表,让各个节点分别写不同的表或者库,避免让galera解决数据冲突;
4)新节点可以自动部署,部署操作简单;
5)数据严格一致性,尤其适合电商类应用;
6)完全兼容MySQL;

一些名词介绍:
WS:write set写数据集,写/更新事务
IST:Incremental State Transfer 增量同步
SST:State Snapshot Transfe 全量同步。
SST支持的方法有:mysqldump,rsync ,xtrabackup 。
mysqldump,rsync同步需要READ LOCK, (SST applies FLUSH TABLES WITH READ LOCK command)
xtrabackup 在整个同步数据过程中不需要READ LOCK。
配置参数:wsrep_sst_method=xtrabackup-v2
UUID:节点状态改变及顺序的唯一标识
GTID:Global Transaction ID,由UUID和sequence number偏移量组成。wsrep api中定义的集群内部全局事务id,用于记录集群中发生状态改变的唯一标识以及队列中的偏移量。
WSRWP API:在DBMS库和wsrep provider之间提供接口

PXC环境所涉及的端口:
#mysql实例端口:3306
#PXC cluster相互通讯的端口:4567
#用于SST传送的端口:4444
#用于IST传送的端口:4568

PXC集群搭建实验

在实验前全部主机都要关闭防火墙以及selinux

主机对应IP:

centos7-7-PXC1:192.168.32.7
centos7-17-PXC2:192.168.32.17
centos8-27-PXC3:192.168.32.27

在全部主机中都先配置yum源,用于安装PXC服务

[root@centos7 ~]#vim /etc/yum.repos.d/pxc.repo
[root@centos7 ~]#cat /etc/yum.repos.d/pxc.repo
[percona]
name=percona_repo
baseurl =https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
#清华大学的yum源
enabled = 1
gpgcheck = 0
[root@centos7 ~]#yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
centos7                                                                                                        | 3.6 kB  00:00:00     epel                                                                                                           | 5.3 kB  00:00:00     percona                                                                                                        | 2.9 kB  00:00:00     (1/3): epel/updateinfo                                                                                         | 1.0 MB  00:00:00     (2/3): epel/primary_db                                                                                         | 6.9 MB  00:00:01     (3/3): percona/7/x86_64/primary_db                                                                             | 1.0 MB  00:00:05     repo id                                    repo name                                                     status
centos7                                                           centos7                                                       10,097
epel                                                              aliyunEPEL                                                    13,483
percona/7/x86_64                                                  percona_repo                                                   1,900
repolist: 25,480

分别在三个节点都安装好PXC 5.7。注意:如果已经安装MySQL,必须卸载

[root@centos7 ~]#yum remove mariadb
[root@centos7-7-PXC1 ~]#yum install Percona-XtraDB-Cluster-57 -y
vim
[root@centos7-17-PXC2 ~]#yum install Percona-XtraDB-Cluster-57 -y

[root@centos7-27-PXC3 ~]#yum install Percona-XtraDB-Cluster-57 -y

PXC配置文件作用一览

/etc/percona-xtradb-cluster.conf.d/          #相关配置文件所在目录
mysqld.cnf                                   #数据库服务运行参数配置文件
mysqld_safe.cnf                              #Percona Server xx版本配置文件
wsrep.cnf                                    #PXC集群配置文件

配置文件各项配置意义:
配置说明

配置 说明
wsrep_provider 指定Galera库的路径
wsrep_cluster_name Galera集群的名称
wsrep_cluster_address Galera集群中各节点地址。地址使用组通信协议gcomm://(group communication)
wsrep_node_name 本节点在Galera集群中的名称
wsrep_node_address 本节点在Galera集群中的通信地址
wsrep_sst_method state_snapshot_transfer(SST)使用的传输方法,可用方法有mysqldump、rsync和xtrabackup,前两者在传输时都需要对Donor加全局只读锁(FLUSH TABLES WITH READ LOCK),xtrabackup则不需要(它使用percona自己提供的backup lock)。强烈建议采用xtrabackup
wsrep_sst_auth 在SST传输时需要用到的认证凭据,格式为:“用户:密码”
pxc_strict_mode 是否限制PXC启用正在试用阶段的功能,ENFORCING是默认值,表示不启用
binlog_format 二进制日志的格式。Galera只支持row格式的二进制日志
default_storage_engine 指定默认存储引擎。Galera的复制功能只支持InnoDB
innodb_autoinc_lock_mode 只能设置为2,设置为0或1时会无法正确处理死锁问题

在各个节点上分别配置mysql及集群配置文件wsrep.cnf
在 /etc/percona-xtradb-cluster.conf.d/ 目录中/etc/my.cnf为主配置文件,当前版本中,其余的配置文件都放在/etc/percona-xtradb-cluster.conf.d目录里,包括mysqld.cnf,mysqld_safe.cnf,wsrep.cnf 三个文件

在centos7-7-PCX1中操作:

#下面配置文件不需要修改
[root@centos7-7-PXC1 ~]#cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf

[client]
socket=/var/lib/mysql/mysql.sock
[mysqld]
server-id=1           #节点可以相同也可以不同,建议各个节点不用,好区分
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

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[root@centos7-7-PXC1 ~]#cat /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

#PXC的配置文件必须修改
[root@centos7-7-PXC1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so    #模块路径

# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://192.168.32.7,192.168.32.17,192.168.32.27       #多少节点就写几个节点地址

# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW       #要求采用行型的日志文件 不能是语句型或者混合型

# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB

# Slave thread to use
wsrep_slave_threads= 8

wsrep_log_conflicts

# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2

# Node IP address
#wsrep_node_address=192.168.70.63
wsrep_node_address=192.168.32.7      #当前集群的ip地址
# Cluster name
wsrep_cluster_name=pxc-cluster        #可以修改自己选用的名字

#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=pxc-cluster-node-1     #节点名字改为不一致即可

#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING

# SST method
wsrep_sst_method=xtrabackup-v2          #默认备份工具是xtarbackup

#Authentication for SST method
wsrep_sst_auth="sstuser:centos"          #对应的复制账户和密码
~
[root@centos7-7-PXC1 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so    #模块路径
wsrep_cluster_address=gcomm://192.168.32.7,192.168.32.17,192.168.32.27       #多少节点就写几个节点地址
binlog_format=ROW                      #要求采用行型的日志文件 不能是语句型或者混合型
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.32.7       #当前集群的ip地址
wsrep_cluster_name=pxc-cluster        #可以修改自己选用的名字
wsrep_node_name=pxc-cluster-node-1    #节点名字改为不一致即可
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2        #默认备份工具是xtarbackup
wsrep_sst_auth="sstuser:centos"       #对应的复制账户和密码
[root@centos7-7-PXC1 ~]#

注意:尽管Galera Cluster不再需要通过binlog的形式进行同步,但还是建议在配置文件中开启二进制
日志功能,原因是后期如果有新节点需要加入,老节点通过SST全量传输的方式向新节点传输数据,很
可能会拖垮集群性能,所以让新节点先通过binlog方式完成同步后再加入集群会是一种更好的选择。

在centos7-17-PCX2中操作:

[root@centos7-17-PXC2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://192.168.32.7,192.168.32.17,192.168.32.27

# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW

# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB

# Slave thread to use
wsrep_slave_threads= 8

wsrep_log_conflicts

# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2

# Node IP address
#wsrep_node_address=192.168.70.63
wsrep_node_address=192.168.32.17
# Cluster name
wsrep_cluster_name=pxc-cluster

#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=pxc-cluster-node-2

#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING

# SST method
wsrep_sst_method=xtrabackup-v2

#Authentication for SST method
wsrep_sst_auth="sstuser:centos"

在centos7-27-PCX3中操作:

[root@centos7-27-PXC3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so

# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://192.168.32.7,192.168.32.17,192.168.32.27

# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW

# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB

# Slave thread to use
wsrep_slave_threads= 8

wsrep_log_conflicts

# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2

# Node IP address
#wsrep_node_address=192.168.70.63
wsrep_node_address=192.168.32.27
# Cluster name
# Cluster name
wsrep_cluster_name=pxc-cluster

#If wsrep_node_name is not specified,  then system hostname will be used
wsrep_node_name=pxc-cluster-node-3

#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING

# SST method
wsrep_sst_method=xtrabackup-v2

#Authentication for SST method
wsrep_sst_auth="sstuser:centos"

启动PXC集群中第一个节点。这么可以选择在集群中的任意一个主机当作是PXC集群的第一个节点,这里选择centos7-7(192.168.32.7)为第一个节点。第一个节点启动的命令和其他节点启动的命令有所区别。第一个节点启动服务命令为
systemctl start mysql@bootstrap.service

[root@centos7-7-PXC1 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:111                                       *:*
LISTEN      0      128                           *:48307                                     *:*
LISTEN      0      5                 192.168.122.1:53                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      128                   127.0.0.1:631                                       *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                   127.0.0.1:6010                                      *:*
LISTEN      0      128                          :::111                                      :::*
LISTEN      0      128                          :::22                                       :::*
LISTEN      0      128                         ::1:631                                      :::*
LISTEN      0      128                          :::56312                                    :::*
LISTEN      0      100                         ::1:25                                       :::*
LISTEN      0      128                         ::1:6010                                     :::*
[root@centos7-7-PXC1 ~]#systemctl start mysql@bootstrap.service

[root@centos7-7-PXC1 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:111                                       *:*
LISTEN      0      128                           *:48307                                     *:*
LISTEN      0      5                 192.168.122.1:53                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      128                           *:4567                                      *:*
LISTEN      0      128                   127.0.0.1:631                                       *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                   127.0.0.1:6010                                      *:*
LISTEN      0      80                           :::3306                                     :::*
LISTEN      0      128                          :::111                                      :::*
LISTEN      0      128                          :::22                                       :::*
LISTEN      0      128                         ::1:631                                      :::*
LISTEN      0      128                          :::56312                                    :::*
LISTEN      0      100                         ::1:25                                       :::*
LISTEN      0      128                         ::1:6010                                     :::*
#重点关注3306端口和4567端口
[root@centos7-7-PXC1 ~]#


由于这里是数据库5.7的版本,默认第一次登入的密码需要查看临时日志找密码,然后登入之后强制修改对应的口令。

[root@centos7-7-PXC1 ~]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@centos7-7-PXC1 ~]#
#查找临时密码
[root@centos7-7-PXC1 ~]#grep "temporary password" /var/log/mysqld.log
2019-12-03T07:51:00.715111Z 1 [Note] A temporary password is generated for root@localhost: hx):Xy&PO3JU

[root@centos7mini 15:52:08 ~]#mysql -uroot -p'hx):Xy&PO3JU'  #用临时密码登入
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by 'centos';    #修改root账号新密码
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'centos';  #创建新的同步账号。这个密码需要和配置文件
中的wsrep_sst_auth="sstuser:centos"密码一致
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
#给sstuser账号授权
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
| sstuser       | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

在其他机器上面不用改,因为之后节点启动加入集群之后就会自动同步所有文件,也包括该密码口令和账号。

mysql> show variables like 'wsrep%'\G    #查看系统变量

mysql> show status like 'wsrep%'\G       #查看系统状态

mysql> show status like 'wsrep_local%';

mysql> show status like 'wsrep_ready%';

mysql> show status like 'wsrep_connected%';

说明:
wsrep_cluster_size表示,该Galera集群中节点的个数
wsrep_local_state_comment 状态为Synced(4),表示数据已同步完成(因为是第一个引导节点,无数
据需要同步)。 如果状态是Joiner, 意味着 SST 没有完成. 只有所有节点状态是Synced,才可以加新节点
wsrep_cluster_status为Primary,且已经完全连接并准备好。

启动PXC集群中其它所有节点。
其他节点的启动命令和第一个节点不一样,直接用systemctl start mysql启动即可。
在centos7-17-PCX2中操作:

[root@centos7-17-PXC2 ~]#systemctl start mysql
[root@centos7-17-PXC2 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:4567                                      *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      100                       [::1]:25                                     [::]:*
LISTEN      0      80                         [::]:3306                                   [::]:*
LISTEN      0      128                        [::]:22                                     [::]:*

在centos7-27-PCX3中操作:

[root@centos7-27-PXC3 ~]#systemctl start mysql

[root@centos7-27-PXC3 ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:4567                                      *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      100                       [::1]:25                                     [::]:*
LISTEN      0      80                         [::]:3306                                   [::]:*
LISTEN      0      128                        [::]:22                                     [::]:*

测试是否同步成功

查看集群状态,验证集群是否成功
这里可以选用任意的一个节点,因为成功之后都是同步的。这里选择centos7-7-PCX1中操作

mysql> show status like '%wsrep_size%';
Empty set (0.00 sec)

mysql> show status like 'wsrep_connected%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| wsrep_connected | ON    |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'wsrep_node_name';
+-----------------+--------------------+
| Variable_name   | Value              |
+-----------------+--------------------+
| wsrep_node_name | pxc-cluster-node-1 |
+-----------------+--------------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+--------------+
| Variable_name      | Value        |
+--------------------+--------------+
| wsrep_node_address | 192.168.32.7 |
+--------------------+--------------+
1 row in set (0.00 sec)

mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |  #集群中主机的个数
+--------------------+-------+
1 row in set (0.00 sec)

创建一个新的数据库db1,看其他的节点主机是否同步成功
centos7-7-PCX1中操作:

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

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

#利用Xshell软件,同时在三个节点数据库,在其中一个节点成功
mysql> create database db2;
Query OK, 1 row affected (0.00 sec)
#其他节点显示失败
mysql> create database db2;
ERROR 1007 (HY000): Can't create database 'db2'; database exists
#但是数据库都会同步,查看任意节点数据库db2都已经存在。

centos7-17-PCX2中操作:

[root@centos7-17-PXC2 ~]#mysql -uroot -p'centos'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

centos7-27-PXC3中操作:

[root@centos7-27-PXC3 ~]#mysql -uroot -p'centos'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

PXC集群搭成功!

扩展:PXC集群搭建新增节点

新增扩展节点主机位centos7-37:192.168.32.37
安装Percona-XtraDB-Cluster-57这个软件包。前提也是防火墙关闭和selinux关闭。

[root@centos7-37-PXC ~]#yum install Percona-XtraDB-Cluster-57 -y

在centos7-7(192.168.32.7)主机中操作,看下现在的节点状态

mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |  #显示节点主机的个数
+--------------------+-------+
1 row in set (0.00 sec)

在centos7-37(192.168.32.37)主机中操作

1 [mysqld]
2 # Path to Galera library
3 wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
4
5 # Cluster connection URL contains IPs of nodes
6 #If no IP is found, this implies that a new cluster needs to be created,
7 #in order to do that you need to bootstrap this node
8 wsrep_cluster_address=gcomm://192.168.32.7,192.168.32.17,192.168.32.27,192.168.32.37
#增加了新增节点192.168.32.37的ip地址
9
10 # In order for Galera to work correctly binlog format should be ROW
11 binlog_format=ROW
12
13 # MyISAM storage engine has only experimental support
14 default_storage_engine=InnoDB
15
16 # Slave thread to use
17 wsrep_slave_threads= 8
18
19 wsrep_log_conflicts
20
21 # This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
22 innodb_autoinc_lock_mode=2
23
24 # Node IP address
25 wsrep_node_address=192.168.32.37
26 # Cluster name
27 wsrep_cluster_name=pxc-cluster
28
29 #If wsrep_node_name is not specified,  then system hostname will be used
30 wsrep_node_name=pxc-cluster-node-4
31
32 #pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
33 pxc_strict_mode=ENFORCING
34
35 # SST method
36 wsrep_sst_method=xtrabackup-v2
37
38 #Authentication for SST method
39 wsrep_sst_auth="sstuser:centos"

在centos7-37(192.168.32.37)主机中操作

[root@centos7-37-PXC ~]#systemctl start mysql
[root@centos7-37-PXC ~]#ss -ntl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      128                           *:4567                                      *:*
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      100                       [::1]:25                                     [::]:*
LISTEN      0      80                         [::]:3306                                   [::]:*
LISTEN      0      128                        [::]:22                                     [::]:*

在centos7-7(192.168.32.7)主机中操作,看下现在的节点状态

mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 4     |
+--------------------+-------+
1 row in set (0.00 sec)

在centos7-37(192.168.32.37)主机中操作,查看是否同步数据库

[root@centos7-37-PXC ~]#mysql -uroot -p'centos'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql>

到现在为止,增加新的节点只在节点服务器配置/etc/percona-xtradb-cluster.conf.d/wsrep.cnf 文件,在主节点服务器和其他节点服务器中的配置文件中wsrep_cluster_address="gcomm:// 这行是没有新增主机的ip地址的,但是也能正常的加入到集群。所以在正常生产中,假如了新节点之后,需要在其他的节点的配置文件中手动加上新增节点的ip地址。这里省略。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Magedu-M39-李凯 发布了52 篇原创文章 · 获赞 0 · 访问量 660 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: