mysql-master
Dockerfile
首先,Dockerfile 长这样,具体的命令上面有解释。核心逻辑就是拷贝两个文件进去,然后在容器启动的时候执行 conf.sh,由 conf.sh 执行另外一个文件。
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"]
这里首先设置允许免密登录是为了方便后面配置,密码最后再通过 privileges.sql 来设置。
启动脚本
#!/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
这个文件是对 MySQL 进行一些权限配置,比如设置用户密码,创建新用户,数据库授权等。
后面两句很重要
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
root@9fa3dcf5299e:/# mysql -uroot -p1234567
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
查看权限:
mysql> select * from mysql.user where user='repl'\G
*************************** 1. row ***************************
Host: %
User: repl
Nice,成功创建mysql-master镜像
mysql-slave
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"]
启动脚本
#!/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 .
查看mysql-master的容器IP
docker inspect tigersql |grep IPAddr
ip为: 172.17.0.2
启动命令
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
主从测试OK
可以直接拉取上传共有仓库的镜像
tigerfive/mysql5.7-slave:2.1
tigerfive/mysql5.7-master:2.1
仓库还有8.0的版本,欢迎使用
后续更新k8s版本的镜像制作和yaml文件编写,实现一主多从