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

MySQL数据库主从复制

目录

基础环境设置

网络对时

防火墙与SELinux

配置主从复制

配置master配置文件

从服务器配置


1、master开启二进制日志记录

2、slave开启IO进程,从master中读取二进制日志并写入slave的中继日志

3、slave开启SQL进程,从中继日志中读取二进制日志并进行重放

4、最终,达到slave与master中数据一致的状态,我们称作为主从复制的过程。

基础环境设置

网络对时

主与从主机都需要操作

[root@localhost ~]# cat /etc/chrony.conf | grep -Ev '^$|#'server ntp.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
leapsectz right/UTC
logdir /var/log/chrony 
[root@slave1 ~]# timedatectl set-timezone Asia/Shanghai
[root@slave1 ~]# systemctl restart chronyd.service 

防火墙与SELinux

[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# sed -i 's/SELIUNX=enforcing/SELINUX=disable/' /etc/selinux/config
[root@localhost ~]# setenforce 0

配置主从复制

配置master配置文件

[root@localhost ~]# cd /etc/my.cnf.d/
[root@localhost my.cnf.d]# ls
mysql-server.cnf
[root@localhost my.cnf.d]# vi 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
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
log-bin=mysql-bin
binlog_format="statement"
server-id=11
log-slave-updates=true

启动服务

[root@localhost my.cnf.d]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

创建一个可以在从主机访问的账户并查看正在使用的日志文件及日志书写位置

[root@localhost my.cnf.d]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.42 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> create user slave@'192.168.44.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)mysql> grant all on *.* to 'slave'@'192.168.44.%';
Query OK, 0 rows affected (0.00 sec)mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      713 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

注意:查看位置完毕后,不要对master做insert、update、delete、create、drop等操作!!!

从服务器配置

[root@localhost ~]# vi /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
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
log-bin=mysql-bin
binlog_format="statement"
relay-log-index=slave-bin.index
server-id=22

此时没有与主服务器进行连接,所以没有产生对应的relay log

systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# lsauto.cnf             ibdata1            mysql.ibd            public_key.pemca-key.pem           ibtmp1             mysql.sock           server-cert.pemca.pem              '#innodb_redo'      mysql.sock.lock      server-key.pemclient-cert.pem     '#innodb_temp'      mysql_upgrade_info   sysclient-key.pem       mysql              mysqlx.sock          undo_001
'#ib_16384_0.dblwr'   mysql-bin.000001   mysqlx.sock.lock     undo_002
'#ib_16384_1.dblwr'   mysql-bin.000002   performance_schemaib_buffer_pool       mysql-bin.index    private_key.pem

配置从服务器的所属主服务器

[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.42 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>  change master to master_host='192.168.44.163',master_user='slave',master_pas
sw
Query OK, 0 rows affected, 8 warnings (0.01 sec)mysql> start slave-> ;
Query OK, 0 rows affected, 1 warning (0.03 sec)mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Connecting to sourceMaster_Host: 192.168.44.163Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 713Relay_Log_File: localhost-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: yesSlave_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: 713Relay_Log_Space: 157Until_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: NULL
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 2061Last_IO_Error: Error connecting to source 'slave@192.168.44.163:3306'. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_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: 250717 09:32:17Last_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)

查看配置是否生效

[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# lsauto.cnf             ibtmp1                       mysqlx.sock.lockbinlog.000001       '#innodb_redo'                performance_schemabinlog.index        '#innodb_temp'                private_key.pemca-key.pem           localhost-relay-bin.000001   public_key.pemca.pem               localhost-relay-bin.000002   server-cert.pemclient-cert.pem      mysql                        server-key.pemclient-key.pem       mysql.ibd                    slave-bin.index
'#ib_16384_0.dblwr'   mysql.sock                   sys
'#ib_16384_1.dblwr'   mysql.sock.lock              undo_001ib_buffer_pool       mysql_upgrade_info           undo_002ibdata1              mysqlx.sock

主服务器建立一个数据库能同步到从主机

mysql> create database test;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

http://www.dtcms.com/a/283756.html

相关文章:

  • 如何将 ONLYOFFICE 文档集成到使用 Laravel 框架编写的 PHP 网络应用程序中
  • 7.事务操作
  • 第2章通用的高并发架构设计——2.6 高并发写场景方案1:数据分片之数据库分库分表
  • win10 安装mysql启动
  • 配置mysql
  • ONLYOFFICE Docs 9.0 重磅上线:全面升级界面体验,AI 驱动高效办公
  • Java全栈工程师面试实录:从电商支付到AI大模型架构的深度技术挑战
  • 下载了docker但是VirtualBox突然启动不了了
  • [IRF/Stack]华为/新华三交换机堆叠配置
  • 安装wsl-Ubuntu到D盘
  • 虚拟化测试工具Parasoft Virtualize如何为汽车企业提供仿真测试?
  • php主流框架FastAdmin框架详解以及如何查看版本号和初始安装fastadmin框架-优雅草卓伊凡|大东家
  • Java并发编程第三篇(深入解析Synchronized)
  • Python reduce函数和lambda表达式完全指南 | 函数式编程教程
  • Day04_C语言网络编程20250716_sql语言大全
  • API 接口开发与接入实践:自动化采集淘宝商品数据
  • 基于单片机公交车报站系统/报站器
  • 国产化PDF处理控件Spire.PDF教程:使用 Python 向 PDF 添加文字(支持创建与编辑)
  • 腾讯位置商业授权鸿蒙地图SDK工程配置
  • 网络爬虫的详细知识点
  • 【JVM】深入理解 JVM 类加载器
  • 语雀编辑器内双击回车插入当前时间js脚本
  • Webpack5 新特性与详细配置指南
  • 爬虫小知识
  • 机器学习:数据清洗与预处理 | Python
  • 【后端】.NET Core API框架搭建(9) --配置使用Log4Net日志
  • 结合自身,制定一套明确的 Web3 学习路线和技术栈建议
  • Elasticsearch MCP 服务器现已在 AWS Marketplace 上提供
  • 概念设计总监的“VR”雕刻术:用Substance 3D Modeler,实现直觉式3D建模
  • HOOPS SDK赋能PLM:打造全生命周期3D数据管理与协作能力