💡 PXC(Percona XtraDB Cluster)是一个开源的、高可用性的MySQL解决方案,它基于Galera集群技术,提供了内置的多主节点、同步复制和故障转移等功能。PXC支持无需共享存储就能实现可扩展性和高可用性,使得应用程序能够更好地应对高并发访问和故障恢复。PXC集群主要由两部分组成:Percona Server with XtraDB和Write Set Replication patches(同步、多主复制插件)
PXC集群的优点:
PXC特点:
PXC相应端口:
pxc1 | 10.29.134.67 | VIP:10.29.134.70 |
---|---|---|
pxc2 | 10.29.134.68 | |
pxc3 | 10.29.134.67 |
修改/etc/hosts
vim /etc/hosts
添加:
10.29.134.67 pxc1
10.29.134.68 pxc2
10.29.134.69 pxc3
另外两台主机相同,可以通过scp复制:
scp /etc/hosts root@10.29.134.68:/etc
💡 提前制作PXC的本地yum源 安装包需要下载Percona-XtraDB-Cluster,Xtrabackup,Percona-Toolkit
下载地址:https://www.percona.com/downloads
注意选择对应系统的版本
local.repo:
[Mysql_PXC]
name=mysql
baseurl=file:///home/zoms/mysql #创建PXC——yum仓库所在地址
enabled=1
gpgcheck=0
yum -y install socat qpress
yum -y install openssl socat procps-ng chkconfig procps-ng coreutils shadow-utils grep libaio libev libcurl perl-DBD-MySQL perl-Digest-MD5 libgcc libstdc++ libgcrypt libgpg-error zlib glibc openssl-libs
yum -y install percona-xtra* #一般会有冲突,可以将其他repo文件做为备份
yum install Percona-XtraDB*
#设置开机自启并启动
systemctl enable mysql
#获取初始密码
grep 'temporary password' /var/log/mysqld.log
#复制初始密码使用root账户登录,并修改其账户密码:
mysql -u root -p'初始密码'
#修改密码为‘123456’
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#在将其他节点添加到群集之前,需要登录当前节点,来为SST操作创建用户提供权限,命令如下:创建用户
mysql> CREATE USER 'pxc'@'localhost' IDENTIFIED BY '123456';
#授予权限
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'pxc'@'localhost';
#刷新权限
mysql> FLUSH PRIVILEGES;
#退出服务
mysql> exit
#停止服务:
systemctl stop mysql
以上安装过程在三台主机上均相同执行,之后就可以开始集群的搭建。
首先需要修改每个服务器上的 /etc/my.cnf 文件,第一个节点上的内容如下:
[mysqld]
server-id=1 #PXC集群中MySQL实例的唯一ID,不能重复,必须是数字
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
#这边需要找下文件 find / -name 'libgalera_smm.so'
wsrep_cluster_name=mysql1
wsrep_cluster_address=gcomm://10.29.134.67,10.29.134.68,192.168.1.30 #集群IP
wsrep_node_name=pxc1 #对应主机名
wsrep_node_address=10.29.134.67 #对应主机IP
wsrep_sst_method=xtrabackup-v2 #同步方法(mysqldump、rsync、xtrabackup
wsrep_sst_auth=sstuser:sstuser #之前设置的账号和密码
pxc_strict_mode=ENFORCING #同步严厉模式
binlog_format=ROW #基于ROW复制(安全可靠)
default_storage_engine=InnoDB #默认引擎
innodb_autoinc_lock_mode=2 #主键自增长不锁表
#第一个节点需要以引导模式启动:
systemctl start mysql@bootstrap.service
#接着在第二和第三个节点上正常启动数据库服务,命令如下:
systemctl start mysql
启动后,可以登录任意节点并使用以下命令查看集群状态。输出中的 wsrep_cluster_size 的值应该为 3 ,
即集群中节点总数为 3,此时代表所有节点都已成功加入集群,至此集群已经搭建完成。
mysql> show status like 'wsrep%';
PXC 集群允许动态下线节点,但需要注意的是节点的启动命令和关闭命令必须一致,如以引导模式启动的第一个节点必须
以引导模式来进行关闭:
systemctl stop mysql@bootstrap.service
其他节点则可以按照正常方式关闭:
service stop mysql
与上章节相同;
mkdir -p /app
cd /app
mkdir binlog
mkdir log
touch log/mysql.err
mkdir relaylog
mkdir data socket pid
mkdir tmp
mkdir undo
mkdir scripts
my.cnf具体配置如下:
[client]
port=3307
socket=/app/socket/mysqld.sock
default-character-set=utf8mb4
[mysql]
prompt=mysql(\\u@\\h:\\p [\\d])>
default-character-set=utf8mb4
[mysqld]
performance-schema-instrument='wait/lock/metadata/sql/mdl=ON'
lc_messages_dir=/usr/share
######## Global
user=mysql
port=3307
basedir=/bin
datadir=/app/data
server_id=1 #每个节点不同
socket=/app/socket/mysqld.sock
pid-file=/app/pid/mysqld.pid
default-storage-engine=INNODB
character-set-server=utf8mb4
collation-server=utf8mb4_bin
explicit_defaults_for_timestamp=on
log_timestamps=system
default_time_zone=system
skip_name_resolve=1
log_error_verbosity=3
lower_case_table_names=1
auto_increment_increment=3
auto_increment_offset=1 #每个节点设置不一样,三个节点各自设置为1,2,3
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION '
autocommit=1
transaction_isolation=READ-COMMITTED
max_allowed_packet=1024M
event_scheduler=1
######## password policy
#plugin_load_add='validate_password.so'
#loose-validate_password_policy=0
#loose-validate_password_length=4
######## Connection
interactive_timeout=1800
wait_timeout=1800
lock_wait_timeout=1800
max_connections=4000 #根据实际业务设置
max_connect_errors=1000000
open_files_limit=65535 #mysqld能打开的文件数
######## Table cache
table_open_cache=16384
table_definition_cache=4096
table_open_cache_instances=64
######## Session memory
read_buffer_size=1M
read_rnd_buffer_size=1M
sort_buffer_size=1M
tmp_table_size=1024M
max_heap_table_size=1024M
join_buffer_size=1M
thread_cache_size=64
######## Binlog
binlog_format=row
log_bin=/app/binlog/mysql-bin
relay_log=/app/relaylog/relay-bin
log_slave_updates=on
binlog_checksum=none
sync_binlog=1
log_bin_trust_function_creators=1
binlog_gtid_simple_recovery=1
binlog_cache_size=1024M
max_binlog_cache_size=2048M
optimizer_switch='use_index_extensions=off'
######## input/output dir
#secure_file_priv =/app/tmp
######## Error, general, slow query logs
log-error=/app/log/mysql.err
general_log=off
general_log_file=/app/log/general.log
slow_query_log=on
slow_query_log_file=/app/log/slow.log
long_query_time=0.3 #慢查时间
log_slow_admin_statements=1 #记录DDL语句
log_queries_not_using_indexes=1 #记录没有使用索引的语句
log_throttle_queries_not_using_indexes=20 #每分钟记录没有使用索引的语句的个数限制
######## GTID
gtid_mode=on
enforce_gtid_consistency=on
######## Replication
#skip_replica_start=1
slave_parallel_type=logical_clock
slave_parallel_workers=16 #设置CPU的两倍
relay_log_recovery=1
slave_preserve_commit_order=1
slave_transaction_retries=128
sync_relay_log=0
sync_relay_log_info=0
######## innodb
default_storage_engine=innodb
default_tmp_storage_engine=innodb
innodb_data_file_path=ibdata1:12M:autoextend
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:20G ##max设置为数据盘的5%-10%
innodb_buffer_pool_filename=ib_buffer_pool
innodb_log_group_home_dir=/app/data
innodb_log_files_in_group=3
innodb_log_file_size=2048M
innodb_file_per_table=on
innodb_online_alter_log_max_size=1G
innodb_open_files=4096
innodb_page_size=16k
innodb_thread_concurrency=0
innodb_page_cleaners=16
innodb_print_all_deadlocks=on
innodb_deadlock_detect=on
innodb_lock_wait_timeout=5
innodb_spin_wait_delay=128
innodb_autoinc_lock_mode=2
innodb_io_capacity=2000
innodb_io_capacity_max=5000
innodb_lru_scan_depth=4096
innodb_rollback_segments=128
innodb_undo_directory=/app/undo
innodb_log_buffer_size=128M
innodb_strict_mode=1
innodb_sort_buffer_size=67108864
innodb_undo_log_truncate=1
innodb_max_undo_log_size=1G
innodb_purge_rseg_truncate_frequency=128
innodb_purge_threads=8
innodb_write_io_threads=32
innodb_read_io_threads=32
innodb_doublewrite=0
innodb_max_dirty_pages_pct=5
######## Persistent Optimizer Statistics
innodb_stats_auto_recalc=on
innodb_stats_persistent=on
innodb_stats_persistent_sample_pages=64
innodb_adaptive_hash_index=on
innodb_change_buffering=all
innodb_change_buffer_max_size=25
innodb_flush_neighbors=0
innodb_flush_method=O_DIRECT
innodb_log_buffer_size=128M
innodb_flush_log_at_timeout=1
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=8G #内存的70%
innodb_buffer_pool_instances=8
autocommit=1
######## innodb scan resistant
innodb_old_blocks_pct=37
innodb_old_blocks_time=1000
######## innodb read ahead
innodb_read_ahead_threshold=56
innodb_random_read_ahead=OFF
######## innodb buffer pool state
innodb_buffer_pool_dump_pct=40
innodb_buffer_pool_dump_at_shutdown=ON #MySQL服务器关闭时记录在InnoDB缓冲池中缓存的页面,以便在下次重新启动时缩短预热过程
innodb_buffer_pool_load_at_startup=ON
######## PXC
pxc_encrypt_cluster_traffic=0
wsrep_provider_options="gcs.fc_limit=256;gcs.fc_factor=1.0;gcs.fc_master_slave=yes;gcache.size=10G"
#bind-address=10.29.134.67 #每个节点自身的IP
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://10.29.134.67,10.29.134.68,10.29.134.69
wsrep_slave_threads=8
wsrep_log_conflicts=1
wsrep_node_address=10.29.134.67 #每个节点自身的IP
wsrep_node_name=co3t-134-67-cos-premysql1 #每个节点的hostname
wsrep_sst_auth=sstuser:sstuser #8.0的不需要创建这个用户
wsrep_cluster_name=PXC #集群名称,每个节点必须是一样的名字
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
并修改/app的属主:
chown -R mysql:mysql /app
主节点67:
#初始化mysql
mysqld --defaults-file=/app/my.cnf --user=mysql --initialize
echo $? #等于0代表状态正常
#启动mysql
mysqld_safe --defaults-file=/app/my.cnf --user=mysql --wsrep_new_cluster &
#登录
cat /app/log/mysql.err|grep password #获取临时密码
mysql -uroot -p‘临时密码’ -S /app/socket/mysqld.sock
#初次登陆用户需要修改密码:
ALTER USER root@'localhost' IDENTIFIED BY 'mysql@123';
#进程查询
ps -aux|grep mysqld
#创建SST用户(important)
grant all on *.* to sstuser identified by 'sstuser';
FLUSH PRIVILEGES;
#查询用户
select user,host from mysql.user;
其他两个节点等引导节点启动完了之后再启动
mysqld_safe --defaults-file=/app/my.cnf --user=mysql &
#密码已经同步,无需查询临时密码
#确定高可用状态
mysql> show global status like 'wsrep_cluster%';
#验证PXC是否正常同步
mysql> create database test;
#所有节点上都能查到这个库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| employees |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
yum install keepalived
修改/etc/keepalived/keepalived.conf文件(三个节点有不同之处)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id PXC
}
vrrp_syns_group pxc1 {
group {
pxc1
}
}
vrrp_script check_apps {
script "/sbin/ha_check 3307 274b9ecd-52cc-11ee-9d09-83a19857f84a" #需要留意
interval 3
}
vrrp_instance pxc1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 69
priority 100 #修改100 95 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.29.134.70/24 dev eth0 scope global label eth0:1
}
track_script {
check_apps
}
}
这边注意 script "/sbin/ha_check 3307 274b9ecd-52cc-11ee-9d09-83a19857f84a"
1、/sbin/ha_check
chmod +x /sbin/ha_check
要保证有这个文件
链接: https://pan.baidu.com/s/12DpVifSHLWqoWkuMknYt3g 提取码: o8na
2、Mysql该进程的UUID
cd /app/data/
cat grastate.dat
mysql>CREATE USER 'keepalived'@'127.0.0.1' IDENTIFIED by '1jian8Shu!’;
#这边所用的ha_check脚本是封装好的,密码必须是这个
mysql>GRANT service_connection_admin ON *.* TO 'keepalived'@'127.0.0.1';
mkdir -p /mysql/data3307/log
systemctl restart keepalived;
systemctl status keepalived; #active(running)
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-09-15 09:20:45 +08; 2h 10min ago
Process: 141368 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 141369 (keepalived)
CGroup: /system.slice/keepalived.service
├─141369 /usr/sbin/keepalived -D
├─141370 /usr/sbin/keepalived -D
└─141371 /usr/sbin/keepalived -D
#查询日志
tail -200f /mysql/data3307/log/...log
2023-09-15 11:32:59 The cluster online nodes is 3
2023-09-15 11:33:02 The mysqld service is up
2023-09-15 11:33:02 The current node is primary
2023-09-15 11:33:02 The cluster online nodes is 3
2023-09-15 11:33:05 The mysqld service is up
2023-09-15 11:33:05 The current node is primary
2023-09-15 11:33:05 The cluster online nodes is 3
2023-09-15 11:33:08 The mysqld service is up
2023-09-15 11:33:08 The current node is primary
2023-09-15 11:33:08 The cluster online nodes is 3
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。