如何使用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-bin server-id=1 gtid-mode=on enforce-gtid-consistency=on
!includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] server-id=2 gtid-mode=on enforce-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 | +--------------------+
以上是如何使用Docker部署MySQL5.7&8.0主从集群的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MySQL和phpMyAdmin是强大的数据库管理工具。1)MySQL用于创建数据库和表、执行DML和SQL查询。2)phpMyAdmin提供直观界面进行数据库管理、表结构管理、数据操作和用户权限管理。

容器化技术如Docker增强而非替代Java的平台独立性。1)确保跨环境的一致性,2)管理依赖性,包括特定JVM版本,3)简化部署过程,使Java应用更具适应性和易管理性。

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

Docker在Linux上重要,因为Linux是其原生平台,提供了丰富的工具和社区支持。1.安装Docker:使用sudoapt-getupdate和sudoapt-getinstalldocker-cedocker-ce-clicontainerd.io。2.创建和管理容器:使用dockerrun命令,如dockerrun-d--namemynginx-p80:80nginx。3.编写Dockerfile:优化镜像大小,使用多阶段构建。4.优化和调试:使用dockerlogs和dockerex

MySQL和MariaDB的主要区别在于性能、功能和许可证:1.MySQL由Oracle开发,MariaDB是其分支。2.MariaDB在高负载环境中性能可能更好。3.MariaDB提供了更多的存储引擎和功能。4.MySQL采用双重许可证,MariaDB完全开源。选择时应考虑现有基础设施、性能需求、功能需求和许可证成本。

SQL是一种用于管理关系数据库的标准语言,而MySQL是一个使用SQL的数据库管理系统。SQL定义了与数据库交互的方式,包括CRUD操作,而MySQL实现了SQL标准并提供了额外的功能,如存储过程和触发器。

安全地处理JSON中的函数和正则表达式在前端开发中,经常需要将JavaScript...

MySQL和phpMyAdmin可以通过以下步骤进行有效管理:1.创建和删除数据库:在phpMyAdmin中点击几下即可完成。2.管理表:可以创建表、修改结构、添加索引。3.数据操作:支持插入、更新、删除数据和执行SQL查询。4.导入导出数据:支持SQL、CSV、XML等格式。5.优化和监控:使用OPTIMIZETABLE命令优化表,并利用查询分析器和监控工具解决性能问题。
