master:1.1.1.73
slave:1.1.1.74
master和slave新建目录:
因为数据目录挂载盘在data,所以在data下新建master和slaver的映射配置文件、数据目录
master:
/data/software/mysql8/master/config(配置文件)
/data/software/mysql8/master/data(数据目录)
/data/software/mysql8/master/logs(日志)
slave:
/data/software/mysql8/slave/config
/data/software/mysql8/slave/data
/data/software/mysql8/slave/logs
master:新建
vim /data/software/mysql8/master/config/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
# MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
[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=1 #从库ID必须不一致
log-bin=mysql-bin
binlog-do-db=cdh_db #要同步的数据库
slave:新建
vim /data/software/mysql8/slave/config/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
# MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
[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=2 #从库ID必须不一致
log-bin=mysql-bin
binlog-do-db=cdh_db #要同步的数据库
master:
docker run --restart=always -d -v /data/software/mysql8/master/config/my.cnf:/etc/mysql/my.cnf -v /data/software/mysql8/master/logs:/logs -v /data/software/mysql8/master/data/mysql:/var/lib/mysql -p 3306:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=root mysql:8.0.23
slave:
docker run --restart=always -d -v /data/software/mysql8/slave/config/my.cnf:/etc/mysql/my.cnf -v /data/software/mysql8/slave/logs:/logs -v /data/software/mysql8/slave/data/mysql:/var/lib/mysql -p 3306:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=root mysql:8.0.23
master:
docker exec -it mysql_master /bin/sh
进入数据库:mysql -uroot -p
create user 'slave'@'1.1.1.74'IDENTIFIED WITH mysql_native_password BY 'xxxxxxxx';
grant replication slave on *.* to 'slave'@'1.1.1.74';
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 402 | cdh_db | | |
slave:
docker exec -it mysql_master /bin/sh
进入数据库:mysql -uroot -p
change master to master_host='1.1.1.73',master_user='slave',master_password='xxxxxxxxx',master_log_file='mysql-bin.000003 ',master_log_pos=402;
stop slave;
reset slave;
start slave;
验证 在要主从复制的数据库里面创建一条数据,从数据库的表则会自动创建。