【docker】docker安装的mysql8.0主从复制搭建

 准备两台docker安装的数据库

主:10.204.212.75

从:10.204.212.148

1、配置主数据库

使用docker进入容器
docker exec -it 容器id bin/bash

进入/etc/mysql/

vi my.conf

 如果vi命令没发现,在centos的docker环境下,命令下面两个命令:

apt-get update   #更新软件列表
apt-get install vim  #下载

编辑my.conf 内容如下:


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

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/
# lower_case_table_names=1
#
server-id=100
log-bin=mysql-bin
binlog-do-db=pay_db #要同步的数据库

命令登录主数据库

mysql -u root -p

提示输入数据库root账号的密码

创建从数据需要登录主数据的账号和密码,并给账号授权复制

// 10.204.212.148 从数据库ip
create user 'slave'@'10.204.212.148' IDENTIFIED WITH mysql_native_password BY 'rjhslave';
grant replication slave on *.* to 'slave'@'10.204.212.148';

 查看主数据库状态

show master status;

 2、配置从数据库

同样进入docker容器内,编辑my.conf

 从数据连接主数据

记得master_log_file='master-bin.000003',master_log_pos=390;对应主数据库查询状态命令查询出来的数据

 change master to master_host='10.204.212.75',master_user='slave',master_password='rjhslave',master_log_file='master-bin.000003',master_log_pos=390;

停用重置启用从数据复制内容

stop slave;
reset slave;
start slave;

3、验证

在要主从复制的数据库里面创建一条数据,从数据库的表则会自动创建。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿来小同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值