docker构建mysql主从复制

   

mysql主节点

Dockerfile

mkdir mysql-master
cd mysql-master
vim Dockerfile

Dockerfile文件内容

From mysql:5.7.25
MAINTAINER tigerfive tigerfive@aliyun.com

ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
COPY conf.sh /mysql/conf.sh
COPY privileges.sql /mysql/privileges.sql

CMD ["sh", "/mysql/conf.sh"]

conf.sh文件内容

#!/bin/bash

set -e

echo '1\. set server_id....'
sed -i '/\[mysqld\]/a server-id=1\nlog-bin=/var/log/mysql/mysql-bin\ngtid-mode=ON\nenforce-gtid-consistency=ON' /etc/mysql/mysql.conf.d/mysqld.cnf

echo '2\. start mysql...'
service mysql start

echo '3\. setting password...'
sed -i 's/MYSQLROOTPASSWORD/'$MYSQL_ROOT_PASSWORD'/' /mysql/privileges.sql
sed -i 's/MYSQLREPLICATIONUSER/'$MYSQL_REPLICATION_USER'/' /mysql/privileges.sql
sed -i 's/MYSQLREPLICATIONPASSWORD/'$MYSQL_REPLICATION_PASSWORD'/' /mysql/privileges.sql
mysql < /mysql/privileges.sql

echo '4\. service mysql status'
echo 'mysql for tigerfive if ready...'

tail -f /dev/null

privileges.sql文件内容

use mysql;
set password for root@'localhost' = password("MYSQLROOTPASSWORD");
grant all on *.* to "MYSQLREPLICATIONUSER"@'%' identified by "MYSQLREPLICATIONPASSWORD" with grant option;
flush privileges;

构建镜像

docker build -t tigerfive/mysql5.7-master:2.1 .

测试

##启动
docker run  --name tigersql -e MYSQL_ROOT_PASSWORD="1234567" -e MYSQL_REPLICATION_USER="repl"  -e MYSQL_REPLICATION_PASSWORD="1234567"  -d tigerfive/mysql5.7-master:2.1
docker exec -it tigersql bash
mysql -uroot -p1234567
##查看权限
select * from mysql.user where user='repl'\G

从节点

Dockerfile

mkdir mysql-slave
cd mysql-slave
vim Dockerfile

Dockerfile文件内容

From mysql:5.7.25
MAINTAINER tigerfive tigerfive@aliyun.com

ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
COPY conf.sh /mysql/conf.sh
COPY privileges.sql /mysql/privileges.sql

CMD ["sh", "/mysql/conf.sh"]

conf.sh文件内容

#!/bin/bash

set -e

echo '1\. set server_id...'
RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})"
sed -i '/\[mysqld\]/a server-id='$RAND'\nlog-bin=/var/log/mysql/mysql-bin\ngtid-mode=ON\nenforce-gtid-consistency=ON' /etc/mysql/mysql.conf.d/mysqld.cnf

echo '2\. start mysql...'
service mysql start

echo '3\. setting password...'
sed -i 's/MYSQLROOTPASSWORD/'$MYSQL_ROOT_PASSWORD'/' /mysql/privileges.sql
sed -i 's/MYSQLMASTERSERVICEHOST/'$MYSQL_MASTER_SERVICE_HOST'/' /mysql/privileges.sql
sed -i 's/MYSQLREPLICATIONUSER/'$MYSQL_REPLICATION_USER'/' /mysql/privileges.sql
sed -i 's/MYSQLREPLICATIONPASSWORD/'$MYSQL_REPLICATION_PASSWORD'/' /mysql/privileges.sql

mysql < /mysql/privileges.sql

echo '4\. service mysql status'
echo 'mysql for tigerfive if ready...'

tail -f /dev/null

privileges.sql文件内容

use mysql;
set password for root@'localhost' = password('MYSQLROOTPASSWORD');
flush privileges;
CHANGE MASTER TO master_host='MYSQLMASTERSERVICEHOST', master_user='MYSQLREPLICATIONUSER', master_password='MYSQLREPLICATIONPASSWORD' ;
START SLAVE;

构建镜像

docker build -t tigerfive/mysql5.7-slave:2.1  .

启动

docker run  --name tigersql2 -e MYSQL_ROOT_PASSWORD="1234567" -e MYSQL_MASTER_SERVICE_HOST="172.17.0.2" -e MYSQL_REPLICATION_USER='repl' -e MYSQL_REPLICATION_PASSWORD="1234567"   -d tigerfive/mysql5.7-slave:2.1

测试

docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                 NAMES
49a02643a28c        tigerfive/mysql5.7-slave:2.1    "docker-entrypoint.s…"   39 seconds ago      Up 38 seconds       3306/tcp, 33060/tcp   tigersql2
acd45de6c4fc        tigerfive/mysql5.7-master:2.1   "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        3306/tcp, 33060/tcp   tigersql

主机

docker exec -it tigersql bash
mysql -uroot -p1234567
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
CREATE DATABASE TEST;
use TEST;
CREATE TABLE book(name char(20),author char(20));
show tables;
+----------------+
| Tables_in_TEST |
+----------------+
| book           |
+----------------+
1 row in set (0.00 sec)

mysql> quit

从机

docker exec -it tigersql2 bash
mysql -uroot -p1234567
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
use TEST;
show tables;
+----------------+
| Tables_in_TEST |
+----------------+
| book           |
+----------------+
1 row in set (0.00 sec)

mysql> quit

主从测试OK
共有仓库镜像:
tigerfive/mysql5.7-slave:2.1
tigerfive/mysql5.7-master:2.1

发表评论