docker中mysql主从配置实践

前言

常言道:有备无患。没有备份早晚会出事得。

直接在主库进行备份,会影响性能,同时也存在不安全。例如高峰时读写,此时备份会造成很大风险。

因此通常需要开启主从功能,在从库进行备份操作。

问题

mysql主从配置的实践。

本地环境是mac ,
访问mysql工具使用了sequel pro
docker版本version 17.12.0-ce-mac49 (21995)

解决过程

  • 创建目录
    依次如下图目录
    这里我的目录是 mysql-backup
tree
.
├── mysql-master
│   └── mysql.conf.d
│       └── mysqld.cnf
└── mysql-slave
    └── mysql.conf.d
        └── mysqld.cnf
  • 创建主库
docker run --name mysql-master  -p 3309:3306  -v $(pwd)/mysql.conf.d:/etc/mysql/mysql.conf.d  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest

其中主库的配置文件

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

#
# 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
#log-error  = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-bin=mysql-bin
server-id=1
  • 创建从库
# Copyright (c) 2014, 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

#
# 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
#log-error  = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-bin=mysql-bin
server-id=2
  • 配置账号-在主库执行。
    创建账号
CREATE USER 'repl'@'%' IDENTIFIED BY 'doudouchidou123456'

分配权限

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'

查看是否开启Bin日志

show VARIABLES  LIKE '%log_%';
image.png
SHOW BINARY LOGS;
image.png

显示日志位置

show master status
image.png

锁定读写。(非常重要。该操作确保主库暂时不再接受读写)

FLUSH TABLES WITH READ LOCK;
  • 从库配置

查看id

show variables like 'server_id'
image.png

如果id相同则需要修改从库

set global server_id=2

配置访问账号

CHANGE MASTER TO
MASTER_HOST='172.17.0.6',
   MASTER_USER='repl',
   MASTER_PASSWORD='doudouchidou123456',
    MASTER_LOG_FILE='mysql-bin.000003',
    MASTER_LOG_POS=2678

启动

start slave

查看运行状态

show slave status
image.png
  • 其他更多命令
停止
stop slave
重置
reset slave
查看容器ip
docker inspect mysql-master
查看运行日志
docker logs mysql-master
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 《老男孩Linux运维》笔记MySQL-Documentation 概述 MySQL介绍 MySQL属于传统关系型...
    Zhang21阅读 4,585评论 0 9
  • https://www.cnblogs.com/along21/p/8011596.html https://bl...
    SkTj阅读 8,317评论 1 4
  • MySQL运维实践 5.1-MySQL日志系统 什么是日志 日志(log)是一种顺序记录事件流水的文件 记录计算机...
    极客圈阅读 5,319评论 1 11
  • 一、MySQL优化 MySQL优化从哪些方面入手: (1)存储层(数据) 构建良好的数据结构。可以大大的提升我们S...
    宠辱不惊丶岁月静好阅读 7,276评论 1 8
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 12,343评论 5 116