galera产品是以galera cluster方式为mysql提高高可用集群解决方案的。galera cluster就是集成了galera插件的mysql集群。
galera replication是codership提供的mysql数据同步方案,具有高可用性,方便扩展,并且可以实现多个mysql节点间的数据同步复制与读写,可保障数据库的服务高可用及数据强一致性。
备注:donor节点就是数据的贡献者,如果一个新节点加入集群,此时又需要大量数据的SST数据传
输,就有可能因此而拖垮整个集群的性能,所以在生产环境中,如果数据量较小,还可以使用SST全量
数据传输,但如果数据量很大就不建议使用这种方式,可以考虑先建立主从关系,然后再加入集群。
(3)节点的数据传输方式:
pxc1:192.168.26.17
pxc1:192.168.26.27
pxc1:192.168.26.37
pxc1:192.168.26.47
----------------------------------
//关闭防火墙
systemctl disable firewalld
//关闭selinux
setenforce 0 #临时关闭
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #改配置文件
//时间同步
yum install -y ntpdate #安装服务
systemctl start ntpdate #开启服务
ntpdate 域名/IP #同步时间
-----------------------------------
注意:如果已经安装MySQL,必须卸载
//配置yum源
vim /etc/yum.repos.d/pxc.repo
//编辑配置文件添加以下数据
[percona]
name=percona_repo
baseurl =https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
enabled = 1
gpgcheck = 0
--------------------------------------
//把配置的yum源仓库发放至其他pxc主机上
[root@pxc1 ~]# scp /etc/yum.repos.d/pxc.repo 192.168.26.27:/etc/yum.repos.d/
[root@pxc1 ~]# scp /etc/yum.repos.d/pxc.repo 192.168.26.37:/etc/yum.repos.d/
[root@pxc1 ~]# scp /etc/yum.repos.d/pxc.repo 192.168.26.47:/etc/yum.repos.d/
---------------------------------------
//在每台主机上安装percona服务
[root@pxc1 ~]# yum -y install Percona-XtraDB-Cluster-57
/etc/my.cnf为主配置文件,当前版本中,其余的配置文件都放在/etc/percona-xtradb-cluster.conf.d目
录里,包括mysqld.cnf,mysqld_safe.cnf,wsrep.cnf 三个文件
[root@pxc1 ~]# cat /etc/my.cnf
#
# The Percona XtraDB Cluster 5.7 configuration file.
#
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
# Please make any edits and changes to the appropriate sectional files
# included below.
#
!includedir /etc/my.cnf.d/
!includedir /etc/percona-xtradb-cluster.conf.d/
//分别查看以上两个路径中的文件
-----------------------------------------
[root@pxc1 ~]# ll /etc/my.cnf.d/
total 0
//查看得这个路径有文件,继续
[root@pxc1 ~]# ll /etc/percona-xtradb-cluster.conf.d/
total 12
-rw-r--r-- 1 root root 381 Sep 16 14:40 mysqld.cnf
-rw-r--r-- 1 root root 440 Sep 16 14:40 mysqld_safe.cnf
-rw-r--r-- 1 root root 1066 Sep 16 14:40 wsrep.cnf
-----------------------------------------
//下列配置文件可不作修改
[root@pxc1 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf # Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock
[mysqld]
server-id=17 #建议每个每个节点不同个,我就用主机id后两位了
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@pxc1 ~]# cat /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
#
# The Percona Server 5.7 configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/lib/mysql/mysql.sock
nice = 0
-----------------------------------------
//下列pxc配置文件必须修改
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.26.17,192.168.26.27,192.168.26.37 #三个节点的IP
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.26.17 #各个节点,指定自已的IP
wsrep_cluster_name=pxc_magedu #群组名,可指定
wsrep_node_name=pxc_magedu_pxc1 #各个节点,指定自已节点名称
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="bokebi:centos" #取消本行注释,用户,密码
----------------------------------------
//查看配置文件
[root@pxc1 ~]# egrep -v "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.26.17,192.168.26.27,192.168.26.37
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.26.17 本机IP
wsrep_cluster_name=pxc_magedu #群组名
wsrep_node_name=pxc_magedu_pxc1 #节点名
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="bokebi:centos" #用户名,密码
注意:尽管Galera Cluster不再需要通过binlog的形式进行同步,但还是建议在配置文件中开启二进制
日志功能,原因是后期如果有新节点需要加入,老节点通过SST全量传输的方式向新节点传输数据,很
可能会拖垮集群性能,所以让新节点先通过binlog方式完成同步后再加入集群会是一种更好的选择
//启动第一人节点
[root@pxc1 ~]# systemctl start [email protected]
---------------------------------------
//使用个grep搜索mysql日志文件中的临时登录密码
[root@pxc1 ~]# grep "temporary password" /var/log/mysqld.log
2019-12-02T14:20:00.617731Z 1 [Note] A temporary password is generated for root@localhost: X2Es)h,:Tzl%
//如果出现如上现象,没有临时登录密码时可在mysql配置文件中加入以下选项
vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
//编辑添加以下选项
[myaqld]
skip_grant_tables=1 #跳过授权表
//空密码直接登录mysql
[root@pxc1 ~]# mysql -uroot
···此处省略登录信息···
mysql>
---------------------------------------
//直接修改密码,可以看出来,显示的是我们开启了跳过授权表,所以无法执行语句
mysql> ALTER user root@'localhost' IDENTIFIED BY 'centos';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
//出现以上情况,别慌,执行一下刷新权限即可
mysql> FLUSH PRIVILEGES; #刷新权限
//再执行一次修改密码,可以看到已经修改成功
mysql> ALTER user root@'localhost' IDENTIFIED BY 'centos';
Query OK, 0 rows affected (0.00 sec)
//别忘了把配置文件中的跳过授权表删除,或者直接注释掉也是可以的
[myaqld]
#skip_grant_tables=1
//退出,使用修改的密码进行重新登录
mysql> exit
Bye
[root@pxc1 ~]# mysql -uroot -pcentos
···此处省略登录信息了···
mysql>
//创建与配置文件wsrep.cnf中相同的用户和密码
mysql> CREATE user bokebi@'localhost' IDENTIFIED BY 'centos';
Query OK, 0 rows affected (0.00 sec)
//给新创建的用户授予相关的权限
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO bokebi@'localhost';
Query OK, 0 rows affected (0.01 sec)
//刷新新创建用户的权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
//查看相关变量
mysql> SHOW VARIABLES LIKE 'wsrep%'\G;
*************************** 1. row ***************************
Variable_name: wsrep_OSU_method
Value: TOI
···此处省略更多信息···
----------------------------------------
//查看相关变量状态
mysql> SHOW STATUS LIKE 'WSREP%'\G;
*************************** 1. row ***************************
Variable_name: wsrep_local_state_uuid
Value: d7ee9cf6-150e-11ea-b628-d695cdaf4abe
···此处忽略更多信息···
----------------------------------------
//重点关注一下内容
mysql> SHOW STATUS LIKE 'WSREP%';
+----------------------------------+---------------------------------------+
| Variable_name | Value |
+----------------------------------+---------------------------------------+
| wsrep_local_state_uuid | d7ee9cf6-150e-11ea-b628-d695cdaf4abe |
···省略部分信息···
//本地状态
| wsrep_local_state | 4 |
//本地状态评论
| wsrep_local_state_comment | Synced |
···省略部分信息···
//群组成员个数
| wsrep_cluster_size | 1 |
···省略部分信息···
//群组身份
| wsrep_cluster_status | Primary |
//连接项
| wsrep_connected | ON |
···省略部分信息···
//准备项
| wsrep_ready | ON |
+----------------------------------+---------------------------------------+
说明:
wsrep_cluster_size表示,该Galera集群中只有一个节点
wsrep_local_state_comment 状态为Synced(4),表示数据已同步完成(因为是第一个引导节点,无数
据需要同步)。 如果状态是Joiner, 意味着 SST 没有完成. 只有所有节点状态是Synced,才可以加新节点
wsrep_cluster_status为Primary,且已经完全连接并准备好
mysql> SHOW VARIABLES LIKE 'wsrep_node_name';
+-----------------+-----------------+
| Variable_name | Value |
+-----------------+-----------------+
| wsrep_node_name | pxc_magedu_pxc1 |
+-----------------+-----------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+---------------+
| Variable_name | Value |
+--------------------+---------------+
| wsrep_node_address | 192.168.26.17 |
+--------------------+---------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'wsrep_on';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on | ON |
+---------------+-------+
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)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//在任意节点创建数据库
[root@pxc1 ~]# mysql -uroot -pcentos;
----------------------------------
mysql> CREATE DATABASE testdb1;
Query OK, 1 row affected (0.01 sec)
//在pxc2节点查看
[root@pxc2 ~]# mysql -uroot -pcentos;
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb1 |
+--------------------+
5 rows in set (0.00 sec)
//在pxc3节点查看
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb1 |
+--------------------+
5 rows in set (0.00 sec)
//利用Xshell软件,同时在三个节点数据库,在其中一个节点成功
mysql> create database testdb2;
Query OK, 1 row affected (0.01 sec)
//在其它节点都提示失败
mysql> create database testdb2;
ERROR 1007 (HY000): Can't create database 'testdb2'; database exists