- 系统选择
- Percona XtraDB Cluster
- 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.
- 1.默认载入以下文件
- 1.1 该文件为空
- !includedir /etc/my.cnf.d/
- 1.2 有三份文件 mysqld.cnf mysqld_safe.cnf wsrep.cnf
- !includedir /etc/percona-xtradb-cluster.conf.d/
- 为了简便,直接拷贝上述内容到这里进行修改
- 2.1 mysqld.cnf
- Template my.cnf for PXC
- Edit to your requirements.
- 服务端字符集
- 可以远端连接
- 跳过 DNS 解析
- pxc 集群中 mysql 的唯一 id,不能重复,且必须是数字
- Disabling symbolic-links is recommended to prevent assorted security risks
- wsrep.cnf 也是写在 [mysqld] 下
- Path to Galera library
- 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
- 集群所有节点地址,逗号分割
- In order for Galera to work correctly binlog format should be ROW
- 基于 ROW 进行 binlog 复制
- MyISAM storage engine has only experimental support
- 默认引擎
- Slave thread to use
- 并行复制线程数,一般是当前 cpu 的 1~1.5 倍,减少流控次数可以设置该值
- wsrep_slave_threads= 8
- wsrep_log_conflicts
- This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
- 主键自增不锁表
- Node IP address
- 当前节点的 ip
- Cluster name
- 集群名称,自定义
- If wsrep_node_name is not specified, then system hostname will be used
- 当前节点的名称,自定义
- pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
- 同步模式,严厉
- SST method
- 同步方法 (mysqldump rsync xtrabackup)
- Authentication for SST method
- 同步使用的账号
- mysqld_safe.cnf
- [mysqld_safe]
- pid-file = /var/run/mysqld/mysqld.pid
- socket = /var/lib/mysql/mysql.sock
- nice = 0
系统选择
- 同步复制,事务再所有集群节点要么同时提交,要么都不提交
- 具有数据同步的强一致性,即所有节点数据保持一致
- 多主复制,没有主从之分,所有节点都可以写入数据
注意点
- 规模不能太大,毕竟需要进行数据同步
- 数据同步取决于配置最低的节点,所以所有节点的硬件配置要相同
- 只支持 Innodb 引擎
开放端口
端口 | 描述 |
---|---|
3306 | db 端口 |
4444 | 请求全量同步 (SST) 端口 |
4567 | 数据库节点之前通信端口 |
4568 | 请求增量同步 (IST) 端口 |
关闭 selinux
vim /etc/selinux/config
SELINUX=disabled
下载 pxc
- https://www.percona.com/downloads/Percona-XtraDB-Cluster-57/LATEST/ 下载 pxc bundle 包
- https://centos.pkgs.org/7/percona-x86_64/qpress-11-1.el7.x86_64.rpm.html 下载 qpress
安装
- pxc 软件包默认带有 percona server
- 需要提前删除 mariadb 的依赖,
yum remove -y maria*
-
报错
出现缺失 percona-xtrabackup-24 ```shell Error: Package: Percona-XtraDB-Cluster-server-57-5.7.29-31.43.1.el7.x86_64 (/Percona-XtraDB-Cluster-server-57-5.7.29-31.43.1.el7.x86_64)
Requires: percona-xtrabackup-24 >= 2.4.20
- [https://www.percona.com/downloads/Percona-XtraBackup-2.4/LATEST/](https://www.percona.com/downloads/Percona-XtraBackup-2.4/LATEST/) **安装 2.4 版本**
<a name="vmJs5"></a>
### 先运行一下
> systemctl start mysqld
<a name="aIHNS"></a>
### 禁止 mysql 自动启动
> chkconfig mysqld off
- pxc 集群中,一个 mysql 如果宕机,长时间后重启,此时需要同步大量数据,pxc 集群会限制其他 mysql 节点的写入服务
- 应该关闭 mysql 的自动重启,一旦出现宕机,自行拷贝数据进行恢复,再重启
<a name="4iOVG"></a>
### 获取 root 密码
> cat /var/log/mysqld.log | grep "A temporary password"
```shell
2020-05-10T16:21:05.688684Z 1 [Note] A temporary password is generated for root@localhost: mYd#Pk/5oC%9
修改 root 密码
mysql_secure_installation
- 初始密码要8位,大小写,数字,特殊符号,否则报错
Your password does not satisfy the current policy requirements
- 可以设置,但是我懒
- 出现是否禁止 root 远程连接,我这里设置为 y ,后面貌似有点问题,所以我设置为 n 了,后期再修改
创建远程连接账号
CREATE USER ‘admin’@’%’ IDENTIFIED BY “Root666.”; GRANT all privileges ON . TO ‘admin’@’%’; FLUSH PRIVILEGES;
修改 Mysql 配置文件
- 除了字符集等指令,还包括了其他 pxc 指令
```bash
#
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.
#1.默认载入以下文件
1.1 该文件为空
!includedir /etc/my.cnf.d/
1.2 有三份文件 mysqld.cnf mysqld_safe.cnf wsrep.cnf
!includedir /etc/percona-xtradb-cluster.conf.d/
为了简便,直接拷贝上述内容到这里进行修改
#
2.1 mysqld.cnf
#
Template my.cnf for PXC
Edit to your requirements.
[client] socket=/var/lib/mysql/mysql.sock
[mysqld]
服务端字符集
character_set_server = utf8mb4
可以远端连接
bind-address = 0.0.0.0
跳过 DNS 解析
skip-name-resolve
pxc 集群中 mysql 的唯一 id,不能重复,且必须是数字
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
#
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.1.201,192.168.1.202,192.168.1.203
In order for Galera to work correctly binlog format should be ROW
基于 ROW 进行 binlog 复制
binlog_format=ROW
MyISAM storage engine has only experimental support
默认引擎
default_storage_engine=InnoDB
Slave thread to use
并行复制线程数,一般是当前 cpu 的 1~1.5 倍,减少流控次数可以设置该值
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
当前节点的 ip
wsrep_node_address=192.168.1.201
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
同步方法 (mysqldump rsync xtrabackup)
wsrep_sst_method=xtrabackup-v2
Authentication for SST method
同步使用的账号
wsrep_sst_auth=”admin:Root666.”
#
mysqld_safe.cnf
#
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/lib/mysql/mysql.sock
nice = 0
启动集群
- 修改 my.cnf 中的
server_id
等 - 选择一个节点,键入
systemctl start mysql@bootstrap.service
- 其他正常启动即可,即
systemctl start mysql
- 都可以查看配置中定义的 log 日志查看信息
/var/logs/mysqld.log
常见错误
- 掉电启动无法重启集群,看
/var/log/mysqld.log
建议,对/var/lib/mysql/grastate.dat
的 safe_xxx 项设置为 1 - 手动新建
/var/log/mysqld.log
,导致 db 无法启动,使用chown mysql:mysql /var/log/mysqld.log
切换所属 - 拷贝虚拟机,无法启动,看日志提示 -98,看了下 mysql 进程,发现有 pxc 的守护进程,直接干掉,重启
- 禁止 mysql 的开机自启动
chkconfig mysqld o
- 禁止 mysql 的开机自启动