docker创建主从关系的mysql数据库

一 准备


1. 准备镜像

  首先要下载mysql镜像,最好是下载官方网站的镜像,我这里下载的版本是++mysql:5.7.27++镜像:

docker pull mysql:5.7.27

2. 准备目录

  既然开始了,就要在linux操作系统上准备一个目录用来存放这些文件:

/home/docker/mysql
/home/docker/mysql/master
/home/docker/mysql/slave

3. 准备my.cnf文件

  my.conf文件时mysql启动的配置文件,这个文件我是从镜像容器里面复制出来的,然后添加了一部分内容。

添加内容为

[mysqld]
# 表示此MySQL服务器级别,这里我设置1是主服务,2是从服务
server-id = 1
# 开启二进制记录。这是为了主从复制而做的设置
log-bin = mysql-bin

原镜像的my.cnf文件内容为

# Copyright (c) 2016, 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

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

合并生成新的内容为

# Copyright (c) 2016, 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

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
# 表示此MySQL服务器级别,这里我设置1是主服务,2是从服务
server-id = 1
log-bin = mysql-bin

  合并完成之后就是将my.cnf文件放到指定的位置.
  server-id=1的my.cnf文件放在 ++/home/docker/mysql/master/conf++ 下面;
  server-id=2的my.cnf文件放在 ++/home/docker/mysql/slave/conf++下面

3. 准备mysql-compose.yml文件

  接下来就是准备mysql-compose.yml文件,其实也是可以直接写命令进行启动的,但是写compose文件,以后可以继续使用。

# Use root/example as user/password credentials
version: '3.1'

services:
  mysql-master:
    image: mysql:5.7.27
    container_name: mysql-master
    restart: always
    ports:
      - "3306:3306"
    volumes:
        - ./master/data:/var/lib/mysql
        - ./master/conf/my.cnf:/etc/mysql/my.cnf
    environment:
        MYSQL_ROOT_PASSWORD: wszgr
  mysql-slave:
    image: mysql:5.7.27
    container_name: mysql-slave
    restart: always
    ports:
      - "3307:3306"
    volumes:
        - ./slave/data:/var/lib/mysql
        - ./slave/conf/my.cnf:/etc/mysql/my.cnf
    environment:
        MYSQL_ROOT_PASSWORD: wszgr

  mysql-compose.yml放在 /home/docker/mysql 下面

二 启动


  采用docker-compose的命令启动,所以要提前安装docker-compose命令。(进入到/home/docker/mysql下执行)

docker-compose -f mysql-compose.yml up -d

三 配置主从关系

  启动之后连接数据库,用户名root,密码wszgr。

  1. 打开主数据库执行下面命令

show master status

获取到一下信息:
在这里插入图片描述
:这个信息是不断变化的,所以要在查询到之后就使用,如果查询到后一段时间才使用,可能数据已经发生了变化

  1. 查看主数据库mysql-master的docker容器ip

docker inspect mysql-master

  我这边查询到的是:

上面换有数据就不粘贴出来的,只是粘贴出来使用的一部分
"NetworkID": "19549390e6c2949a92114b839423cdadf007e3013c9b092637c2cedda8bb1c76",
"EndpointID": "4afd1608976572b0b919c84def53d0706e45dd6d32234a0c90614f2dbb8f8e99",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:03",
"DriverOpts": null

  1. 打开从数据库执行下面命令
change master to   
# 这个是查询出来的ipAddress
master_host='172.19.0.3',   
# 这个是数据库的端口
master_port=3306,   
master_user='root',  
master_password='wszgr',   
# 这个是先前查询出来的表格中的file
master_log_file='mysql-bin.000003',  
# 这个先前查询出来的表格中的position
master_log_pos=556;

然后执行:

show slave status

就可以看到从数据连接主数据库的内容了
在这里插入图片描述

  1. 启动从数据库

start slave

然后执行:

show slave status

然后就可以看到
在这里插入图片描述

查看字段Slave_SQL_Running 和 Slave_IO_Running是否都是yes如果都是yes说明连接成功了,你就可以测试是否主从同步。如果显示connection或者no,则是下面几种情况

  • 主机网络不同,没有连接成功
  • file字段不正确
  • position字段不正确

:当前我做的是一主一从,当前你也可以继续加从

题外话

  我们这里是采用了docker部署的方式,所以在主从连接关系上的前提必须是容器必须能够互通,如果容器都不能互通,主从关系也就连接不会成功。如果采用了跨主机的docker方式,则需要进行跨主机互通。 docker也可以采用–link的方式直接进行主从连接。

  如果不是采用docker方式进行主从的话,那么整个就会变得方便很多,只需要执行后面的主从关系配置就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章