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方式進行主從的話,那麼整個就會變得方便很多,只需要執行後面的主從關係配置就可以了。

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