基于 Docker 的 MySQL 主从复制搭建
Docker 是一个开源的引擎,可以轻松的为任何应用创建一个轻量级、可移植、自给自足的容器。
Docker 通常用于如下场景:
- web 应用的自动化打包发布
- 自动化测试和持续集成、发布
- 在服务性环境中部署和调整数据库或其他的后台应用
Docker 是一种新型的虚拟化方式,和传统的的虚拟化方式相比具有以下优势:
- Docker 容器的启动可以在秒级实现,这比传统的的虚拟机方式要快很多。
- Docker 对系统资源的利用率很高,一台主机上可以同时运行数千个 Docker 容器。
本文主要讲 MySQL 的主从复制的搭建,使用 Docker 主要是手头资源有限,只有一台 MacBook,而 Docker 又能满足我需要多台 MySQL 服务器的需求,所以就选用了 Docker 来进行搭建。Docker 的具体使用可以去找相关的书籍或其他资料自行进行学习,本文就不涉及 Docker 的具体命令的讲解了。
本文分成以下几步来说明如何基于 Docker 来搭建 MySQL 的主从复制:
- 准备两台 MySQL 服务器
- 配置主服务器(Master)
- 配置从服务器(Slave)
- 完成Master和Slave链接
- 测试配置是否成功
1. 准备两台 MySQL 服务器
使用 Docker 创建 MySQL 服务器很简单:
[code]docker run --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
命令解释:
[code]通过镜像 mysql:latest 启动一个名为 mysql_master 的 MySQL 服务器,端口号是3306,映射的宿主机端口号是3306,root 账号密码是123456
使用同样的方式创建 Slave 服务器:
[code]docker run --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
使用
docker ps查看当前运行的容器,如下:
正在运行中的容器
我们还需要知道这两台服务器的 IP 地址:
查看服务器IP地址
Master 服务器地址是 172.17.0.2 ,Slave 服务器地址是 172.17.0.3,MySQL 的端口号都为 3306。
好了,到现在为止,我们已经有了两台 MySQL 服务器。
2. 配置主服务器(Master)
首先,进入到 Master 服务器。
[code]docker exec -it aeba94ef3aa6 /bin/bash
aeba94ef3aa6 是 mysql_master 的容器id。
搭建主从复制时,mysql保持版本的一致。
修改配置文件--my.cnf:
[code]cat /etc/mysql/my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server_id=100 log-bin=mysql-bin # Custom config should go here !includedir /etc/mysql/conf.d/
在我的容器中,my.cnf 的路径是 /etc/mysql。
配置完成后重启容器。
接下来创建数据同步用户:
[code]CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
这里主要是要授予用户 slave REPLICATION SLAVE权限和REPLICATION CLIENT权限。
3. 配置从服务器(Slave)
修改配置文件--my.cnf
[code]cat /etc/mysql/my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=101 log-bin=mysql-slave-bin relay_log=edu-mysql-relay-bin # Custom config should go here !includedir /etc/mysql/conf.d/
配置完成后重启mysql,和配置 Master 一样,会使容器停止,需要启动容器。
4. 完成Master和Slave链接
注意,需要保证 Master 和 Slave 除了不同步的数据库,其他数据库的数据要一致。
在 Master 进入 MySQL, 然后执行命令:
[code]show master status;
结果如下:
master status
记录下 File 和 Position 字段的值,后面会用到。
然后到 Slave 中进入 mysql,执行命令:
[code]change master to master_host='172.17.0.2', master 4000 _user='slave', master_password='123456', master_port=3306, master_log_file='edu-mysql-bin.000001', master_log_pos=929, master_connect_retry=30;
命令解释:
[code]master_host: Master 的IP地址 master_user: 在 Master 中授权的用于数据同步的用户 master_password: 同步数据的用户的密码 master_port: Master 的数据库的端口号 master_log_file: 指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值 master_log_pos: 从哪个 Position 开始读,即上文中提到的 Position 字段的值 master_connect_retry: 当重新建立主从连接时,如果连接失败,重试的时间间隔,单位是秒,默认是60秒。
在 Slave 的 MySQL 终端执行查看主从同步状态
[code]show slave status \G;
结果如下:
slave status
SlaveIORunning 和 SlaveSQLRunning 是No,表明 Slave 还没有开始复制过程。相反 SlaveIORunning 和 SlaveSQLRunning 是Yes表明已经开始工作了,因为我已经运行过了,所以我的显示的都是 Yes。
执行以下命令,开始开启主从同步:
[code]start slave;
OK!
5. 测试配置是否成功
最后一步,测试是否可以同步。测试方法比较多,可以在 Master 中增加一个数据库,然后去 Slave 中查看是否同步过来了,如果没有成功,请仔细检查你的配置文件和配置过程。
- 基于Docker搭建MySQL主从复制
- 基于Docker搭建MySQL主从复制
- 基于Docker搭建MySQL主从复制
- 基于docker搭建的mysql主从复制
- 基于Docker的Mysql主从复制搭建
- Docker Compose搭建mysql主从复制
- MySQL--------基于半同步复制搭建主从
- MySQL 主从复制搭建,基于日志(binlog)
- MySQL 主从复制搭建,基于日志(binlog)
- mysql 5.7 docker 主从复制架构搭建教程
- mysql 5.7 docker 主从复制架构搭建教程
- Docker学习+基于Docker MySQL的主从复制
- 基于Docker的MySql主从复制
- mysql之 mysql 5.6不停机主从搭建(一主一从基于日志点复制)
- 基于Docker MySQL的主从复制
- mysql之 mysql 5.6不停机主从搭建(一主一从基于日志点复制)
- mysql5.5搭建主从复制,半同步复制,基于ssl的主从复制
- postgresql基于docker主从复制搭建
- mysql 5.7 docker 主从复制架构搭建
- MySql基于GTID主从复制的搭建