《MySQL系列-主从相关》Docker安装MySQL,实现主从复制

本文详细介绍了如何在Docker环境下配置MySQL的主从复制。首先,分别创建并配置了两个MySQL容器作为主从节点,包括修改配置文件以开启binlog和设置server_id。接着,在主节点上创建了具有复制权限的用户,并查看了主节点的日志和状态。然后,从节点配置了中继日志,并设置为主节点的从属,通过CHANGE MASTER命令同步主节点信息。最后,验证了主从复制成功,主节点的数据变化能实时同步到从节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

Docker安装MySQL,实现主从复制

一、前言

1 Docker安装MySQL

参考历史文章Docker安装MySQL,准备两台MySQL容器

master节点

  • 容器名称

    MySQL01

  • 容器ID

    faf2312fd62a

  • 端口

    33061

slave节点

  • 容器名称

    MySQL01

  • 容器ID

    dfc693c2bb04

  • 端口

    33062

2 MySQL主从复制配置实操

参考历史文章MySQL主从复制配置实操,实现MySQL容器主从配置

二、MySQL01(主节点)

*1 修改主节点配置文件

  • log-bin = mysql-bin

    开启binlog

  • binlog_format = mixed

    指定binlog的格式

  • server-id = 1

    指定节点id

  • innodb_file_per_table = ON

    独立表空间

    在MySQL创建表时,如果设置为ON,则会生成两个文件tablename.frmtablename.idb

    如果设置为OFF,则只会生成一个table.frm文件,此表的数据将存放在idbdata*这个文件中。

  • skip_name_resolve = ON

    优化选项

    禁用DNS解析

[root@zxy_master ~]# docker exec -it faf2312fd62a /bin/bash
bash-4.2# vi /etc/my.cnf

# [mysqld]下添加如下内容
log-bin = mysql-bin
binlog_format = mixed
server-id = 1
innodb_file_per_table = ON
skip_name_resolve = ON

*2 重启主节点容器

修改配置文件后,需要重启容器

[root@zxy_master ~]# docker restart faf2312fd62a
faf2312fd62a

*3 登陆主节点MySQL

直接在主机远程登陆MySQL01容器即可

[root@zxy_master ~]# mysql -uroot -p123456 -h127.0.0.1 -P33061
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 2
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>

4 查看主节点日志

主要查看binlog日志开启状态

mysql> show global variables like '%log%';
+--------------------------------------------+---------------------------------------------+
| Variable_name                              | Value                                       |
+--------------------------------------------+---------------------------------------------+
| back_log                                   | 80                                          |
| binlog_cache_size                          | 32768                                       |
| binlog_checksum                            | CRC32                                       |
| binlog_direct_non_transactional_updates    | OFF                                         |
| binlog_error_action                        | ABORT_SERVER                                |
| binlog_format                              | MIXED                                       |
| binlog_group_commit_sync_delay             | 0                                           |
| binlog_group_commit_sync_no_delay_count    | 0                                           |
| binlog_gtid_simple_recovery                | ON                                          |
| binlog_max_flush_queue_time                | 0                                           |
| binlog_order_commits                       | ON                                          |
| binlog_row_image                           | FULL                                        |
| binlog_rows_query_log_events               | OFF                                         |
| binlog_stmt_cache_size                     | 32768                                       |
| binlog_transaction_dependency_history_size | 25000                                       |
| binlog_transaction_dependency_tracking     | COMMIT_ORDER                                |
| expire_logs_days                           | 0                                           |
| general_log                                | OFF                                         |
| general_log_file                           | /var/lib/mysql/faf2312fd62a.log             |
| innodb_api_enable_binlog                   | OFF                                         |
| innodb_flush_log_at_timeout                | 1                                           |
| innodb_flush_log_at_trx_commit             | 1                                           |
| innodb_locks_unsafe_for_binlog             | OFF                                         |
| innodb_log_buffer_size                     | 16777216                                    |
| innodb_log_checksums                       | ON                                          |
| innodb_log_compressed_pages                | ON                                          |
| innodb_log_file_size                       | 50331648                                    |
| innodb_log_files_in_group                  | 2                                           |
| innodb_log_group_home_dir                  | ./                            
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DATA数据猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值