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> 
相关推荐
火山上的企鹅2 小时前
Codex实战:APP远程升级服务搭建(三)后台管理页面(APK 上传、版本管理、多应用页签)
服务器·网络·数据库·oracle·qgc
阿狸猿2 小时前
论 NoSQL 数据库技术及其应用
数据库·nosql
FBI HackerHarry浩2 小时前
DataGrip2023.2.3默认保存的数据库和.sql文件在哪里?怎么修改默认路径?
数据库
袁小皮皮不皮2 小时前
3.HCIP OSPF补充知识(优化版)
服务器·网络·数据库·网络协议·智能路由器
运筹vivo@3 小时前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python
志栋智能3 小时前
超自动化巡检:知识沉淀与团队协作的新载体
大数据·运维·网络·数据库·人工智能·自动化
syt_biancheng3 小时前
Redis初识
数据库·redis·缓存
cmes_love4 小时前
股票逐笔level2历史行情下载十档订单薄五档tick分钟下载分享
数据库·区块链
仙俊红4 小时前
SQL 调优需要掌握的知识
数据库·sql
fofantasy5 小时前
NSK LH12AN 微型导轨技术手册
运维·网络·数据库·经验分享·规格说明书