ubuntu24实现单节点mysql mgr配置

背景

本地主机性能不足开启虚拟机后总是容易卡死,将mgr集群迁移到本地服务器上,在本地服务器上实现单节点mgr集群,用来测试项目。

先安装mysql实例

具体安装参考文章:https://blog.csdn.net/warton88/article/details/156641727?spm=1001.2014.3001.5502

创建必要目录

复制代码
root@mzi:/www/tools/mysql-8.0.36# cd /www
root@mzi:/www# mkdir -p /www/mysql-mgr/{mysql-master,mysql-slave1,mysql-slave2}/{conf,data,logs,tmp,binlogs}

root@mzi:/www# chmod 755 /www
root@mzi:/www# chown -R mysql:mysql /www/mysql-mgr

初始化数据目录

复制代码
root@mzi:/www# service mysql stop

# 更新 AppArmor 配置‌(关键步骤)
root@mzi:/www# vim /etc/apparmor.d/usr.sbin.mysqld

# 添加如下内容
/www/mysql-mgr/ r,
/www/mysql-mgr/** rwk,
/www/mysql-mgr/{mysql-master,mysql-slave1,mysql-slave2}/data/ r,
/www/mysql/{mysql-master,mysql-slave1,mysql-slave2}/data/** rwk,
/www/mysql/{mysql-master,mysql-slave1,mysql-slave2}/logs/ rw,
/www/mysql/{mysql-master,mysql-slave1,mysql-slave2}/tmp/ rw,

root@mzi:/www# systemctl reload apparmor

root@mzi:/www# sudo -u mysql mysqld --initialize-insecure --datadir=/www/mysql-mgr/mysql-master/data
root@mzi:/www# sudo -u mysql mysqld --initialize-insecure --datadir=/www/mysql-mgr/mysql-slave1/data
root@mzi:/www# sudo -u mysql mysqld --initialize-insecure --datadir=/www/mysql-mgr/mysql-slave2/data

配置三个节点的cnf

配置master节点

复制代码
root@mzi:/www# vim /www/mysql-mgr/mysql-master/conf/master.cnf

[mysqld]
datadir = /www/mysql-mgr/mysql-master/data
socket = /www/mysql-mgr/mysql-master/mysql.sock
log-error = /www/mysql-mgr/mysql-master/logs/error.log
pid-file = /www/mysql-mgr/mysql-master/mysql.pid
tmpdir = /www/mysql-mgr/mysql-master/tmp
port = 3306

# ========================
# 内存优化配置 (48核64G专属)
# ========================
# 缓冲池 master占50%,剩余从占25%, 总数占用不超过48G
innodb_buffer_pool_size  = 24G
innodb_buffer_pool_instances = 16  # 匹配CPU核心数

# 连接与线程优化
max_connections          = 1000     # 避免内存耗尽
thread_cache_size        = 16
table_open_cache         = 2048
table_definition_cache   = 512

# InnoDB引擎优化
innodb_log_file_size     = 4G
innodb_log_buffer_size   = 2G
innodb_flush_log_at_trx_commit = 1
innodb_flush_method      = O_DIRECT
innodb_file_per_table    = 1
innodb_thread_concurrency= 0      # 自动优化

# 临时表与排序优化
tmp_table_size           = 128M
max_heap_table_size      = 128M
sort_buffer_size         = 128M
join_buffer_size         = 128M
read_buffer_size         = 128M
read_rnd_buffer_size     = 128M

# ========================
# 安全与维护配置
# ========================
skip_name_resolve        = ON
log_slow_extra           = ON
long_query_time          = 2
log_queries_not_using_indexes = OFF
max_allowed_packet       = 128M
transaction_isolation    = READ-COMMITTED

# ========================
# MGR 集群核心配置
# ========================
server_id = 1  
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_checksum = NONE
log_slave_updates = ON
binlog_format = ROW
transaction_write_set_extraction = XXHASH64

# MGR专用配置
loose-group_replication_group_name = "161ec121-861b-4f61-b6ed-c969087cc1be" 
loose-group_replication_start_on_boot = OFF
loose-group_replication_recovery_get_public_key = ON
loose-group_replication_recovery_use_ssl = ON
loose-group_replication_local_address = "127.0.0.1:33061" 
loose-group_replication_group_seeds = "127.0.0.1:33061,127.0.0.1:33062,127.0.0.1:33063"
loose-group_replication_bootstrap_group = OFF

[mysql]
socket=/www/mysql-mgr/mysql-master/mysql.sock

[client]
socket=/www/mysql-mgr/mysql-master/mysql.sock

配置slave1节点

复制代码
root@mzi:/www# vim /www/mysql-mgr/mysql-slave1/conf/slave1.cnf

[mysqld]
datadir = /www/mysql-mgr/mysql-slave1/data
socket = /www/mysql-mgr/mysql-slave1/mysql.sock
log-error = /www/mysql-mgr/mysql-slave1/logs/error.log
pid-file = /www/mysql-mgr/mysql-slave1/mysql.pid
tmpdir = /www/mysql-mgr/mysql-slave1/tmp
port = 3307

# ========================
# 内存优化配置 (48核64G专属)
# ========================
# 缓冲池 master占50%,剩余从占25%, 总数占用不超过48G
innodb_buffer_pool_size  = 12G
innodb_buffer_pool_instances = 12  # 匹配CPU核心数

# 连接与线程优化
max_connections          = 1000     # 避免内存耗尽
thread_cache_size        = 16
table_open_cache         = 2048
table_definition_cache   = 512

# InnoDB引擎优化
innodb_log_file_size     = 4G
innodb_log_buffer_size   = 2G
innodb_flush_log_at_trx_commit = 1
innodb_flush_method      = O_DIRECT
innodb_file_per_table    = 1
innodb_thread_concurrency= 0      # 自动优化

# 临时表与排序优化
tmp_table_size           = 128M
max_heap_table_size      = 128M
sort_buffer_size         = 128M
join_buffer_size         = 128M
read_buffer_size         = 128M
read_rnd_buffer_size     = 128M

# ========================
# 安全与维护配置
# ========================
skip_name_resolve        = ON
log_slow_extra           = ON
long_query_time          = 2
log_queries_not_using_indexes = OFF
max_allowed_packet       = 128M
transaction_isolation    = READ-COMMITTED

# ========================
# MGR 集群核心配置
# ========================
server_id = 2
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_checksum = NONE
log_slave_updates = ON
binlog_format = ROW
transaction_write_set_extraction = XXHASH64

# MGR专用配置
loose-group_replication_group_name = "161ec121-861b-4f61-b6ed-c969087cc1be" 
loose-group_replication_start_on_boot = OFF
loose-group_replication_recovery_get_public_key = ON
loose-group_replication_recovery_use_ssl = ON
loose-group_replication_local_address = "127.0.0.1:33062" 
loose-group_replication_group_seeds = "127.0.0.1:33061,127.0.0.1:33062,127.0.0.1:33063"
loose-group_replication_bootstrap_group = OFF

[mysql]
socket=/www/mysql-mgr/mysql-slave1/mysql.sock

[client]
socket=/www/mysql-mgr/mysql-slave1/mysql.sock

配置slave2节点

复制代码
root@mzi:/www# vim /www/mysql-mgr/mysql-slave2/conf/slave2.cnf

[mysqld]
datadir = /www/mysql-mgr/mysql-slave2/data
socket = /www/mysql-mgr/mysql-slave2/mysql.sock
log-error = /www/mysql-mgr/mysql-slave2/logs/error.log
pid-file = /www/mysql-mgr/mysql-slave2/mysql.pid
tmpdir = /www/mysql-mgr/mysql-slave2/tmp
port = 3308

# ========================
# 内存优化配置 (48核64G专属)
# ========================
# 缓冲池 master占50%,剩余从占25%, 总数占用不超过48G
innodb_buffer_pool_size  = 12G
innodb_buffer_pool_instances = 12  # 匹配CPU核心数

# 连接与线程优化
max_connections          = 1000     # 避免内存耗尽
thread_cache_size        = 16
table_open_cache         = 2048
table_definition_cache   = 512

# InnoDB引擎优化
innodb_log_file_size     = 4G
innodb_log_buffer_size   = 2G
innodb_flush_log_at_trx_commit = 1
innodb_flush_method      = O_DIRECT
innodb_file_per_table    = 1
innodb_thread_concurrency= 0      # 自动优化

# 临时表与排序优化
tmp_table_size           = 128M
max_heap_table_size      = 128M
sort_buffer_size         = 128M
join_buffer_size         = 128M
read_buffer_size         = 128M
read_rnd_buffer_size     = 128M

# ========================
# 安全与维护配置
# ========================
skip_name_resolve        = ON
log_slow_extra           = ON
long_query_time          = 2
log_queries_not_using_indexes = OFF
max_allowed_packet       = 128M
transaction_isolation    = READ-COMMITTED

# ========================
# MGR 集群核心配置
# ========================
server_id = 2
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_checksum = NONE
log_slave_updates = ON
binlog_format = ROW
transaction_write_set_extraction = XXHASH64

# MGR专用配置
loose-group_replication_group_name = "161ec121-861b-4f61-b6ed-c969087cc1be" 
loose-group_replication_start_on_boot = OFF
loose-group_replication_recovery_get_public_key = ON
loose-group_replication_recovery_use_ssl = ON
loose-group_replication_local_address = "127.0.0.1:33063" 
loose-group_replication_group_seeds = "127.0.0.1:33061,127.0.0.1:33062,127.0.0.1:33063"
loose-group_replication_bootstrap_group = OFF

[mysql]
socket=/www/mysql-mgr/mysql-slave2/mysql.sock

[client]
socket=/www/mysql-mgr/mysql-slave2/mysql.sock

分别启动三个节点

复制代码
root@mzi:/www# sudo -u mysql  mysqld --defaults-file=/www/mysql-mgr/mysql-master/conf/master.cnf &
[1] 81314

root@mzi:/www# sudo -u mysql  mysqld --defaults-file=/www/mysql-mgr/mysql-slave1/conf/slave1.cnf &
[2] 81918

root@mzi:/www# sudo -u mysql  mysqld --defaults-file=/www/mysql-mgr/mysql-slave2/conf/slave2.cnf &
[3] 82116


#验证
root@mzi:~# ps -ef | grep mysql
root       81314       1  0 11:29 ?        00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-master/conf/master.cnf
root       81325   81314  0 11:29 pts/1    00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-master/conf/master.cnf
mysql      81326   81325  5 11:29 pts/1    00:00:08 mysqld --defaults-file=/www/mysql-mgr/mysql-master/conf/master.cnf
root       81918       1  0 11:30 ?        00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-slave1/conf/slave1.cnf
root       81929   81918  0 11:30 pts/4    00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-slave1/conf/slave1.cnf
mysql      81930   81929  6 11:30 pts/4    00:00:04 mysqld --defaults-file=/www/mysql-mgr/mysql-slave1/conf/slave1.cnf
root       82116       1  0 11:31 ?        00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-slave2/conf/slave2.cnf
root       82127   82116  0 11:31 pts/5    00:00:00 sudo -u mysql mysqld --defaults-file=/www/mysql-mgr/mysql-slave2/conf/slave2.cnf
mysql      82128   82127 21 11:31 pts/5    00:00:04 mysqld --defaults-file=/www/mysql-mgr/mysql-slave2/conf/slave2.cnf
root       82637   82371  0 11:31 pts/6    00:00:00 grep --color=auto mysql

配置三个节点的账号密码

配置master节点

复制代码
root@mzi:~# mysql -u root --socket=/www/mysql-mgr/mysql-master/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 65
Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> CREATE USER 'rpl_user'@'%' IDENTIFIED BY '@Password1122!';
Query OK, 0 rows affected (0.14 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'rpl_user'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> create user 'warton'@'%' identified by '@Password1122!';
Query OK, 0 rows affected (0.12 sec)

mysql> grant all privileges on *.* to 'warton'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.22 sec)

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.04 sec)

配置slave1

复制代码
root@mzi:~# mysql -uroot --socket=/www/mysql-mgr/mysql-slave1/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> CREATE USER 'rpl_user'@'%' IDENTIFIED BY '@Password1122!';
Query OK, 0 rows affected (0.06 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'rpl_user'@'%';
Query OK, 0 rows affected (0.04 sec)

mysql> create user 'warton'@'%' identified by '@Password1122!';
Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on *.* to 'warton'@'%';
Query OK, 0 rows affected (0.05 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.10 sec)

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.12 sec)

配置slave2

复制代码
root@mzi:~# mysql -uroot --socket=/www/mysql-mgr/mysql-slave2/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> CREATE USER 'rpl_user'@'%' IDENTIFIED BY '@Password1122!';
Query OK, 0 rows affected (0.06 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'rpl_user'@'%';
Query OK, 0 rows affected (0.04 sec)

mysql> create user 'warton'@'%' identified by '@Password1122!';
Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on *.* to 'warton'@'%';
Query OK, 0 rows affected (0.05 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> RESET MASTER;
Query OK, 0 rows affected (0.10 sec)

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.12 sec)

开始组内复制

master节点执行

复制代码
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (1.69 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)

slave节点执行

复制代码
mysql> CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="@Password1122!" FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 5 warnings (0.45 sec)

mysql> start group_replication;
Query OK, 0 rows affected (2.16 sec)

验证

复制代码
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 93c11147-fa62-11f0-ad6e-e8611f1437f2 | mzi         |        3306 | ONLINE       | PRIMARY     | 8.0.36         | XCom                       |
| group_replication_applier | a6631346-fa62-11f0-b426-e8611f1437f2 | mzi         |        3307 | ONLINE       | SECONDARY   | 8.0.36         | XCom                       |
| group_replication_applier | ba0cec62-fa62-11f0-87ee-e8611f1437f2 | mzi         |        3308 | ONLINE       | SECONDARY   | 8.0.36         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.01 sec)
相关推荐
0xDevNull4 小时前
MySQL数据冷热分离详解
后端·mysql
科技小花4 小时前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸4 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain4 小时前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希5 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神5 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员5 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java5 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿6 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴6 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存