黄东旭解析 TiDB 的核心优势
755
2023-07-02
如何使用Docker部署MySQL5.7&8.0主从集群
> 部署 mysql 5.7 集群 master & slave (仅测试用)
镜像版本 5.7
1、创建 overlay 网络
docker network create --driver overlay common-network --attachable登录后复制登录后复制
2、编辑两个配置文件 master.cnf 与 slave.cnf
!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/[mysqld]log-bin=mysql-binserver-id=1gtid-mode=onenforce-gtid-consistency=on登录后复制
!includedir /etc/mysql/conf.d/!includedir /etc/mysql/mysql.conf.d/[mysqld]server-id=2gtid-mode=onenforce-gtid-consistency=on登录后复制
3、启动 2 个 mysql:mysql-master 、mysql-slave
docker run -d \--name mysql-master \--network common-network \-e mysql_root_password=passw0rd \-v `pwd`/master.cnf:/etc/mysql/my.cnf \-p 3306:3306 \-d mysql:5.7登录后复制
docker run -d \--name mysql-slave \--network common-network \-e mysql_root_password=passw0rd \-v `pwd`/slave.cnf:/etc/mysql/my.cnf \-p 3307:3306 \-d mysql:5.7登录后复制
4、添加从库用于复制的用户
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -ppassw0rd \-e "create user 'repl'@'%' identified by 'password' require ssl; " \-e "grant replication slave on *.* to 'repl'@'%';"登录后复制
5、连接 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd \-e "change master to master_host='mysql-master', master_port=3306, master_user='repl', master_password='password', master_auto_position=1, master_ssl=1;" \-e "start slave;"登录后复制登录后复制
6、验证 slave 状态
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd -e "show slave status\g"登录后复制登录后复制
如下状态为正常:
slave_io_running: yes
slave_sql_running: yes
> 部署 mysql 8.0 集群 master & slave(仅测试用)
镜像版本 mysql:8.0
1、创建 overlay 网络
docker network create --driver overlay common-network --attachable登录后复制登录后复制
2、启动 2 个 mysql:mysql-master 、mysql-slave
docker run -d \--name mysql-master \--network common-network \-e mysql_root_password=passw0rd \-p 3306:3306 \-d mysql --default-authentication-plugin=mysql_native_password登录后复制
docker run -d \--name mysql-slave \--network common-network \-e mysql_root_password=passw0rd \-p 3307:3306 \-d mysql --default-authentication-plugin=mysql_native_password登录后复制
3、配置 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -ppassw0rd \-e "set persist server_id=1;" \-e "set persist_only gtid_mode=on;" \-e "set persist_only enforce_gtid_consistency=true; " \-e "create user 'repl'@'%' identified by 'password' require ssl; " \-e "grant replication slave on *.* to 'repl'@'%';"登录后复制
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd \-e "set persist server_id=2;" \-e "set persist_only gtid_mode=on;" \-e "set persist_only enforce_gtid_consistency=true; "登录后复制
4、重启 master & slave
docker restart mysql-master mysql-slave登录后复制
5、连接 master & slave
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd \-e "change master to master_host='mysql-master', master_port=3306, master_user='repl', master_password='password', master_auto_position=1, master_ssl=1;" \-e "start slave;"登录后复制登录后复制
6、验证 slave 状态
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd -e "show slave status\g"登录后复制登录后复制
如下状态为正常:
slave_io_running: yes
slave_sql_running: yes
> 验证数据同步
在 master 创建数据库 anoyi
docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -ppassw0rd \ -e "create database anoyi default character set utf8mb4 collate utf8mb4_general_ci;"登录后复制
在 slave 查看数据库列表
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -ppassw0rd \ -e "show databases;"mysql: [warning] using a password on the command line interface can be insecure.+--------------------+| database |+--------------------+| anoyi || information_schema || mysql || performance_schema || sys |+--------------------+登录后复制
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。