使用docker安装mysql8.0主从

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;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jockerzoo@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值