目前专注小微企业运维外包

docker-compose mysql主从一键部署

快速部署mysql主从环境

github:https://github.com/younglinuxer/awesome-yuan.git

前提:安装docker及docker-compose

快速部署

git clone https://github.com/younglinuxer/awesome-yuan.git
cd awesome-yuan/mysql/mysql-master-slave/ && docker-compose up -d
# 运行设置主从脚本
docker cp start.sh mysql-slave:/tmp && docker exec -it mysql-slave bash /tmp/start.sh

验证主从同步 在主库导入数据(3306)  查看从库是否同步(3307)
说明
网络连接使用docker-compose之间的服务名调用
slave.conf中配置 即可指定需要同步的数据库
#replicate-do-db = db1
read_only=1  #设置从库只读 root用户一样可以写入 需要限制root则需要添加下面配置
super_read_only=1
start.sh
#!/usr/bin/env bash
#设置主从使用脚本

Repl_passwd="aA123%^"
M_USER_PASSWD="bGludXIK"
S_USER_PASSWD="eW91bmcK"

add_user_sql="grant replication slave, replication client on *.* to 'repl'@'%' identified by  'Repl_passwd';"
File=`echo "SHOW MASTER STATUS" | MYSQL_PWD=M_USER_PASSWD  mysql -uroot -h mysql-master -N|awk '{print1}'`
Position=`echo "SHOW MASTER STATUS" | MYSQL_PWD=M_USER_PASSWD  mysql -uroot -h mysql-master -N|awk '{print2}'`
slave_sql="change master to master_host='mysql-master',master_user='repl',master_password='Repl_passwd',master_port=3306,master_log_file='File',master_log_pos=Position,master_connect_retry=30;"


master_sql(){
  echo "add_user_sql"
  echo "add_user_sql" | MYSQL_PWD=M_USER_PASSWD  mysql -uroot -h mysql-master -N
  sleep 6
}

slave_sql(){
  echo "slave_sql "
  echo "slave_sql" | MYSQL_PWD=S_USER_PASSWD  mysql -uroot -h mysql-slave -N
  sleep 6
  echo "start slave;" | MYSQL_PWD=S_USER_PASSWD  mysql -uroot -h mysql-slave -N
  echo "show slave status \G;" | MYSQL_PWD=S_USER_PASSWD  mysql -uroot -h mysql-slave -N

}

#echo "add_user_sql"
master_sql
slave_sql
脚本说明