docker.io/mysql里有入口脚本 docker-entrypoint.sh,需要密码(环境变量)
由以上编写脚本:
#!/bin/bash #Get musql image set -e mysql_image=`docker images | grep 'docker.io/mysql' | awk {'print $1'}` if [ -n "$mysql_image" ] then echo 'The docker.io/mysql is already existed.' else echo 'Pull the image.' docker pull docker.io/mysql fi #Create network for zookeeper containers net=`docker network ls | grep yapi_net | awk {'print $2'}` if [ -n "$net" ] then echo 'The kfknetwork is already existed.' else echo 'Create kfknetwork.' docker network create --subnet 172.30.0.0/16 yapi_net fi #Start 3 zookeeper cluster echo 'Start mysql master and slave' rm -rf /opt/mysql_master/data rm -rf /opt/mysql_slave_1/data rm -rf /opt/mysql_slave_2/data mkdir -p /opt/mysql_master/data mkdir -p /opt/mysql_slave_1/data mkdir -p /opt/mysql_slave_2/data master_ip='172.30.0.51' slave_1_ip='172.30.0.52' slave_2_ip='172.30.0.53' docker run --restart always -d --name sql_master --network yapi_net --ip ${master_ip} -v /opt/mysql_master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD='123qqq...A' -p 3307:3306 docker.io/mysql docker run --restart always -d --name sql_slave_1 --network yapi_net --ip ${slave_1_ip} -v /opt/mysql_slave_1/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD='123qqq...A' -p 3308:3306 docker.io/mysql docker run --restart always -d --name sql_slave_2 --network yapi_net --ip ${slave_2_ip} -v /opt/mysql_slave_2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD='123qqq...A' -p 3309:3306 docker.io/mysql #add server_id and log_bin into my.cnf echo 'sleep 30' sleep 30 docker exec -it sql_master sed -ir "/[mysqld]/a log_bin=mas server_id=`echo ${master_ip} |awk -F. '{print $4}'`" /etc/mysql/my.cnf docker exec -it sql_slave_1 sed -ir "/[mysqld]/a log_bin=mas server_id=`echo ${slave_1_ip} |awk -F. '{print $4}'`" /etc/mysql/my.cnf docker exec -it sql_slave_2 sed -ir "/[mysqld]/a log_bin=mas server_id=`echo ${slave_2_ip} |awk -F. '{print $4}'`" /etc/mysql/my.cnf docker restart sql_master docker restart sql_slave_1 docker restart sql_slave_2
1、执行代码
2、启动docker后,不能远程登录,报错:
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
意思是客户端不支持caching_sha2_password的加密方式
修改一下加密方式即可:
alter user 'root'@'%' identified with mysql_native_password by '123qqq...A' (知识补充:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
)
3、登录mysql,在master上授权从用户:
create user 'repl'@'%' identified by 'repl'; #grant replication slave on *.* to 'replicater'@'172.17.0.%' identified by '123qqq...A';会有语法错误,应该是版本太高 grant replication slave on *.* to 'repl'@'%';
reset master; #在master上执行show master status; #查看偏移量和log日制文件名
4、先手动同步数据:
mysqldump -u root -p -A > /var/lib/mysql/mytest.sql #在master上执行,导出时,必须进入容器纸箱
mysql -u root -p < /var/lib/mysql/mytest.sql #都必须进入容器执行
5、登录mysql,在slave上
mysql> change master to master_host='172.30.0.51',master_port=3306,master_user='repl',master_password='repl',master_log_file='fang.000001',master_log_pos=155; Query OK, 0 rows affected, 2 warnings (0.02 sec)
start slave;
show slave statusG;#查看io和sql线程是否ok