Docker mysql 主从配置详解及实例

 更新时间:2016年11月23日 10:09:15   作者:来自地球的外星人  
这篇文章主要介绍了Docker mysql 主从配置详解及实例的相关资料,需要的朋友可以参考下

Docker mysql 主从配置

1、首先创建两个文件my-m.cnf(主库配置) 、my-s.cnf(从库配置)

my-m.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/

主要是这两行,只需要在原来的配置里面加上就行

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

my-s.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 = 2

# 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/

同样,主要的是这两行

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

2、OK,有了配置文件,就可以启动MySQL了,先启动主库

$ docker run -d -e MYSQL_ROOT_PASSWORD=admin --name mysql-master -v /soft/my-m.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql

3、启动从库

$ docker run -d -e MYSQL_ROOT_PASSWORD=admin --name mysql-slave -v /soft/my-s.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql

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

$ GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';

5、查看主库状态

$ show master status;

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

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

$ change master to master_host='121.32.32.54',master_user='backup',master_password='123456',
master_log_file='mysql-bin.000004',master_log_pos=312,master_port=3307;

7、启动同步

$ start slave;

8、查看同步状态

$ show slave status

如果看到Waiting for master send event.. 什么的就成功了,你现在在主库上的修改,都会同步到从库上

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • RHEL6.5编译安装MySQL5.6.26教程

    RHEL6.5编译安装MySQL5.6.26教程

    这篇文章主要介绍了RHEL6.5编译安装MySQL5.6.26教程的相关资料,需要的朋友可以参考下
    2015-10-10
  • windows7下启动mysql服务出现服务名无效的原因及解决方法

    windows7下启动mysql服务出现服务名无效的原因及解决方法

    这篇文章主要介绍了windows7下启动mysql服务出现服务名无效的原因及解决方法,需要的朋友可以参考下
    2014-06-06
  • MySQL limit使用方法以及超大分页问题解决

    MySQL limit使用方法以及超大分页问题解决

    这篇文章主要给大家介绍了关于MySQL limit使用方法以及超大分页问题解决的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MySQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-10-10
  • MySQL InnoDB中的锁机制深入讲解

    MySQL InnoDB中的锁机制深入讲解

    这篇文章主要给大家介绍了关于MySQL InnoDB中锁机制的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用MySQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • mysql对于模糊查询like的一些汇总

    mysql对于模糊查询like的一些汇总

    这篇文章主要给大家介绍了关于mysql对于模糊查询like的一些汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • MySQL进阶SELECT语法篇

    MySQL进阶SELECT语法篇

    从这个基本语法可以看出,最简单的SELECT语句是SELECT select_list,实际上利用这个最简单的SELECT语句,你也可以完成许多你期待的功能,首先你能利用它进行MySQL所支持的任何运算,例如:SELECT 1+1,它将返回2;其次,你也能利用它给变量赋值,而在PHP中,运用SELECT语句的这种功能,你就可以自由地运用MySQL的函数为PHP程序进行各种运算,并赋值给变量。在很多的时候,你会发现MySQL拥有许多比PHP更为功能强大的函数。
    2008-04-04
  • MySQL 4种常用的主从复制架构

    MySQL 4种常用的主从复制架构

    这篇文章主要介绍了MySQL 4种常用的主从复制架构,帮助大家更好的理解和学习MySQL,感兴趣的朋友可以了解下
    2020-08-08
  • 详解Centos7 修改mysql指定用户的密码

    详解Centos7 修改mysql指定用户的密码

    本篇文章主要介绍了Centos7 修改mysql指定用户的密码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2016-12-12
  • 通过mysql show processlist 命令检查mysql锁的方法

    通过mysql show processlist 命令检查mysql锁的方法

    show processlist 命令非常实用,有时候mysql经常跑到50%以上或更多,就需要用这个命令看哪个sql语句占用资源比较多,就知道哪个网站的程序问题了。
    2010-03-03
  • MySQL开启记录执行过的SQL语句方法

    MySQL开启记录执行过的SQL语句方法

    这篇文章主要介绍了MySQL开启记录执行过的SQL语句方法,配置的方法很简单,本文直接给出配置示例,需要的朋友可以参考下
    2015-07-07

最新评论