Docker搭建mysql主从复制集群

Docker搭建mysql主从复制集群

搭建步骤

  • 准备docker镜像
docker search mysql
docker pull mysql
  • 准备映射路径
#master路径准备
mkdir -m777  /Users/a1/develop/docker/mysql/master01/data
mkdir -m777  /Users/a1/develop/docker/mysql/master01/conf
#slave路径准备
mkdir -m777  /Users/a1/develop/docker/mysql/slave01/data
mkdir -m777  /Users/a1/develop/docker/mysql/slave01/conf
  • 主库容器创建及配置
#master的conf文件夹中创建my.cnf配置文件,并写入相关配置
vi my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1

#创建docker容器
docker create --name mysql_master01 -p 3306:3306 \
-v /Users/a1/develop/docker/mysql/master01/data:/var/lib/mysql \
-v /Users/a1/develop/docker/mysql/master01/conf:/etc/my.cnf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql

#启动master容器并跟踪日志
docker start mysql_master01 && docker logs -f mysql_master01

#进入master容器,并执行相关配置sql
docker exec -it mysql_master01 /bin/bash
mysql -uroot -p123456
create user 'trans'@'%' identified by 'trans' ;#创建同步账户
grant replication slave on *.* to 'trans'@'%'; #授予创建的同步账户的同步权限
flush privileges ;

#查看master状态,可以得到binlog日志文件和postion
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      858 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 从库容器创建及配置
#slave的conf文件夹中创建my.cnf配置文件,并写入相关配置
vi my.cnf
[mysqld]
server-id=2

#创建docker容器
docker create --name mysql_slave01 -p 3307:3306 \
-v /Users/a1/develop/docker/mysql/slave01/data:/var/lib/mysql \
-v /Users/a1/develop/docker/mysql/slave01/conf:/etc/my.cnf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql

#启动slave容器节点,并跟踪日志
docker start mysql_slave01 && docker logs -f mysql_slave01

#进入slave容器,并进行相关配置,开启slave
(base) a1@192 conf % docker exec -it mysql_slave01 /bin/bash
bash-4.4# mysql -uroot -p123456

#配置slave的master节点,并启动slave
change master to
    master_host='127.0.0.1',
    master_user='trans',
    master_password='trans',
    master_port=3306,
    master_log_file='binlog.000002',
    master_log_pos=858;
#启动slave
start slave ;
  • 验证
#查看slave状态
show slave status ;

Docker搭建mysql主从复制集群_第1张图片

#创建数据库建表插入数据测试

你可能感兴趣的:(数据库,docker,mysql,adb)