一. 创建docker-compose.yml并启动docker compose
mkdir mysql_server
cd mysql_server
sudo vim docker-compose.yml
docker-compose.yml
version: "3" #版本信息
services: #服务列表
mysql_1:
image: mysql:5.7
ports:
- 3306:3306
volumes:
- "/root/mysql_server/node1/data:/var/lib/mysql"
- "/root/mysql_server/node1/conf.d:/etc/mysql/conf.d"
environment:
- MYSQL_ROOT_PASSWORD=123456
restart: always
mysql_2:
image: mysql:5.7
ports:
- 3307:3306
volumes:
- "/root/mysql_server/node2/data:/var/lib/mysql"
- "/root/mysql_server/node2/conf.d:/etc/mysql/conf.d"
environment:
- MYSQL_ROOT_PASSWORD=123456
restart: always
mysql_3:
image: mysql:5.7
ports:
- 3308:3306
volumes:
- "/root/mysql_server/node3/data:/var/lib/mysql"
- "/root/mysql_server/node3/conf.d:/etc/mysql/conf.d"
environment:
- MYSQL_ROOT_PASSWORD=123456
restart: always
启动:
sudo docker-compose up
查看启动的端口:
netstat -lntp
二、MySQL主从配置
由于docker容器中没有vim 首先apt-get update,然后安装vim
1. docker exec -it 进入mysql镜像,修改配置文件
进入node1,在/etc/mysql/mysql.conf.d/mysqld.cnf 中添加:
server-id=100
log-bin=mysql-bin
进入node2,在/etc/mysql/mysql.conf.d/mysqld.cnf中添加:
server-id=101
log-bin=mysql-slave-bin
进入node3,在/etc/mysql/mysql.conf.d/mysqld.cnf 中添加:
server-id=102
log-bin=mysql-slave-bin
2. 进入到主库,查看status:
show master status;
3. 查看master的docker内网ip
sudo docker inspect mysql_server_mysql_1_1
4. 从库执行同步SQL语句:
CHANGE MASTER TO
MASTER_HOST=‘172.18.0.4‘,
MASTER_USER=‘root‘,
MASTER_PASSWORD=‘123456‘,
MASTER_LOG_FILE=‘mysql-bin.000003‘,
MASTER_LOG_POS=154;
如果出现问题:
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;
5. 启动slave同步进程
start slave;
6. 查看slave状态:
show slave status\G
主从配置成功后主库数据新增、修改、删除会同步到从库