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.. 什么的就成功了,你现在在主库上的修改,都会同步到从库上
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
下面将详细介绍如何在Docker中设置MySQL主从配置。 1. **创建配置文件** 在开始配置之前,我们需要为主库和从库创建两个不同的配置文件,例如`my-m.cnf`和`my-s.cnf`。这两个文件中需要包含MySQL的基本设置以及...
Docker 安装 配置阿里云镜像加速器 Docker 安装 MySQL5.7.30 Docker安装MySQL主从配置
NULL 博文链接:https://crabdave.iteye.com/blog/2329333
关于博客: https://blog.csdn.net/qq_42413011/article/details/126914080 的搭建脚本
docker_compose搭建mysql主从复制
在Docker中配置MySQL主从复制是一个常见的任务,特别是在分布式系统和高可用性环境中。这里我们将详细探讨如何在Docker容器中设置MySQL主从架构。 首先,我们需要理解MySQL主从复制的基本概念。在MySQL主从复制中,...
docker 安装mysql主从
MySQL 主从同步配置过程 一、 MySQL 主从同步配置概述 MySQL 主从同步配置是指将 MySQL 数据库的数据从一台服务器(主服务器)同步到另一台服务器(从服务器)的过程。这种配置可以实现数据的高可用性和灾难恢复,...
Win7 Docker搭建主从复制,包括配置文件和安装包
"基于docker部署主从MYSQL数据库.pdf" 本资源摘要信息主要介绍了基于docker部署主从MYSQL数据库的技术实现和使用场景,以及其在实际应用中的优点和缺陷。 首先,作者介绍了当前时代的科学技术和网络的发展,导致...
本压缩文件包含两个pdf文件,详细讲解了如何在Linux系统中安装Docker、如何在Docker中安装和配置MySQL主数据库、如何在Docker中安装和配置MySQL从数据库以及如何验证主从数据库搭建正常,视频教程:...
Step3: 查看master status, 记住File、Position的值,如果没查到数据,请检查第一、第二步,配置问题。 我查出来的是mysql-bin.000004、312 Step4: 连接slave, 运行以下命令连接master Step5: 启动slave Step6: 查看...
通过上述步骤,您已经成功地在Docker环境中搭建了一个MySQL主从复制系统。这种架构不仅能够有效提高数据的安全性和可靠性,还能够提升系统的整体性能。此外,利用Docker进行部署,使得整个过程更加便捷高效。未来,...
以下是在Docker中配置MySQL主从复制的详细步骤: 1. **创建主服务器所需目录和配置**: - 创建目录 `/usr/local/mysqlData/master/cnfm` 和 `/usr/local/mysqlData/master/data`,分别用于存放配置文件和数据库...
使用allen老师的hub镜像制作自己的mysql主从 --------- docker pull xiaochunping/mysql-master; docker pull xiaochunping/mysql-slave; -- cnetos docker run ,把my.cnf和data数据引入到属主机 -----------------...
下面,将围绕Docker实现MySQL主从复制实践展开详细知识点的总结。 知识点一:Docker技术 Docker是一种轻量级的虚拟化技术,它允许开发者打包应用以及应用的依赖包到一个可移植的容器中,然后发布到任何支持Docker的...
linux系统:docker搭建mysql主从服务器!
【MySQL主从配置】 MySQL主从配置是一种高可用性和数据复制的方法,确保数据在多个服务器之间保持同步。在本文档中,我们了解到如何在Docker环境下进行MySQL的主从配置。 1. **创建MySQL容器** - 使用Docker运行...