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.. 什么的就成功了,你现在在主库上的修改,都会同步到从库上
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- 这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
docker 启动elasticsearch镜像,挂载目录后报错的解决
这篇文章主要介绍了docker 启动 elasticsearch镜像,挂载目录后报错的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-20解决Docker中的error during connect异常情况
这篇文章主要介绍了解决Docker中的error during connect异常情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-22教你使用Portainer管理多台Docker容器环境的方法
这篇文章主要介绍了Portainer管理多台Docker容器环境,本文给大家介绍的非常详细,包括环境准备及管理docker的详细过程,需要的朋友可以参考下...2021-11-11- 这篇文章主要介绍了docker部署confluence的完整步骤,这里的镜像并不是小编自己写的是基于他人打包的文中有详细介绍,需要的朋友可以参考下...2021-06-11
- 这篇文章主要介绍了docker swarm外部验证负载均衡时不生效的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-27
- 这篇文章主要介绍了解决docker挂载的目录无法读写问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-19
- 这篇文章主要介绍了docker端口映射及外部无法访问问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-19
Docker 解决openjdk容器里无法使用JDK的jmap等命令问题
这篇文章主要介绍了Docker 解决openjdk容器里无法使用JDK的jmap等命令问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-04- 这篇文章主要为大家详细介绍了linux下源码安装mysql5.6.20教程的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-15
通过node-mysql搭建Windows+Node.js+MySQL环境的教程
这篇文章主要介绍了通过node-mysql搭建Windows+Node.js+MySQL环境的教程,node-mysql是JavaScript编写的一个Node的MySQL驱动,需要的朋友可以参考下...2016-03-03- 这篇文章主要介绍了一条命令重启所有已停止的docker容器操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-20
使用rpm安装指定版本docker(1.12.6)的详细步骤
为了防止安装高版本的docker引发的错误,需要安装1.12.6版本的docker,下面小编给大家带来了使用rpm安装指定版本的docker(1.12.6)的步骤,感兴趣的朋友一起看看吧...2021-08-11- 通过本文可以帮助大家快速学习Docker安装ElasticSearch的过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧...2021-08-31
- 本篇文章主要介绍了Mac下安装mysql5.7 完整步骤,具有一定的参考价值,有兴趣的可以了解一下,...2017-01-26
- 这篇文章主要介绍了在docker中执行linux shell命令的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-30
- 这篇文章主要为大家详细介绍了Linux CentOS MySQL数据库的安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-05-19
- 这篇文章主要给大家介绍了关于docker容器与宿主机的数据交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-09
安装Docker Desktop报错WSL 2 installation is incomplete的问题(解决报错)
这篇文章主要介绍了安装Docker Desktop报错WSL 2 installation is incomplete的问题,解决方法很简单只需我们自己手动更新一下,我们根据提示去微软官网下载最新版的wsl2安装后即可正常打开,需要的朋友可以参考下...2021-06-13- 这篇文章主要介绍了在windows10上安装mysql详细图文教程,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看看吧...2016-09-18