搭建Mysql主从复制

Posted java王文龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Mysql主从复制相关的知识,希望对你有一定的参考价值。

点击上方蓝色字体,关注公众号
目录

01:linux中安装docker

02:使用docker安装mysql

03:可视化工具连接mysql

04:主机中分配用户

05:开启binlog

06:配置从机

07:创建数据库,测试主从复制



01
linux中安装docker


首先说明一点,我在linux中安装docker是为了更好的安装其他应用程序,如mysql、nginx,简单方便,当然小伙伴儿们也可以手动一步步安装应用程序,linux中安装docker命令如下:

[root@localhost mysql]# yum -y install docker
查看版本,出现下面内容说明安装成功
[root@localhost mysql]# docker -vDocker version 1.13.1, build 0be3e21/1.13.1
启动docker
[root@localhost mysql]# service docker start

02
使用docker安装mysql

本次通过docker在linux中安装两个mysql,一个做主机,另一个做从机,通过后面的配置达到主从复制的效果

  • 安装第一个mysql


[root@localhost /]# docker run --name mysql1 -p 33061:3306 -e MYSQL_ROOT_PASSWORD=root1234 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
--name :表示创建的容器的名字
-d :表示容器在后台运行
-p : 表示将容器的 3306端口映射到宿主机的 33061 端口
MYSQL_ROOT_PASSWORD :设置密码
--character-set-server : 设置数据库字符集
--collation-server :排序规则
mysql:5.7 指定安装mysql的版本,本次就以安装5.7版本为例
docker run 命令:容器创建+启动的命令,如果不想启动,可以用docker create 命令创建所需容器,即使创建不启动,后面还需手动再启动一遍,所以我这里直接用了 docker run 命令一步操作
  • 安装第二个mysql

跟上面操作一致,只要端口号和名称不一致即可,其他看小伙伴们自己意愿可自行修改
[root@localhost /]# docker run --name mysql2 -p 33062:3306 -e MYSQL_ROOT_PASSWORD=root1234 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

03
可视化工具连接mysql

我的两个mysql都装在了同一个Linux系统中,所以两个mysql的主机地址是一样的,唯一不一样的是端口号
                                                                                                                         

搭建Mysql主从复制

04
主机分配用户

上面我创建了两个mysql,一个是mysql1,另一个是mysql2,那么我就把mysql1作为主机,mysql2作为从机,首先给主机重新分配一个用户,目的是为从机登录主机读取数据用的,操作如下:



Navicat中输入命令:
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'root1234';
*.* : 表示任意库中的任意表
repl : 用户的名字,小伙伴儿们可以起其他名字,这里用repl为例
% 表示可以用任意位置登录,当然也可以写固定的ip地址
IDENTIFIED BY : 设置登录密码
效果如下:
搭建Mysql主从复制
点击用户可看到刚分配的用户效果如下

搭建Mysql主从复制


05

开启binlog

注:只有开启了binlog之后,从机才能读取都主机中数据的变化
  • 进入 mysql1容器

[root@localhost /]# docker exec -it mysql1 /bin/bash
我docker中没有装vi编辑器(当然如果不嫌麻烦的话,也可以装一个,不过我觉得没什么必要),所以我从docker退出去后,新建一个同名的文件,把配置复制到新创建的文件,修改完配置后再放到docker中覆盖即可,要想开启binlog就得修改配置文件。

  • 查看原配置:


root@125dfce1b700:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf
  • 配置如下:


# Copyright (c) 2014, 2016, 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, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## 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, version 2.0, 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.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysql#log-error = /var/log/mysql/error.log# By default we only accept connections from localhost#bind-address = 127.0.0.1# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0
  • 退出容器

[root@localhost /]# exit


在自己操作系统中创建同名文件,修改后的配置如下:
[root@localhost /]# vi mysqld.cnf
注:名字一定不要错
opyright (c) 2014, 2016, 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, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## 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, version 2.0, 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]
log-bin=/var/lib/mysql/binlogserver-id=1binlog-do-db=master_slave_database

pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysql#log-error = /var/log/mysql/error.log# By default we only accept connections from localhost#bind-address = 127.0.0.1# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0
注: 主要添加了三行配置,上面的配置文件第31~33行
log-bin:binlog的位置,这个位置要确保有操作权限
server-id: 给主机设置id
binlog-do-db:设置哪些库可以被复制
  • 通过docker命令将修改好的配置拷贝到下面的文件夹中

[root@localhost /]# docker cp ./mysqld.cnf mysql1:/etc/mysql/mysql.conf.d/
  • 重启mysql1

[root@localhost /]# docker restart mysql1
  • 主机中创建数据库master_slave_database(主机中配置的是哪个数据库就建哪个数据库)

  • 可视化工具(Navicat)输入命令出现下面效果说明开启binlog成功


show master status;

搭建Mysql主从复制

注:file、position、binlog_do_db的内容记住,待会配置从机的时候要用到

  • 配置从机mysql2 

步骤跟配mysql1的步骤是一样的,新建文件,修改配置,然后覆盖原文件即可,从机只是配置内容变了(只配一个server-id=2即可),配置如下

[mysqld]server-id=2
pid-file = /var/run/mysqld/mysqld.pidsocket = /var/run/mysqld/mysqld.sockdatadir = /var/lib/mysql#log-error = /var/log/mysql/error.log# By default we only accept connections from localhost#bind-address = 127.0.0.1# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0
  • 重启mysql2

[root@localhost /]# docker restart mysql2

06

配置从机

  • 进入mysql2容器

[root@localhost /]# docker exec -it mysql2 /bin/bash
  • 登录

root@490fac23b15d:/# mysql -u root -p Enter password: 输入自己设置的mysql密码,然后回车即可,会出现下面内容Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
  • 开启数据同步【就是指定从机的master是谁】

mysql> change master to master_host='10.20.60.130',master_port=33061,master_user='rep1',master_password='root1234',master_log_file='binlog.000002',master_log_pos=735;
//返回这条数据,说明成功了:Query OK, 0 rows affected, 2 warnings (0.03 sec)
  • 查看状态

mysql> show slave status\G;

搭建Mysql主从复制

上面两行都是yes才算成功,如果不成功请查看报错信息,如上图

 Last_IO_Error: error connecting to master 'rep1@10.20.60.130:33061' - retry-time: 60 retries: 969
  • 停止从机

mysql> stop slave
  • linux打开新窗口【重点:打开新窗口】,查看主机的docker的ip。这样当前页面就不用重复退出mysql及docker容器再重新进入了

[root@localhost /]# docker inspect mysql1

回车后拉到最下面,IPADDress就是docker容器的ip

搭建Mysql主从复制


  • 回到刚才的页面重新开启数据同步

mysql> change master to master_host='172.17.0.1',master_port=3306,master_user='rep1',master_password='root1234',master_log_file='binlog.000002',master_log_pos=735;
  • 启动从机,并且查看状态

mysql> start slave;Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;

此时可以看到,Slave_IO_Running: Yes,Slave_SQL_Running: Yes,说明已经成功了

07

创建数据库,测试主从复制

从机中创建 master_slave_database 数据库(跟主机保持一致),创建成功后在主机的master_slave_database 库中创建一个user表,刷新从机,此时会看到从机中master_slave_database 库里也出现了user表,说明主从复制配置成功,效果如下

搭建Mysql主从复制

主机中添加一条数据,看是否能从从机中查询到(观察下面两图的mysql连接是不一样的)

搭建Mysql主从复制


能查到结果,说明配置成功了,至此结束,关注下方公众号获取更多内容!


以上是关于搭建Mysql主从复制的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制的简单搭建

MySQL主从复制的简单搭建

MySQL主从复制与读写分离

mysql 主从复制搭建

MySQL 主从搭建

mysql主从复制架构搭建以及主从复制模式