当前位置: 首页 > news >正文

基于openEuler系统部署MySQL数据库主从

案例分析基于openEuler系统部署MySQL数据库主从

1. 规划节点

节点规划,见表1。

表1 节点规划

IP 主机名 节点
192.168.100.4 master mysql
192.168.100.5 slave mysql

2. MySQL数据库主从简介

MySQL 主从复制(Master-Slave Replication)是 MySQL 官方提供的异步数据同步机制,核心是将主数据库(Master)的数据变更操作,实时同步到一个或多个从数据库(Slave),从而实现数据多副本存储、读写分离、故障容灾等核心能力,是 MySQL 高可用、高性能架构的基础组件。

3. MySQL主从架构图

图片

4. MySQL主从工作原理

① 主库执行 INSERT/UPDATE/DELETE 等操作并记录到 Binary Log

② 主库将 Binary Log 中的事件传递给 Binlog Dump 线程进行处理

③ 从库 I/O 线程向主库 Binlog Dump 线程请求二进制日志

④ 主库 Binlog Dump 线程将二进制日志发送给从库 I/O 线程

⑤ 从库 I/O 线程接收到二进制日志后解析事件

⑥ 从库 I/O 线程将解析后的事件写入 Relay Log

⑦ 从库 SQL 线程发送确认信息到主库

⑧ 主库确认日志传输完成

⑨ 从库 SQL 线程读取 Relay Log 并在从库上执行,实现数据同步

5. 基础准备

参考链接

修改master主机名

[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bashWelcome to 6.6.0-28.0.0.34.oe2403.x86_64System information as of time:  Sun Dec  7 03:20:56 PM CST 2025System load:  0.08
Memory used:  4.1%
Swap used:  0%
Usage On:   3%
IP address:   192.168.100.4
Users online:   1[root@master ~]# 

修改slave主机名

[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bashWelcome to 6.6.0-28.0.0.34.oe2403.x86_64System information as of time:  Sun Dec  7 03:21:08 PM CST 2025System load:  0.00
Memory used:  4.2%
Swap used:  0%
Usage On:   3%
IP address:   192.168.100.5
Users online:   1[root@slave ~]# 

配置主机映射(两个节点都需要执行)

[root@master ~]# vi /etc/hosts
...
192.168.100.4 master
192.168.100.5 slave

设置防火墙和selinux开机不自启(两个节点都需要执行)

[root@master ~]# systemctl disable firewalld --now && setenforce 0

永久关闭selinux(两个节点都需要执行)

[root@master ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

配置华为云YUM源,openEuler常用repo源链接(两个节点都需要执行)

[root@master ~]# cp -rvf /etc/yum.repos.d/openEuler.repo /tmp/
[root@master ~]# sed -i \-e '/^baseurl/ s#http://repo.openeuler.org#https://mirrors.huaweicloud.com/openeuler#g' \-e '/^gpgkey/ s#http://repo.openeuler.org#https://mirrors.huaweicloud.com/openeuler#g' \/etc/yum.repos.d/openEuler.repo

清理YUM/DNF的所有缓存文件(包括元数据、软件包缓存等)并快速生成YUM缓存(两个节点都需要执行)

[root@master ~]# yum clean all
[root@master ~]# yum makecache

安装工具(两个节点都需要执行)

[root@master ~]# yum install -y vim tar net-tools lrzsz lsof bash-com*
[root@master ~]# source /etc/profile

案例实施

1. 部署 Chrony 时间同步

(1)安装chrony

两个节点都需要执行

[root@master ~]# yum install -y chrony

启动chrony服务(两个节点都需要执行)

[root@master ~]# systemctl enable chronyd --now
(2)配备主节点时间同步服务器
[root@master ~]# vim /etc/chrony.conf
# 注释此行 pool pool.ntp.org iburst 添加以下内容
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
allow 192.168.0.0/16
local stratum 10

重启chrony服务

[root@master ~]# systemctl restart chronyd

手动同步时间

[root@master ~]# chronyc makestep
200 OK

查看 NTP 时间源状态

[root@master ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6   377    55  -4897us[-3796us] +/-   22ms
^+ 106.55.184.199                2   6   377    54  -5868us[-5868us] +/-   50ms
(3)配备从节点时间同步服务器
[root@slave ~]# vim /etc/chrony.conf 
# 注释此行 pool pool.ntp.org iburst 添加以下内容
server master iburst

重启chrony服务

[root@slave ~]# systemctl restart chronyd

手动同步时间

[root@slave ~]# chronyc makestep
200 OK

查看 NTP 时间源状态

[root@slave ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* master                        3   6   377    54  +1736us[+2215us] +/-   23ms
(4)配置 亚洲/上海 时区
[root@master ~]# timedatectl set-timezone Asia/Shanghai

验证时区

[root@master ~]# timedatectl Local time: Sun 2025-12-07 18:18:44 CSTUniversal time: Sun 2025-12-07 10:18:44 UTCRTC time: Sun 2025-12-07 10:18:44Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yesNTP service: activeRTC in local TZ: no

2. 部署 MySQL 环境

(1)安装mysql

两个节点都需要执行

[root@master ~]# yum install -y mysql-server
(2)设置mysql开机自启

两个节点都需要执行

[root@master ~]# systemctl enable mysqld --now
(3)配置数据库密码

两个节点都需要执行

[root@master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.44 Source distributionCopyright (c) 2000, 2025, 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> alter user 'root'@'localhost' identified by '000000';
Query OK, 0 rows affected (0.03 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

直接登录失败需要使用密码登录

[root@master ~]# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.44 Source distributionCopyright (c) 2000, 2025, 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>

3. 配置 MySQL 主数据库

(1)配置主配置文件
[root@master ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
# 1. 作用范围:所有 MySQL 客户端工具(如mysql命令行、mysqldump)和服务端(mysqld)都会读取该块下的参数;
# 2. 典型用途:存放客户端+服务端共用的基础参数(如字符集 default-character-set=utf8mb4);
# 3. 优先级:该块参数会被后续专属配置块(如[mysqld]、[mysql])的同名参数覆盖。#
# This group is read by the server
#
[mysqld]
# 1. 作用范围:仅作用于 MySQL 服务进程(mysqld),客户端工具不会读取该块参数;
# 2. 典型用途:存放服务端核心参数(如端口、数据目录、主从复制、性能优化等);
# 3. 注意:该块在本文件中未定义具体参数,仅作为「占位标识」,实际参数在引入的子配置文件中。# ===================== 主从复制核心配置 =====================
# 注释:主库必须配置,从库若为「级联复制」(从库再作为其他库的主库)也需配置# 服务器唯一标识ID(主从集群必唯一)
server-id = 4
# 详细说明:
# 1. 取值范围1~4294967295,主库和所有从库的server-id绝对不能重复,否则复制中断
# 2. 建议用服务器IP最后一位(如本示例IP最后一位为128),便于运维快速识别实例
# 3. 修改后需重启MySQL生效# 启用二进制日志(主从复制的数据源)
log_bin = mysql-bin
# 详细说明:
# 1. 启用Binlog日志,文件命名格式为mysql-bin.000001、mysql-bin.000002(自动递增)
# 2. Binlog记录所有修改数据的操作(INSERT/UPDATE/DELETE),是主库向从库同步的核心
# 3. 从库若仅作为「只读副本」可关闭,若需级联复制则必须启用
# 4. 修改后需重启MySQL生效# 二进制日志格式说明(三种模式对比)
# STATEMENT: 记录执行的SQL语句(日志体积小,但非确定性语句(如NOW()/UUID())会导致同步不一致)
# ROW: 记录每行数据的修改前后状态(体积大,但数据一致性100%,生产环境首选)
# MIXED: 自动混合模式(确定性语句用STATEMENT,非确定性用ROW),稳定性不如ROW
binlog_format = ROW
# 详细说明:
# 1. 强制指定ROW模式,规避STATEMENT模式的同步不一致问题(如电商订单、金融交易场景)
# 2. 虽日志体积较大,但可通过expire_logs_days自动清理,是生产环境的标准配置# Binlog日志自动过期清理时间(避免磁盘占满)
expire_logs_days = 7
# 详细说明:
# 1. 超过7天的Binlog文件会被自动删除,需匹配业务备份周期(如备份周期7天则设为7)
# 2. 若主从复制延迟超过7天,会导致从库因缺失Binlog同步失败,需确保复制延迟<过期时间
# 3. 手动清理可执行 PURGE BINARY LOGS BEFORE '2025-12-01 00:00:00';# 事务提交后立即刷Binlog到磁盘(数据安全最高级别)
sync_binlog = 1
# 详细说明:
# 1. 取值0:由操作系统决定刷盘时机(性能高,但系统崩溃会丢失未刷盘的Binlog)
# 2. 取值1:每次事务提交强制刷盘(性能略降,但保障Binlog不丢失,主库必设)
# 3. 生产环境主库建议强制设为1,从库可设为0/100(兼顾性能)# 忽略同步的系统数据库(避免从库权限/元数据混乱)
binlog-ignore-db = mysql
# 详细说明:mysql库存储用户、权限、密码等核心信息,主从同步会导致从库权限混乱,需忽略
binlog-ignore-db = information_schema
# 详细说明:内存虚拟库,存储数据库元数据(表结构/列信息),无持久化数据,无需同步
binlog-ignore-db = performance_schema
# 详细说明:性能监控库,存储运行时性能指标(如慢查询、连接数),仅用于监控,无需同步
binlog-ignore-db = sys
# 详细说明:基于performance_schema封装的监控视图库,无业务数据,无需同步# ===================== openEuler 性能优化配置 =====================
# 注释:适配openEuler多核、高内存的系统特性,针对性优化InnoDB性能# InnoDB缓冲池大小(MySQL性能最核心参数)
# 配置依据:物理内存4GB,预留1.5GB给操作系统/其他进程,故设为2560M(2.5GB)
# 通用建议:物理内存的60%-70%(如8GB内存设为5GB,16GB设为10GB)
innodb_buffer_pool_size = 2560M
# 详细说明:
# 1. 缓存InnoDB表的数据页、索引页、插入缓存,减少磁盘IO(内存读写比磁盘快1000倍+)
# 2. openEuler对大内存管理更友好,该参数可充分利用物理内存
# 3. 修改后需重启MySQL生效,建议结合业务压测调整# InnoDB缓冲池实例数(提升多核并发性能)
innodb_buffer_pool_instances = 4
# 详细说明:
# 1. 将缓冲池拆分为4个独立实例,每个实例有独立锁机制,减少多核CPU的锁竞争
# 2. 建议:缓冲池≥1GB时启用,实例数不超过CPU核心数(如8核设为8)
# 3. openEuler多核优化特性可最大化该参数的性能收益# 最大并发连接数(适配高并发业务)
max_connections = 500
# 详细说明:
# 1. 默认值151,openEuler系统资源限制更宽松,可提升至500(需结合内存调整)
# 2. 每个连接约占用100KB~1MB内存,500连接约占用500MB内存,需确保内存充足
# 3. 建议搭配 max_user_connections = 100(限制单用户连接数),防止恶意占满连接# 服务器默认字符集(适配全字符场景)
character-set-server = utf8mb4
# 详细说明:
# 1. utf8mb4是utf8的超集,支持Emoji表情、特殊符号(utf8仅支持3字节Unicode)
# 2. openEuler默认字符集为utf8,显式指定utf8mb4避免中文/Emoji乱码
# 3. 需确保客户端连接时也使用utf8mb4(如jdbc连接串加 characterEncoding=utf8mb4)# 字符集排序规则(兼容多语言排序)
collation-server = utf8mb4_unicode_ci
# 详细说明:
# 1. 基于Unicode标准排序,不区分大小写(ci=case insensitive),适配中文/英文排序
# 2. 性能优于 utf8mb4_general_ci,是utf8mb4字符集的推荐排序规则
# 3. 若需区分大小写,可改为 utf8mb4_bin#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

重启mysql服务

[root@master ~]# systemctl restart mysqld

验证服务状态

[root@master ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database serverLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)Active: active (running) since Sun 2025-12-07 15:56:14 CST; 1min 33s agoProcess: 6352 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)Process: 6380 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCC>Main PID: 6416 (mysqld)Status: "Server is operational"Tasks: 40 (limit: 47123)Memory: 572.4M ()CGroup: /system.slice/mysqld.service└─6416 /usr/libexec/mysqld --basedir=/usrDec 07 15:56:12 master systemd[1]: Starting MySQL 8.0 database server...
Dec 07 15:56:14 master systemd[1]: Started MySQL 8.0 database server.

查看 binlog 日志是否启用

[root@master ~]# mysql -uroot -p000000 -e "show variables like 'log_bin';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
(2)查看子配置文件
[root@master ~]# vim /etc/my.cnf.d/mysql-server.cnf
#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd[mysqld]
datadir=/var/lib/mysql
# 数据目录:MySQL 所有核心数据的存储根路径
socket=/var/lib/mysql/mysql.sock
# 本地套接字文件路径:本地客户端与 mysqld 通信的专用文件(替代 TCP/IP)
log-error=/var/log/mysql/mysqld.log
# 错误日志路径:mysqld 运行过程中所有错误/警告/启动停止日志的存储文件
pid-file=/run/mysqld/mysqld.pid
# PID 文件路径:存储 mysqld 进程 ID 的文件
(3)创建从数据库复制用户
mysql> create user 'repl'@'slave' identified with mysql_native_password by 'Abc@1234';
Query OK, 0 rows affected (0.01 sec)mysql> grant replication slave on *.* to 'repl'@'slave';
Query OK, 0 rows affected (0.01 sec)mysql> select user,host,authentication_string,plugin from mysql.user where user='repl';
+------+-------+-------------------------------------------+-----------------------+
| user | host  | authentication_string                     | plugin                |
+------+-------+-------------------------------------------+-----------------------+
| repl | slave | *3930876FA8B772FDE4FB8ED345D32585735D09F8 | mysql_native_password |
+------+-------+-------------------------------------------+-----------------------+
1 row in set (0.00 sec)

查看主数据库状态获取 Binlog 文件名和当前 Binlog 文件的写入偏移量(字节数)

mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 |      864 |              | mysql,information_schema,performance_schema,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)

4. 配置 MySQL 主数据库

(1)配置主配置文件
[root@slave ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]#
# This group is read by the server
#
[mysqld]
# ===================== 主从复制核心配置 =====================
# 注释:从库必须配置,核心保障复制链路稳定、数据不被误写、故障可恢复# 服务器唯一标识ID(必须与主库不同,主从集群必唯一)
server-id = 5
# 详细说明:
# 1. 取值范围1~4294967295,必须与主库(如4)严格区分,重复会直接导致复制中断(8.0报错:ERROR 1236 - server id conflict)
# 2. 建议用服务器IP最后一位(如本示例IP最后一位为129),便于运维快速识别实例
# 3. 修改后需重启MySQL生效(8.0不支持动态修改server-id)# 启用中继日志(从库复制的核心中间文件)
relay-log = mysql-relay-bin
# 详细说明:
# 1. 启用中继日志,文件命名格式为mysql-relay-bin.000001、mysql-relay-bin.000002(自动递增)
# 2. 中继日志存储从库I/O线程从主库获取的Binlog事件,由SQL线程读取重放,是从库复制的核心载体
# 3. 从库仅作为「只读副本」时必须启用,若为级联复制(从库再做主库)需同时启用log_bin
# 4. 修改后需重启MySQL生效# 从库只读模式(防止误写入数据,保障主从数据一致性)
read_only = 1
# 详细说明:
# 1. 取值1:开启只读模式,限制普通用户执行INSERT/UPDATE/DELETE操作;取值0:关闭只读(默认)
# 2. 注意事项:root等拥有SUPER权限的用户不受此限制,8.0建议搭配super_read_only=1强化限制
# 3. 生产环境从库必须设为1,避免开发/运维直接写入从库导致数据不一致
# 4. 8.0支持动态修改(SET GLOBAL read_only=1),配置文件中设为1是永久生效# 跳过主从复制中的特定错误(生产环境慎用!仅临时应急)
# slave-skip-errors = 1062,1032
# 详细说明:
# 1. 错误码含义:
#    - 1062:主键冲突(Duplicate entry),主从数据不一致时易触发;
#    - 1032:记录不存在(Can't find record in table),通常是从库数据缺失导致;
# 2. 启用后从库会跳过指定错误继续复制,但会导致主从数据永久不一致,仅适用于临时应急
# 3. 生产规范:禁止长期启用,出现复制错误需先定位根因,再通过pt-table-checksum/pt-table-sync修复# ===================== openEuler 性能优化配置 =====================
# 注释:适配openEuler多核、高内存的系统特性,针对性优化InnoDB性能,与主库保持一致即可# InnoDB缓冲池大小(MySQL性能最核心参数)
# 配置依据:物理内存4GB,预留1.5GB给操作系统/其他进程,故设为2560M(2.5GB)
# 通用建议:物理内存的60%-70%(如8GB内存设为5GB,16GB设为10GB)
innodb_buffer_pool_size = 2560M
# 详细说明:
# 1. 缓存InnoDB表的数据页、索引页、插入缓存,减少磁盘IO(内存读写比磁盘快1000倍+)
# 2. openEuler对大内存管理更友好,该参数可充分利用物理内存,从库重放事务时缓存至关重要
# 3. 8.0支持动态调整(SET GLOBAL innodb_buffer_pool_size=2684354560),配置文件设为2560M需重启生效
# 4. 建议结合业务压测调整,缓冲池命中率需≥99%# InnoDB缓冲池实例数(提升多核并发性能)
innodb_buffer_pool_instances = 4
# 详细说明:
# 1. 将缓冲池拆分为4个独立实例,每个实例有独立锁机制,减少多核CPU的锁竞争
# 2. 建议:缓冲池≥1GB时启用,实例数不超过CPU核心数(如8核设为8)
# 3. openEuler多核优化特性可最大化该参数的性能收益,从库并行复制场景下效果更明显# 最大并发连接数(适配高并发业务场景)
max_connections = 500
# 详细说明:
# 1. 默认值151,openEuler系统资源限制更宽松,可提升至500(需结合内存调整)
# 2. 每个连接约占用100KB~1MB内存,500连接约占用500MB内存,需确保内存充足
# 3. 从库注意:需预留连接数给复制线程(I/O线程+SQL线程+并行复制线程),建议略高于主库
# 4. 建议搭配 max_user_connections = 100(限制单用户连接数),防止恶意占满连接# 服务器默认字符集(适配全字符场景)
character-set-server = utf8mb4
# 详细说明:
# 1. utf8mb4是utf8的超集,支持Emoji表情、特殊符号(utf8仅支持3字节Unicode)
# 2. openEuler默认字符集为utf8,显式指定utf8mb4避免中文/Emoji乱码
# 3. 必须与主库字符集一致,否则同步含特殊字符的数据时会出现乱码/复制中断
# 4. 需确保客户端连接时也使用utf8mb4(如jdbc连接串加 characterEncoding=utf8mb4)# 字符集排序规则(兼容多语言排序)
collation-server = utf8mb4_unicode_ci
# 详细说明:
# 1. 基于Unicode标准排序,不区分大小写(ci=case insensitive),适配中文/英文排序
# 2. 性能优于 utf8mb4_general_ci,是utf8mb4字符集的推荐排序规则
# 3. 必须与主库排序规则一致,否则从库执行ORDER BY/WHERE查询时结果可能与主库不同
# 4. 若需区分大小写,可改为 utf8mb4_bin# ===================== 并行复制优化(MySQL 8.0.44 特性) =====================
# 注释:8.0.44并行复制比5.7更高效,核心降低主从复制延迟# 启用多线程复制,指定并行复制模式(8.0推荐LOGICAL_CLOCK)
slave_parallel_type = LOGICAL_CLOCK
# 详细说明:
# 1. 模式说明:
#    - LOGICAL_CLOCK:基于主库事务的提交顺序(逻辑时钟),无冲突事务并行重放(8.0优化版,效率提升30%);
#    - DATABASE:按数据库维度并行(单库事务串行,8.0已标记为过时);
# 2. 核心优势:突破传统单线程复制瓶颈,适配主库高并发写入场景
# 3. 生效条件:需配合slave_parallel_workers>0使用,修改后需重启MySQL生效# 并行复制工作线程数(控制并行度,降低主从延迟)
slave_parallel_workers = 4
# 详细说明:
# 1. 配置建议:线程数等于CPU核心数(如4核设为4,8核设为8),过多会导致线程竞争反而降性能
# 2. 核心作用:启动4个SQL工作线程并行重放中继日志,大幅提升从库复制速度
# 3. 8.0特性:工作线程锁竞争比5.7减少50%,openEuler多核调度下性能收益更明显
# 4. 监控:可通过show processlist查看「Slave SQL Worker x」线程状态判断并行度是否合理# 从库中继日志自动恢复(故障自愈,生产环境推荐启用)
relay_log_recovery = 1
# 详细说明:
# 1. 取值1:从库重启时,自动删除损坏/未处理的中继日志,重新从主库获取最新Binlog重建
# 2. 适用场景:从库意外宕机、中继日志损坏导致复制中断时,无需手动清理,自动恢复
# 3. 取值0:关闭(默认),生产环境从库必须设为1,保障故障后复制可自动恢复
# 4. 修改后需重启MySQL生效(8.0不支持动态修改)#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

重启mysql服务

[root@slave ~]# systemctl restart mysqld

验证服务状态

[root@slave ~]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database serverLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)Active: active (running) since Sun 2025-12-07 18:05:23 CST; 22s agoProcess: 3758 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)Process: 3784 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCC>Main PID: 3820 (mysqld)Status: "Server is operational"Tasks: 47 (limit: 47123)Memory: 615.8M ()CGroup: /system.slice/mysqld.service└─3820 /usr/libexec/mysqld --basedir=/usrDec 07 18:05:22 slave systemd[1]: Starting MySQL 8.0 database server...
Dec 07 18:05:23 slave systemd[1]: Started MySQL 8.0 database server.

查看服务器唯一 ID和 read_only 从库只读模式

[root@slave ~]# mysql -uroot -p'000000' -e "show variables like 'server_id';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 5     |
+---------------+-------+
[root@slave ~]# mysql -uroot -p'000000' -e "show variables like 'read_only';"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
(2)配置主从复制
mysql> CHANGE MASTER TO->   MASTER_HOST='master',->   MASTER_USER='repl',->   MASTER_PASSWORD='Abc@1234',->   MASTER_PORT=3306,->   MASTER_LOG_FILE='mysql-bin.000001',->   MASTER_LOG_POS=864;
Query OK, 0 rows affected, 9 warnings (0.02 sec)

启动复制线程

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

验证从库复制状态

mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: masterMaster_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 3083087Relay_Log_File: mysql-relay-bin.000002Relay_Log_Pos: 326Relay_Master_Log_File: mysql-bin.000001# IO 线程(从主库拉取 Binlog)正常运行Slave_IO_Running: Yes# SQL 线程(重放中继日志)正常运行Slave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 3083087Relay_Log_Space: 536Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 4Master_UUID: fd5fe262-d33d-11f0-992a-000c299876c9Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

5. 测试 MySQL 主从复制

(1)主节点创建数据库
mysql> create database cloud;
Query OK, 1 row affected (0.01 sec)

切换数据库

mysql> use cloud;
Database changed

创建表

mysql> create table user (->   id int auto_increment primary key,->   username varchar(50) not null-> ) ;
Query OK, 0 rows affected (0.14 sec)

插入表数据

mysql> insert into user (username) values ('gxl');
Query OK, 1 row affected (0.01 sec)

查询表数据

mysql> select * from user;
+----+----------+
| id | username |
+----+----------+
|  1 | gxl      |
+----+----------+
1 row in set (0.00 sec)
(2)从节点验证创建
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| cloud              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

切换数据库

mysql> use cloud;
Database changed

查看表结构

mysql> show create table user\G
*************************** 1. row ***************************Table: user
Create Table: CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT,`username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

查询表数据

mysql> select * from user;
+----+----------+
| id | username |
+----+----------+
|  1 | gxl      |
+----+----------+
1 row in set (0.00 sec)

6. 优化初始架构

(1)数据库备份
[root@wordpress ~]# mysqldump -uroot -p000000 wordpress > wordpress.sql

远程复制到mysql集群主节点

[root@wordpress ~]# scp wordpress.sql 192.168.100.4:/root
(2)创建用户和数据库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)mysql> create user 'wordpress'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.02 sec)mysql> create user 'wordpress'@'192.168.100.%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)mysql> grant all privileges on wordpress.* to 'wordpress'@'localhost';
Query OK, 0 rows affected (0.01 sec)mysql> grant all privileges on wordpress.* to 'wordpress'@'192.168.100.%';
Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> show grants for 'wordpress'@'192.168.100.%';
+----------------------------------------------------------------------+
| Grants for wordpress@192.168.100.%                                   |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `wordpress`@`192.168.100.%`                    |
| GRANT ALL PRIVILEGES ON `wordpress`.* TO `wordpress`@`192.168.100.%` |
+----------------------------------------------------------------------+
2 rows in set (0.01 sec)

wordpress数据库导入集群

[root@master ~]# mysql -uroot -p000000 wordpress < wordpress.sql

更改wordpress正式配置文件主机为mysql集群主节点IP地址

[root@wordpress ~]# vim /usr/share/nginx/html/wp-config.php 
...
/** Database hostname *
define( 'DB_HOST', '192.168.100.4' );
...
http://www.jsqmd.com/news/701200/

相关文章:

  • 【VSCode 2026工业协议解析插件终极指南】:覆盖Modbus/TCP、OPC UA、CANopen等12类协议,实测解析速度提升370%
  • 微软FinnTS:基于AutoML与LLM Agent的自动化时间序列预测框架
  • Java应用运行时安全防护:基于RASP技术的无侵入探针实战
  • VSCode AI配置速度慢?实测数据:正确配置后首响应≤832ms,错误配置平均延迟4.7s——附性能压测报告
  • 反射驱动的元编程范式跃迁,深度对比C++20/23/26三版本实现差异与面试必答逻辑链
  • 机器学习数据准备框架:从原理到工程实践
  • SuperDesign:在IDE中用AI自然语言生成UI设计与代码
  • 多智能体LLM推理实战:从思维链到自适应思维图
  • Empire渗透测试框架:无文件攻击与C2通信的经典实现与防御启示
  • 分布式任务编排系统OpenClaw:从核心架构到生产实践的深度解析
  • 3步搞定B站字幕下载转换:从零开始获取离线字幕资源
  • 2026年评价高的塑粉稳定供货厂家推荐 - 行业平台推荐
  • Unity UI框架实战:巧用快捷键与层级管理,解决弹窗叠加和界面切换的坑
  • Marksman:深度集成开发工作流的智能文档生成与管理工具实践
  • 如何快速上手KKManager:Illusion游戏模组管理的终极解决方案
  • 【AI Agent实战】8000字源码分析,AI帮我2小时吃透——学技术文章的新姿势
  • 机器学习项目协作平台选型与实战指南
  • ARM CP15协处理器架构与缓存控制技术详解
  • ELK+Kafka+Zookeeper日志收集系统
  • 2026气动设备回收标杆名录:风冷系统回收、食品车间回收、食品车间拆除、CNC铣床回收、PLC伺服设备回收、SMC气动设备回收选择指南 - 优质品牌商家
  • 基于DeepChat框架构建AI对话应用:从原理到实践
  • 一种通用的前端复刻思路:提取 UI 结构数据,交给 AI 生成代码
  • 深度学习目标识别:从分类到检测的完整指南
  • csp信奥赛C++高频考点专项训练之贪心算法 --【删数问题】:删数问题2
  • 2026年上海拼多多客服外包选哪家:上海视频号客服外包、专席客服外包、临时客服外包、全包客服外包、售前客服外包选择指南 - 优质品牌商家
  • RAG 实战:给 AI 接上私有知识库的完整方案
  • 大模型API缓存的底层原理:从显存到网关
  • Python机器学习数据预处理实战与Scikit-Learn技巧
  • Claude AI代码编辑器插件:架构解析与四大核心开发场景实战
  • 当Parquet文件不再神秘:浏览器里就能轻松查看的数据探索工具