文档章节

docker配置mysql主从实现读写分离

Vinku
 Vinku
发布于 2018/09/12 11:57
字数 810
阅读 98
收藏 0

1.获取mysql镜像

docker pull mysql:5.7

2.创建工作目录

cd ~
mkdir docker-mysql-config

3.创建主从mysql配置文件

  • 创建主mysql配置文件
vim ~/docker-mysql-config/master.cnf

复制如下配置:

# Copyright (c) 2014, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin 
server-id = 1 

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
  • 创建从mysql配置文件
vim ~/docker-mysql-config/slave01.cnf

复制主mysql配置文件,注意: 
将server-id = 1 修改为server-id = 2

4.运行主myqsl容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-master -v ~/docker-mysql-config/master.cnf:/etc/mysql/my.cnf -p 3306:3306 mysql:5.7

5.运行从mysql容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-slave01 -v ~/docker-mysql-config/slave01.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql:5.7

6.主从服务配置

  • 终端1:主mysql容器
docker exec -it mysql-master bash
mysql -uroot -p123

mysql> 
  • 终端2:从mysql容器
docker exec -it mysql-slave01 bash
mysql -uroot -p123

mysql> 
  • 主mysql容器配置
mysql> grant replication slave on *.* to 'slave01'@'%' identified by 'slave01';

mysql> flush privileges;

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      1160 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 从mysql容器配置
mysql> change master to master_host='192.168.99.100',
    master_user='slave01',
    master_password='slave01',
    master_log_file='mysql-bin.000004',
    master_log_pos=0,         ## 从master什么记录位置开始读取,从0开始读取可以复制master整个库表
    master_port=3306;

mysql> start slave;

mysql> show slave status \G

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1160
               Relay_Log_File: 3dc37943fb7a-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1160
              Relay_Log_Space: 534
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 39f6cd9a-f870-11e7-b14e-0242ac110002
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

只要一下两个为Yes,代表配置完成

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

若Slave_SQL_Running为No,表示执行SQL出错,因为slave数据库没有mater数据库的库和表结构,需要把master和slave同步一下后,执行一下命令

mysql> stop slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;    #客户端运行,用来跳过几个事件,只有当同步进程出现错误而停止的时候才可以执行。
mysql> start slave ;

 

本文转载自:https://blog.csdn.net/lynnyq/article/details/79055000

Vinku
粉丝 0
博文 13
码字总数 1901
作品 0
东莞
私信 提问
Mysql读写分离——主从数据库+Atlas

mysql集群 最近在参加项目开发微信小程序后台,由于用户数量巨大,且后台程序并不是很完美,所以对用户的体验很是不友好(简单说就是很卡)。赶巧最近正在翻阅《大型网站系统与Java中间件实践...

NinWoo
2018/09/11
0
0
MySQL 读写分离 (基于Amoeba)

一.MySQL读写分离 主数据库处理事务性查询,从数据库处理select查询。数据库复制用来把事务性查询导致的变更同步到从数据库中。 二.最为常见的读写分离有两种: 1.基于程序代码实现 在代码中...

郭晓辉gg
2017/05/06
0
0
Mysql主从复制和读写分离方案分析

引子 最近在研究Web服务端负载均衡方面的技术,参考网上资料,总体思路可以分为如下几类: 1.应用服务器集群,典型的代表就是Nginx+Tomcat实现负载均衡; 2.数据库集群。 本文主要关注数据库...

蓝狐乐队
2015/03/24
0
1
mysql5.7的主从复制+读写分离

Mysql主从复制与读写分离 目录: 1、mysql主从复制工作原理 2、mysql读写分离原理,使用Amoeba中间代理层实现读写分离 3、在vmware中实现mysql主从复制+读写分离 一、Mysql主从复制的工作原理...

咖啡猫Mr
2017/05/25
0
0
MySQL5.6主从复制及读写分离的实现

MySQL5.6主从复制及读写分离的实现 MySQL 5.6 基于GTID的主从复制及使用amoeba配置实现读写分离 一、amoeba 简介 Amoeba(变形虫)项目,该开源框架于2008年开始发布一款 Amoeba forMysql软件。...

bdstravel
2015/01/27
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周三乱弹 —— 公司俩美女离职了

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @无形的肖申克 :分享広橋真紀子的单曲《時には昔の話を》: 《時には昔の話を》- 広橋真紀子 手机党少年们想听歌,请使劲儿戳(这里) @小橙子...

小小编辑
39分钟前
522
11
内存池和tcmalloc的性能比较

最近在测试tcmalloc性能的时候发现了一个现象!!!!就是new似乎很消耗性能!!直接上代码! #include "time.h" #include <gperftools/tcmalloc.h> using namespace std; #define MAX_SIZE......

shzwork
今天
5
0
Protocol Buffers 简介

文档编辑和持续集成状态: 本文档的 Protocol Buffer 的中文文档使用的是 Asciidoctor 进行编排的 http://docs.ossez.com/protocol-buffers-docs/index.html(本 WIKI 中的内容将会与在线发布...

honeymoose
今天
6
0
uniapp + bootstrapvue 移动/PC 一套搞定 (一)配置bootstrapvue

1.准备文件 自己到DCloud官网: http://dcloud.io/ 去下载官方的IDE Hbuilder,新建一个空的uniapp项目即可。 uniapp框架自带优化的vue,我们仅仅需要准备以下三个文件: bootstrap.min.css ...

panyunxing
今天
15
0
Android Camera原理之camera service类与接口关系

camera service主要是指 frameworks/av/services/camera/下面的代码,最近在看这一块的代码,为了更好地理清这一块的代码,也为了后续学习camera方便一些,我觉得很有必要理一下这一块的整体...

天王盖地虎626
今天
11
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部