docker hub mysql主从_使用 Docker Compose 搭建 MySQL 数据库主从复制实例

说明

由于是单机同时运行主从实例,仅用于开发环境或学习主从复制配置。

文件结构

env

根目录下的 .env 文件作用域是在 docker-compose.yaml 中,而 env 路径下的文件作用与容器内部环境变量,两者作用不同。

.env

docker-compose.yaml

由于使用了 YAML 的引用语法,可以通过 docker-compose config 查看完整的内容。这里配置了一个主库,两个从库,可以根据需求改变从库数量。

初始化实例

基础镜像会在初始化(仅首次运行)时按文件名顺序执行 /docker-entrypoint-initdb.d 下的 .sql .sh 等文件,详细信息可以查看镜像说明页面:

Initializing a fresh instance

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

配置实例参数

使用执行参数可以不依靠 my.cnf 对实例进行配置,详细资料可以查看镜像说明页面:

Configuration without a cnf file Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:

If you would like to see a complete list of available options, just run:

主从配置

init-master.sh

在主库中添加用于复制的帐号,由于镜像默认开启 --skip-name-resolve 参数,因此只能通过 IP 配置权限。

注:脚本中是获取容器默认网关网段后添加用户,不适用于生产环境。

init-node.sh

配置从库时会先等待主库就绪,并在从库连接到主库获取必要的参数值进行从库初始化并开启从库复制状态。

使用说明

根据需要调整变量值与脚本,修改 docker-compose.yaml 中各个实例暴露的端口,在项目目录下执行 docker-compose up -d 部署编排。

使用 docker-compose logs mysql-master 可以查看对应容器输出日志。

使用 docker exec -ti mysql-master bash 可进入对应容器控制台环境。

其他

GTIDs 模式需要调整脚本与编排文件中的执行参数。如果想使用容器来为已有实例添加从库,需要修改 init-node.sh 将 File 与 Position 作为环境变量传入应该更合适。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值