Mysql的主从配置

文章详细介绍了如何在Docker容器中设置MySQL服务器的主从复制。首先,通过拷贝my.cnf文件并创建相应的目录结构来准备配置。接着,停止并删除旧的MySQL容器,创建新的运行容器,并指定数据和配置文件路径。然后,修改my.cnf文件以适应从服务器需求。最后,在主服务器上创建复制账户,获取主服务器状态和IP,配置从服务器并启动复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 拷贝my.cnf文件

将容器内配置文件的位置/etc/my.cnf,拷贝到centos下

查看容器内的配置文件所在位置

在/etc/localhost/software创建mysql文件,并在mysql下创建文件夹3306和3310,并在两个文件夹下创建conf和data文件夹,并将my.cnf文件拷贝到conf文件夹下

先跳转到conf文件夹下,然后输入命令docker cp mysql_3306:/etc/my.cnf ./

2.删除之前的mysql docker

  1. docker stop mysql_3306
  2. docker rm mysql_3306
  3. docker ps -a

 3.创建运行容器

docker run -itd --name mysql_3306 --privileged=true -p 3306:3306 -v /usr/local/software/mysql/3306/data:/var/lib/mysql -v /usr/local/software/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/software/mysql/3306/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123 mysql

4.修改conf文件夹下的my.cnf文件 

3306

# 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
default_authentication_plugin=mysql_native_password

# Custom config should go here
!includedir /etc/mysql/conf.d/

server-id=200
log-bin=mysql-bin
binlog_format=row

3310

# 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
default_authentication_plugin=mysql_native_password

# Custom config should go here
!includedir /etc/mysql/conf.d/

server-id=201
log-bin=mysql-slave-01-bin
relay_log=edu-mysql-relay-bin
read-only=1

5.master中创建slave账号操作从服务

进入mysql_3306容器:docker exec -it mysql_3306 bash

登录mysql:mysql -uroot -p123

 创建主从账号”slave”: 

  • create user 'slave'@'%' identified by '123';
  • grant replication slave, replication client on *.* to 'slave'@'%';
  • alter user 'slave'@'%' identified with mysql_native_password by '123';
  • flush privileges;

获取master容器的状态:

show master status;

 获取mysql_master的ip地址

 docker inspect mysql_3306 |grep IPA

 mysql从服务器

修改从配置文件(前面已改过,就是3310下的my.cnf文件)

 和上述一样,在对应的文件夹下创建3310的容器,然后进入mysql配置主从

change master to master_host='172.17.0.2',master_user='slave',master_password='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=157;

开启主从:start slave

 查看结果:mysql> show slave status \G;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值