数据库主从(一主一从)模式,docker配置步骤

记录一下今天数据库的主从配置

  1. 配置docker-compose.yml
version: '3'
services:
  mysqmaster: 
    image: mysql
    restart: always
    container_name: mysql-master
    environment:
      MYSQL_ROOT_PASSWORD: 123456 #root账户密码
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --max_allowed_packet=128M
    ports:
      - 3306:3306
      - 33060:33060
    volumes:
      - ./master/data:/var/lib/mysql    #data数据映射
      - ./master/config/my.cnf:/etc/mysql/my.cnf    # 配置
      - ./master/log/err.log:/var/log/err.log   # 错误日志

  mysqlslave: 
    image: mysql
    container_name: mysql-slave1
    ports:
    - 6607:3306
    command:
      - "--user=root"
    volumes:
      - ./slave/data:/var/lib/mysql   
      - ./slave/config/my.cnf:/etc/my.cnf
      - ./slave/log/err.log:/var/log/err.log
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    restart: always

  1. 配置my.conf,master和slave除了server-id外都一样
# 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]
server-id=1 #主从不能配置成一样
log-bin=mysql-bin
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
log-error=/var/log/err.log

# Custom config should go here
!includedir /etc/mysql/conf.d/

default_authentication_plugin= mysql_native_password
default-time-zone= '+8:00'

上面两步完成后启动容器,具体方式不加赘述,请百度docker

  1. 设置主数据库

给某一账号赋予主从复制权限

GRANT REPLICATION SLAVE ON . TO ‘root’@’%’;

刷新权限

flush privileges

查看主数据库的状态,并记录下来,配置从数据库时会用到
> show master status[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NxjKWaHI-1606142917232)(F5B420D352AA40CCBB9D488E84A7C1F1)]

其中File指的是最新binlog二进制文件的名称,Position指的是偏移量

  1. 配置从数据库
    输入以下命令,将从数据库连接上主数据库(参数用自己配置的,端口默认是3306,可以用MASTER_PORT修改)

CHANGE MASTER TO,

MASTER_HOST=‘192.168.3.128’,

MASTER_USER=‘root’,

MASTER_PASSWORD=‘123456’,

MASTER_LOG_FILE=‘mysql-bin.000001’,

MASTER_LOG_POS=5415;

启动slave同步线程

start slave

  1. 查看连接状态

show slave status

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LYCQGLl2-1606142917234)(F544D66A70CC4A558B9961FA82E39A9A)]

注意要这两个选项都是YES才标识主从正常启动

SLAVE_IO_Running表示和主数据库的IO通信,Slave_SQL_Running表示从数据库的slave sql线程是否工作正常

当两者出问题时,要查看err日志对症下药

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值