准备两台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、验证
在要主从复制的数据库里面创建一条数据,从数据库的表则会自动创建。