mysql主从同步(复制)搭建

一、主服务器操作

1.docker-compose文件

复制代码
version: '3'

services:
  mysql: 
    image:  mariadb:10.5.28
    container_name: mariadb_1
    restart: always
    ports:
      - 13306:3306
    environment:
      MARIADB_ROOT_PASSWORD: 123456     # 设置root用户密码
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MARIADB_DATABASE: test01              # 初始化的数据库名称
    volumes:
      - ./mysql_conf:/etc/mysql
      - ./mysql_data:/var/lib/mysql

2.配置文件编写

复制代码
vim /data/mariadb/mysql_conf/mariadb.conf.d/50-server.cnf

# 启用binlog日志
server-id              = 1
log-bin=mysql1

3.重启服务

4.创建一个主从复制用户并授权

复制代码
#用户授权

[root@mysql53 ~]# mysql
mysql> create user repluser@"%" identified by "123456";
mysql> grant replication slave on *.*  to repluser@"%";

create user dbuser@"%" identified by "dbuser";
grant replication slave on *.*  to dbuser@"%";

5.查看信息

复制代码
MariaDB [(none)]> show master status;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql1.000001 |      645 |              |                  |
+---------------+----------+--------------+------------------+
1 row in set (0.000 sec)


MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000057 |      342 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.000 sec)

二、从服务器操作

1.docker-compose文件

2.配置文件编写

复制代码
vim /data/mariadb/mysql_conf/mariadb.conf.d/50-server.cnf

# 启用binlog日志
server-id              = 2
relay-log=mysql-relay-bin # 中继日志

3.重启服务

4.登录mysql操作

复制代码
[root@mysql54 ~]# mysql
mysql> change master to  
master_host="192.168.88.13", 
master_port=13306,
master_user="repluser",
master_password="123456",
master_log_file="mysql1.000001", 
master_log_pos=645;


master_host="192.168.88.13",  # 主服务器的ip
master_port=13306,  # 主服务器的端口
master_user="repluser",       # 用户名
master_password="123456", # 密码
master_log_file="mysql1.000001",  # 刚刚主查出来的
master_log_pos=645;  # 刚刚主查出来的

4.启动从库上的复制相关线程

复制代码
mysql> start slave ; 
线程名称 核心职责
Slave IO Thread(IO 线程) 连接主库 → 读取主库的二进制日志(binlog) → 将 binlog 内容写入从库的中继日志(relay log)
Slave SQL Thread(SQL 线程) 读取从库的中继日志 → 解析其中的 SQL 语句 → 在从库上执行这些 SQL,实现数据和主库一致

5.查看状态信息

复制代码
mysql> show slave status \G 

mysql> show slave status \G 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.88.53
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql53.000001
          Read_Master_Log_Pos: 667
               Relay_Log_File: mysql54-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql53.000001
             Slave_IO_Running: Yes   //IO线程
            Slave_SQL_Running: Yes   //SQL线程

三、测试

1.在master服务器建库 、建表、添加用户 、授权

复制代码
[root@mysql53 ~]# mysql 连接服务
mysql> create database gamedb; 建库
mysql> create table gamedb.user(name char(10) , class char(3)); 建表
mysql> create user plj@"%" identified by "123456"; 创建用户
mysql> grant select,insert,update,delete on gamedb.* to plj@"%" ; 授予权限

2.在slave服务器查看库、表、用户

复制代码
[root@mysql54 ~]# mysql 连接服务
mysql> show  databases; 查看库
mysql> desc gamedb.user; 查看表头
mysql> select user from mysql.user where user="plj"; 查看用户
mysql> show grants for plj@"%" ; 查看权限

3.连接master服务器存储数据

复制代码
mysql -h192.168.88.53 -uplj -p123456
mysql> insert into gamedb.user values ("yaya","nsd");
mysql> select  * from gamedb.user;
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
+------+-------+
1 row in set (0.01 sec)
mysql>

4.连接从服务器查看数据

复制代码
[root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456 
Mysql> select  * from gamedb.user;
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
+------+-------+
Mysql> 
相关推荐
笃行3506 小时前
数据库性能优化的五处内核改造,KingbaseES 配了四件诊断工具
数据库
非优秀程序员8 小时前
第02章 杭电的英语老师与一台服务器|《互联网产品恩仇录》
数据库·node.js·代码规范
SelectDB13 小时前
SelectDB 是哪个公司的?是国产数据库吗?一篇讲清飞轮科技与 Apache Doris 的关系
数据库
这个DBA有点耶13 小时前
库内计算推理:一条SQL背后的算子融合,和外挂方案差了几次网络往返
数据库·人工智能·sql
这个DBA有点耶15 小时前
函数索引的“完全一致”有多苛刻:写法、字符集、类型,差一处就失效
数据库·mysql·代码规范
星恒随风15 小时前
MySQL 从零入门(三):表的约束详解
linux·服务器·数据库·笔记·mysql
Alluxio16 小时前
Alluxio + OCI:为 AI 工作负载实现亚毫秒访问延迟与可扩展吞吐
运维·数据库·人工智能·分布式·缓存·ai·语言模型
数据库急诊室_阿哲20 小时前
一次加索引建议的复核记录:从报告到签字
数据库·ai数据库
青瓦梦滋20 小时前
Redis主从复制
服务器·数据库·redis·缓存·bootstrap
慧都小妮子21 小时前
Wyn商业智能国产数据库适配:数据接入、分析缓存与审计日志验证清单
数据库·数据分析·数据可视化·审计日志·商业智能