docker-compose部署mysql高可用
时间: 2023-12-23 09:27:11 浏览: 141
docker-compose 部署 mysql服务
以下是使用docker-compose部署mysql高可用的步骤:
1. 在两台物理机器上分别创建目录和文件:
- 在master-1上创建目录和文件:
```shell
cd /
mkdir docker
cd /docker
mkdir mysql8
cd mysql8
mkdir mysql8data
touch master_1_init.sql
touch Dockerfile_master_1
touch docker-compose.yml
```
- 在master-2上创建目录和文件:
```shell
cd /
mkdir docker
cd /docker
mkdir mysql8
cd mysql8
mkdir mysql8data
touch master_2_init.sql
touch Dockerfile_master_2
touch docker-compose.yml
```
2. 配置master-1的docker-compose.yml文件:
```yaml
version: '3'
services:
mysql-master-1:
build:
context: .
dockerfile: Dockerfile_master_1
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql8data:/var/lib/mysql
ports:
- 3306:3306
networks:
- mysql-network
networks:
mysql-network:
driver: bridge
```
3. 配置master-2的docker-compose.yml文件:
```yaml
version: '3'
services:
mysql-master-2:
build:
context: .
dockerfile: Dockerfile_master_2
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql8data:/var/lib/mysql
ports:
- 3307:3306
networks:
- mysql-network
networks:
mysql-network:
driver: bridge
```
4. 配置master-1的Dockerfile_master_1文件:
```Dockerfile
FROM mysql:8.0
COPY master_1_init.sql /docker-entrypoint-initdb.d/
```
5. 配置master-2的Dockerfile_master_2文件:
```Dockerfile
FROM mysql:8.0
COPY master_2_init.sql /docker-entrypoint-initdb.d/
```
6. 在master-1和master-2上分别执行以下命令启动mysql容器:
```shell
docker-compose up -d
```
7. 进入mysql-slave容器,查看同步情况:
```shell
docker exec -ti mysql-slave bash
mysql -uroot -p123456
SHOW DATABASES;
```
阅读全文