Mysql主从复制

发布于:2024-12-21 ⋅ 阅读:(39) ⋅ 点赞:(0)

首先启动虚拟机

打开终端要进入root账号,然后输入cd ~,在root目录下建立两个文件夹

然后先进入mysql1,创建镜像,如果很慢可以连自己的手机热点

-p 3308:3306 \
--name=my_mysql1 \
-v /root/mysql11/conf:/etc/mysql/conf.d \x
-v /root/mysql11/logs:/logs \
-v /root/mysql11/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7

然后先进入mysql2,创建镜像

docker run -id \
-p 3309:3306 \
--name=my_mysql2 \
-v /root/mysql2/conf:/etc/mysql/conf.d \
-v /root/mysql2/logs:/logs \
-v /root/mysql2/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7

然后输入docker ps查看镜像是否创建好,创建好之后再次进入mysql1给他创建一个配置文件

在里面先按下i键进入输入模式,然后把下面这一段放进去

# 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-bin=mysql-bin     #[必须]启用二进制日志
server-id=100         #[必须]服务器唯一ID

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

按下Esc退出编辑模式,然后ctrl+:输入wq保存退出,在接着输入docker cp my.cnf my_mysql1:/etc/mysql/将文件复制到容器

然后输入docker restart mysql1 重启你的主库

然后输入docker exec -it my_mysql1 mysql -u root -p 进入mysql1容器,密码设置的是123456,输入密码的时候不会显示输完之后直接回车就行

然后输入CREATE USER 'xiaoming'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'xiaoming'@'%';
ALTER USER 'xiaoming'@'%' IDENTIFIED WITH 'mysql_native_password' BY '123456';
FLUSH PRIVILEGES;

接着使用show master status;查看主库

这样子就是对的,把这两个东西记下来

进入mysql2

创建my.cnf

跟上面一样按i进入编辑模式

# 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id=101         #[必须]服务器唯一ID

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

然后保存退出

将文件复制到容器docker cp my.cnf my_mysql2:/etc/mysql/,然后重启mysql2容器

进入mysql  docker exec -it my_mysql2 mysql -u root -p

master_host的内容是第一台虚拟机的ip地址,port端口号就是你之前创建容器的端口号

执行语句

CHANGE MASTER TO
  MASTER_HOST='192.168.138.15',
  MASTER_PORT=3308,
  MASTER_USER='xiaoming',
  MASTER_PASSWORD='123456',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=154;

执行语句
start slave;

查看从库状态
show slave status;

把这些东西复制到notepd++打开

如果是这样就说明配置成功,失败的话就把容器和文件夹删除重新来就好了


今日签到

点亮在社区的每一天
去签到