docker安装运行单实例的MySQL参考另一篇文档 http://www.cnblogs.com/manger/p/7611309.html

1、首先在/data/script下创建两个文件my-m.cnf(主库配置) 、my-s.cnf(从库配置)
my-m.cnf 内容如下

[client]
port =
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice =
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port =
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp
log-bin = mysql-bin
server-id =
# 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=
# * 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/

my-m.cnf

主要是这两行,只需要在原来的配置里面加上就行
log-bin = mysql-bin
server-id = 1

my-s.cnf内容如下

[client]
port =
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice =
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port =
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp
log-bin = mysql-bin
server-id =
# 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=
# * 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/

my-s.cnf

log-bin = mysql-bin
server-id = 2

2、OK,有了配置文件,就可以启动mysql了,先启动主库
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master -v /data/script/my-m.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql

3、启动从库
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave -v /data/script/my-s.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql

4、连接主库,并运行以下命令,创建一个用户用来同步数据

想要访问docker的mysql数据库,我的机器上需要装一个mysql-client。
yum install mysql -y

mysql -h192.168.1.20 -P3307 -uroot -p123456
mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by 'repl';

5、查看主库状态
mysql> show master status;

mysql> show master status;

记住File、Position的值,如果没查到数据,请检查第一、第二步,配置问题。
我查出来的是mysql-bin.000003、940

6、连接到从库,运行以下命令,设置主库链接

mysql -h192.168.1.20 -P3308 -uroot -p123456
mysql> change master to master_host='192.168.1.20',master_user='repl',master_password='repl',
master_log_file='mysql-bin.000003',master_log_pos=940,master_port=3307;

7、启动同步
mysql> start slave;

8、查看同步状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.20
                  Master_User: repl
                  Master_Port: 3307
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 940
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 940
              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: 42aa1711-a46e-11e7-945d-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)
如果看到2个yes,说明主从搭建成功

docker mysql 主从配置的更多相关文章

  1. Docker Mysql主从同步配置搭建

    Docker Mysql主从同步配置搭建 建立目录 在虚拟机中建立目录,例如路径/home/mysql/master/data,目录结构如下: Linux中 新建文件夹命令:mkdir 文件夹名 返回 ...

  2. Mysql主从配置实战

    实战mysql主从配置 准备两个docker容器,分别在3306和3307开启两个mysql为主从数据库 可执行以下命令 docker run -p 3306:3306 --name mysql330 ...

  3. linux中MySQL主从配置(Django实现主从读写分离)

    一 linux中MySQL主从配置原理(主从分离,主从同步) mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到sl ...

  4. MySQL主从配置及haproxy和keepalived搭建

    本篇文章主要介绍如何搭建MySQL主主配置.主从配置.haproxy.keepalived,已经搭建过程中的一些问题.本次运行环境是在docker中,也会介绍一些docker的知识 docker 小知 ...

  5. mysql主从配置

    引言: 双11,阿里云服务器打折,于是我忍不住又买了一台服务器,于是咱也是有两台服务器的爷们了,既然有了两台服务器,那么肯定要好好利用一下吧,那么就来玩玩mysql的主从配置吧. 准备 两台数据库服务 ...

  6. Mysql主从配置,实现读写分离

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  7. CentOS 7下的 Mysql 主从配置

    最近在玩mysql主从配置,在此记录一下 一.前言 1.安装两个虚拟机(CentOS 7).iP分别是192.168.47.131 和192.168.47.133.其中192.168.47.133作为 ...

  8. Mysql主从配置+读写分离

    Mysql主从配置+读写分离     MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. ...

  9. mysql主从配置(清晰的思路)

    mysql主从配置.鄙人是在如下环境测试的: 主数据库所在的操作系统:win7 主数据库的版本:5.0 主数据库的ip地址:192.168.1.111 从数据库所在的操作系统:linux 从数据的版本 ...

随机推荐

  1. 自己编写的str操作函数

    1.strcat() 此函数原型为 char *strcat(char *dest, const char *src). 功能为连接两个字符串,把src连接到dest后面:返回dest地址 实现如下 ...

  2. C++小项目-吃豆子游戏

    GMap.h #pragma once //保证头文件只被编译一次 #include "stdafx.h" #define MAP_LEN 19 //逻辑地图大小 (逻辑地图由行. ...

  3. 二分图最大匹配(匈牙利算法) UVA 10080 Gopher II

    题目传送门 /* 匈牙利算法:这题比UVA_670简单,注意是要被吃的鼠的最少个数,套模板 */ #include <cstdio> #include <algorithm> ...

  4. MyEclipse日志文件目录

    MyEclipse存放日志的目录为: <Workspace_Root>/.metadata/.log 在该目录下存放了日志的存档和当前日志,通过该日志可以查看MyEclipse的错误信息.

  5. SQL 语句在存储过程执行和在SSMS中执行的差异

    SQL 语句在存储过程执行和在SSMS中执行的差异 SSMS是SQlSerever management studio.本文所述情形在SQLServer2008中测试. 有时发现同样几条语句,在SSM ...

  6. 防止按钮button重复提交,点击后失效,10秒后恢复

    <script type="text/javascript"> $(function () {//页面完全加载完后执行 /*防止重复提交 10秒后恢复*/ var is ...

  7. FusionCharts之我用

    fusioncharts:(此控件需flash支持) 介绍:http://baike.baidu.com/link?url=SOheR7sQlb93S5TqYmeI7FhtJ0V9ABNwH6OsAa ...

  8. HDU_2955_Robberies_01背包

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  9. jmeter接口测试小结

    摘自:http://www.cnblogs.com/houzhizhe/p/6839736.html JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需 ...

  10. os下,vs code 自动编译ts

    nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...