本次带来的是基于Docker-compose构建出MySQL单节点和主从复制的MySQL环境,本次使用的依旧是bitnami的第三方镜像资源。希望通过这个文章对于学习和使用Docker搭建MySQL的你提供帮助。
下面提供基于Docker-Compose 构建出单节点的MySQL服务的yml文件:
version: '2.1'
services:
mysql:
image: docker.io/bitnami/mysql:8.0
ports:
- '3306:3306'
volumes:
- 'mysql_data:/bitnami/mysql/data'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mysql/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6
volumes:
mysql_data:
driver: local
下面是Docker-Compose构建MySQL主从复制集群的yml配置文件
version: '2.1'
services:
mysql-master:
image: docker.io/bitnami/mysql:8.0
ports:
- '3306'
volumes:
- 'mysql_master_data:/bitnami/mysql/data'
environment:
- MYSQL_REPLICATION_MODE=master
- MYSQL_REPLICATION_USER=repl_user
- MYSQL_USER=my_user
- MYSQL_DATABASE=my_database
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- MYSQL_ROOT_PASSWORD=my_root_password
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mysql/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6
mysql-slave:
image: docker.io/bitnami/mysql:8.0
ports:
- '3306'
depends_on:
- mysql-master
environment:
- MYSQL_REPLICATION_MODE=slave
- MYSQL_REPLICATION_USER=repl_user
- MYSQL_USER=my_user
- MYSQL_DATABASE=my_database
- MYSQL_MASTER_HOST=mysql-master
- MYSQL_MASTER_PORT_NUMBER=3306
- MYSQL_MASTER_ROOT_PASSWORD=my_root_password
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mysql/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6
volumes:
mysql_master_data:
driver: local
下面是基于Docker-Compose构建MySQL主从复制的一些环境变量的配置信息: