mysql shell8.4.10+mysql 8.4.10搭建MySQL replicaset主从

mysql shell8.4.10+mysql 8.4.10搭建MySQL replicaset主从

mysql replicaset 是传统主从模式的增强版本,方便搭建和管理。

安装包:

-rw-r--r-- 1 root root 828832968 Aug 29 16:05 mysql-8.4.10-linux-glibc2.17-x86_64.tar.xz

-rw-r--r-- 1 root root 97256568 Aug 29 16:02 mysql-shell-8.4.10-1.el7.x86_64.rpm

安装数据库忽略

创建集群管理账号

复制代码
--创建集群管理账号
CREATE USER 'rsadmin'@'%' IDENTIFIED BY 'Huawei12#$';# 创建允许任意IP访问的root用户
GRANT ALL PRIVILEGES ON *.* TO 'rsadmin'@'%' WITH GRANT OPTION;# 授予全部权限
FLUSH PRIVILEGES;# 刷新权限
SELECT user,host FROM mysql.user;
exit;

##登录mysqlshell

复制代码
--登录mysql shell 
mysqlsh root@10.10.1.1:6034 --password


Copyright (c) 2016, 2026, 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 '\?' for help; '\quit' to exit.
Creating a session to 'rsadmin@10.10.1.1:6033'
Fetching global names for auto-completion... Press ^C to stop.
Your MySQL connection id is 20
Server version: 8.4.10 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  10.10.194.8:6033 ssl  SQL >


--切换到JS模式

 MySQL  10.10.1.1:6033 ssl  SQL > \js
Switching to JavaScript mode...
 MySQL  10.10.1.1:6033 ssl  JS >

检查是否满足条件(在一个节点操作即可)

复制代码
dba.configureReplicaSetInstance('rsadmin@10.10.1.1:6033', {clusterAdmin: "'rsadmin'@'10.10.1.%'"});

dba.configureReplicaSetInstance('rsadmin@10.10.1.1:6034', {clusterAdmin: "'rsadmin'@'10.10.1.%'"});

 MySQL  10.10.1.1:6033 ssl  JS > dba.configureReplicaSetInstance('rsadmin@10.10.1.1:6034', {clusterAdmin: "'rsadmin'@'10.10.1.%'"});
Please provide the password for 'rsadmin@10.10.1.1:6034': **********
Save password for 'rsadmin@10.10.1.1:6034'? [Y]es/[N]o/Ne[v]er (default No): yes
Configuring local MySQL instance listening at port 6034 for use in an InnoDB ReplicaSet...

This instance reports its own address as oggres1948:6034
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable s                                                                                       hould be changed.

applierWorkerThreads will be set to the default value of 4.

Creating user rsadmin@10.10.1.%.
Account rsadmin@10.10.1.% was successfully created.


The instance 'oggres1948:6034' is valid for InnoDB ReplicaSet usage.

Successfully enabled parallel appliers.

创建集群

复制代码
 MySQL  10.10.1.1:6033 ssl  JS > var rs = dba.createReplicaSet("zhou")
A new replicaset with instance 'oggres1948:6033' will be created.

* Checking MySQL instance at oggres1948:6033

This instance reports its own address as oggres1948:6033
oggres1948:6033: Instance configuration is suitable.

* Checking connectivity and SSL configuration...
* Updating metadata...

ReplicaSet object successfully created for oggres1948:6033.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.

添加安全信仁

复制代码
 MySQL  10.10.1.1:6033 ssl  JS > rs.setOption('replicationAllowedHost', '10.10.1.0/24')
Internally managed replication users updated for ReplicaSet 'zhou'

查看状态

复制代码
 MySQL  10.10.1.1:6033 ssl  JS > rs.status()
{
    "replicaSet": {
        "name": "zhou",
        "primary": "oggres1948:6033",
        "status": "AVAILABLE",
        "statusText": "All instances available.",
        "topology": {
            "oggres1948:6033": {
                "address": "oggres1948:6033",
                "instanceRole": "PRIMARY",
                "mode": "R/W",
                "status": "ONLINE"
            }
        },
        "type": "ASYNC"
    }
}

添加从节点

复制代码
 MySQL  10.10.1.1:6033 ssl  JS > rs.addInstance('10.10.1.1:6034')
Adding instance to the replicaset...

* Performing validation checks

This instance reports its own address as oggres1948:6034
oggres1948:6034: Instance configuration is suitable.

* Checking async replication topology...

* Checking connectivity and SSL configuration...

* Checking transaction state of the instance...

WARNING: A GTID set check of the MySQL instance at 'oggres1948:6034' determined that it contains transactions that do not originate from the replicaset, wh                                                                                       ich must be discarded before it can join the replicaset.

Instance 'oggres1948:6034' has the following errant GTIDs that do not exist in the replicaset:
 322c43d6-a390-11f1-85d1-0cda411d29c9:1-7

WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of 'oggres1948:6034' with a physical snapsho                                                                                       t from an existing replicaset member. To use this method by default, set the 'recoveryMethod' option to 'clone'.

Having extra GTID events is not expected, and it is recommended to investigate this further and ensure that the data can be removed prior to choosing the c                                                                                       lone recovery method.

Please select a recovery method [C]lone/[A]bort (default Abort): C
* Updating topology
Monitoring Clone based state recovery of the new member. Press ^C to abort the operation.
Clone based state recovery is now in progress.

NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.

* Waiting for clone to finish...
NOTE: oggres1948:6034 is being cloned from oggres1948:6033
** Stage DROP DATA: Completed
** Clone Transfer
    FILE COPY  ############################################################  100%  Completed
    PAGE COPY  ############################################################  100%  In Progress
    REDO COPY  ============================================================    0%  Not Started

NOTE: oggres1948:6034 is shutting down...

* Waiting for server restart... ready
* oggres1948:6034 has restarted, waiting for clone to finish...
** Stage RESTART: Completed
* Clone process has finished: 74.72 MB transferred in 2 sec (37.36 MB/s)

** Changing replication source of oggres1948:6034 to oggres1948:6033
** Waiting for new instance to synchronize with PRIMARY...
** Transactions replicated  ############################################################  100%


The instance 'oggres1948:6034' was added to the replicaset and is replicating from oggres1948:6033.

* Waiting for instance 'oggres1948:6034' to synchronize the Metadata updates with the PRIMARY...
** Transactions replicated  ############################################################  100%


 MySQL  10.10.1.1:6033 ssl  JS > rs.status()
{
    "replicaSet": {
        "name": "zhou",
        "primary": "oggres1948:6033",
        "status": "AVAILABLE",
        "statusText": "All instances available.",
        "topology": {
            "oggres1948:6033": {
                "address": "oggres1948:6033",
                "instanceRole": "PRIMARY",
                "mode": "R/W",
                "status": "ONLINE"
            },
            "oggres1948:6034": {
                "address": "oggres1948:6034",
                "instanceRole": "SECONDARY",
                "mode": "R/O",
                "replication": {
                    "applierStatus": "APPLIED_ALL",
                    "applierThreadState": "Waiting for an event from Coordinator",
                    "applierWorkerThreads": 4,
                    "receiverStatus": "ON",
                    "receiverThreadState": "Waiting for source to send event",
                    "replicationLag": "applier_queue_applied",
                    "replicationSsl": "TLS_AES_128_GCM_SHA256 TLSv1.3",
                    "replicationSslMode": "REQUIRED"
                },
                "status": "ONLINE"
            }
        },
        "type": "ASYNC"
    }
}
 MySQL  10.10.1.1:6033 ssl  JS >

附:

启动脚本

复制代码
# cat /usr/lib/systemd/system/mysql6033.service
[Unit]
Description=MySQL 3306  Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql-8.4.10/bin/mysqld_safe --defaults-file=/data/mysql-8.4.10/etc/my.cnf
LimitNOFILE = 65536
LimitNPROC = 65535
# 重启条件
Restart=on-failure
# 退出码为1不重启,即正常shutdown
RestartPreventExitStatus=1
# 重启条件满足后等多久自动重启(秒)
RestartSec=10
# 五分钟内只能重启两次,第三次不重启了
StartLimitInterval=300
# 五分钟内只能重启两次,第三次不重启了
StartLimitBurst=2
# 服务启动的超时时间,单位秒
TimeoutStartSec=30
# 服务关闭的超时时间,单位秒
TimeoutStopSec=30

systemctl daemon-reload
systemctl start mysql6033
systemctl enable mysql6033
systemctl status mysql6033

my.cnf配置文件

复制代码
# cat my.cnf
[mysqld]
basedir=/data/mysql-8.4.10
datadir=/data/mysql-8.4.10/data
socket=/data/mysql-8.4.10/data/mysql.sock
pid-file=/data/mysql-8.4.10/data/mysql.pid
log-error=/data/mysql-8.4.10/data/error-mysql.log
port=6033

binlog_format = ROW
log-bin = /data/mysql-8.4.10/data/mysql-bin.log
#设置binlog清理时间
binlog_expire_logs_seconds=2592000
#binlog每个日志文件大小
max_binlog_size = 200m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size = 512m
#配置serverid

server-id=102
gtid_mode=on
enforce_gtid_consistency=on

注:下期文章安装配置MySQL router 实现读写分离

相关推荐
zcmodeltech40 分钟前
反应装置模型控制系统设计与实现:多设备协同联动方案
java·网络·数据库·stm32·嵌入式硬件·能源·制造
Elastic 中国社区官方博客1 小时前
从建议到修复的 4 个阶段:使用 Elastic Workflows 实现人在回路中的自动化
运维·数据库·人工智能·后端·elasticsearch·ai·自动化
北京靠谱的GEO优化机构1 小时前
海外推广:没有媒体内容,SEO就是“无米之炊“
服务器·数据库·媒体
2601_962071571 小时前
数据库系统架构与DBMS功能探微:现代信息时代数据管理的关键
java·开发语言·数据库
circuitsosk1 小时前
从零搭建行业知识平台:向量库+图数据库+传统关系库的多模检索统一层设计
数据库·python·oracle·向量数据库·rag·多模检索
程序员夏洛1 小时前
Redis 中如何实现分布式锁?
数据库·redis·分布式
2601_962065252 小时前
2.启动mysql容器
java·数据库·mysql
2601_962174173 小时前
Spring 核心技术解析【纯干货版】- XI:Spring 数据访问模块 Spring-Oxm 模块精讲
数据库·python·spring
HanhahnaH3 小时前
垂直分库和水平分库分别解决了什么问题
数据库