首页 > 数据库 >rhel 7.3搭建mysql的主从复制—非单机场景

rhel 7.3搭建mysql的主从复制—非单机场景

时间:2023-05-26 21:01:30浏览次数:48  
标签:主从复制 Log 7.3 Source master rhel mysql Master SSL

文档课题:rhel 7.3搭建mysql的主从复制—非单机场景.
数据库:mysql 8.0.27
系统:rhel 7.3
安装包:mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
环境介绍:

rhel 7.3搭建mysql的主从复制—非单机场景_主从复制

1、理论知识
master将操作语句记录到binlog日志,然后授予slave远程连接权限(master需开启binlog,为数据安全考虑,slave也开启binlog).
slave开启IO线程和SQL线程,其中IO线程负责读取master的binlog到中继日志;SQL线程负责从中继日志读出binlog内容,并更新到slave的数据库里,如此保证slave和master数据一致性.
Mysql复制至少需要两个Mysql服务,当然Mysql服务可以分布在不同的服务器上,也可以分布在一台服务器上,此次测试Mysql在不同的服务器上.master和slave两节点间时间需同步.
2、开始搭建
2.1、安装mysql
准备两台安装好mysql 8.0.27的服务器.
2.2、主库配置
2.2.1、修改配置文件
--修改主节点配置文件
[root@leo-mysql-master etc]# vi /etc/my.cnf
添加如下内容:
log_bin=master_bin   --开启二进制
server_id=1          --设置当前节点全局唯一ID,后续在创建连接时需要该参数.
2.2.2、重启mysql服务
[root@leo-mysql-master etc]# service mysql stop
Shutting down MySQL. SUCCESS! 
[root@leo-mysql-master etc]# service mysql start
Starting MySQL. SUCCESS! 
[root@leo-mysql-master etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status \G
*************************** 1. row ***************************
             File: master_bin.000001
         Position: 957
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.01 sec)
2.2.3、创建复制用户
创建复制用户账号,192.168.133.%表示允许IP前边是192.168.133的repluser用户连接master主机.
mysql> create user repluser@'192.168.133.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
授权用户
mysql> grant replication slave, replication client on *.* to repluser@'192.168.133.%';
Query OK, 0 rows affected (0.00 sec)
2.3、从库配置
2.3.1、修改配置文件
[root@leo-mysql-slave etc]# cp /etc/my.cnf /etc/my_bak.cnf
[root@leo-mysql-slave etc]# vi /etc/my.cnf
添加如下:
server_id=2          --当前节点全局性ID号
log-bin=slave_log     --开启二进制日志
read_only=ON        --设置从库只读,对root用户无效
relay_log=relay-log    --relay log的文件路径
relay_log_index=relay-log.index  -默认值hostname-relay-bin.index
2.3.2、重启mysql服务
重启从库mysql服务.
[root@leo-mysql-slave etc]# service mysql stop
Shutting down MySQL.. SUCCESS! 
[root@leo-mysql-slave etc]# service mysql start
Starting MySQL. SUCCESS!
2.3.3、启动复制线程
从库使用有复制权限的用户连接至主库,并启动复制线程
[root@leo-mysql-slave etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to master_host='192.168.133.111',master_user='repluser',master_password='123456',master_log_file='master_bin.000001',MASTER_LOG_POS=957;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
2.3.4、查看状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.133.111
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master_bin.000001
          Read_Master_Log_Pos: 957
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 325
        Relay_Master_Log_File: master_bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 957
              Relay_Log_Space: 528
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 3393dd41-fb9f-11ed-a80f-000c291140b1
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 1
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                           |
+---------+------+-------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SHOW SLAVE STATUS' is deprecated and will be removed in a future release. Please use SHOW REPLICA STATUS instead |
+---------+------+-------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3、数据测试
3.1、建测试数据
现在主库上创建测试库booksDB,然后创建表books并插入记录.测试数据是否会同步到两个从库.
mysql> create database booksDB;
Query OK, 1 row affected (0.00 sec)

mysql> use booksDB;
Database changed
mysql> create table books
    -> (
    -> bk_id int not null primary key,
    -> bk_title varchar(50) not null,
    -> copyright year not null
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into books values 
    -> (11078,'Learning MYSQL',2010),
    -> (11033,'Study Html',2011),
    -> (11035,'How to use php',2003),
    -> (11072,'Teach yourself javascript',2005),
    -> (11028,'Learning C++',2005),
    -> (11069,'MYSQL professional',2009),
    -> (11026,'Guide to MySQL 5.7',2008),
    -> (11041,'Inside VC++',2011);
Query OK, 8 rows affected (0.01 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql> select * from books;
+-------+---------------------------+-----------+
| bk_id | bk_title                  | copyright |
+-------+---------------------------+-----------+
| 11026 | Guide to MySQL 5.7        |      2008 |
| 11028 | Learning C++              |      2005 |
| 11033 | Study Html                |      2011 |
| 11035 | How to use php            |      2003 |
| 11041 | Inside VC++               |      2011 |
| 11069 | MYSQL professional        |      2009 |
| 11072 | Teach yourself javascript |      2005 |
| 11078 | Learning MYSQL            |      2010 |
+-------+---------------------------+-----------+
8 rows in set (0.00 sec)
3.2、验证数据
在从库执行以下命令,查看数据是否已经同步.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| booksDB            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use booksDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_booksDB |
+-------------------+
| books             |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from books;
+-------+---------------------------+-----------+
| bk_id | bk_title                  | copyright |
+-------+---------------------------+-----------+
| 11026 | Guide to MySQL 5.7        |      2008 |
| 11028 | Learning C++              |      2005 |
| 11033 | Study Html                |      2011 |
| 11035 | How to use php            |      2003 |
| 11041 | Inside VC++               |      2011 |
| 11069 | MYSQL professional        |      2009 |
| 11072 | Teach yourself javascript |      2005 |
| 11078 | Learning MYSQL            |      2010 |
+-------+---------------------------+-----------+
8 rows in set (0.00 sec)
mysql> show replica status \G
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 192.168.133.111
                  Source_User: repluser
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: master_bin.000001
          Read_Source_Log_Pos: 1897
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 1265
        Relay_Source_Log_File: master_bin.000001
           Replica_IO_Running: Yes
          Replica_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Source_Log_Pos: 1897
              Relay_Log_Space: 1468
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File: 
           Source_SSL_CA_Path: 
              Source_SSL_Cert: 
            Source_SSL_Cipher: 
               Source_SSL_Key: 
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Source_Server_Id: 1
                  Source_UUID: 3393dd41-fb9f-11ed-a80f-000c291140b1
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 86400
                  Source_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Source_SSL_Crl: 
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 1
            Network_Namespace: 
1 row in set (0.00 sec)

说明:如上所示,主节点数据成功同步到从节点.

参考网址:
https://blog.csdn.net/weixin_46780832/article/details/129657998
https://blog.csdn.net/weixin_45648789/article/details/129125997

标签:主从复制,Log,7.3,Source,master,rhel,mysql,Master,SSL
From: https://blog.51cto.com/u_12991611/6358783

相关文章

  • rhel 7.3安装mysql 8.0.27
    文档课题:rhel7.3安装mysql8.0.27.系统:rhel7.364位数据库:mysql8.0.27安装包:mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz1、安装前检查1.1、系统版本[root@leo-mysql-master~]#cat/etc/*releaseNAME="RedHatEnterpriseLinuxServer"VERSION="7.3(Maipo)"ID=......
  • RHEL7-NAT模式连接外部网络
     通过VMware成功安装RHEL7服务器后,除了使用Bridge桥接模式连接外部网络外,还可以使用NAT模式连接外部网络前提:建议关闭防火墙及selinux。具体做法参考:https://blog.51cto.com/ztj1216/63445921.配置虚拟机RHEL7NAT模式连接外部网络1.1.设置虚拟机网络为NAT方式1.2. 查看Windows端......
  • RHEL8使用iSCSI部署网络存储-Linux就这么学17
        本章首先介绍计算机硬件存储设备的不同接口的优缺点,并由此切入iSCSI技术主题的讲解。iSCSI技术实现了物理硬盘设备与TCP/IP网络协议的相互结合,使得用户能够通过互联网方便地访问远程机房提供的共享存储资源。我们将学习在Linux系统上部署iSCSI服务端程序,并分别......
  • mysqld_multi实现单机主从复制
    文档课题:mysqld_multi实现单机主从复制.数据库:mysql5.7.18系统:rhel7.3安装包:mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz1、理论知识mysqld_multi用于在一台服务器上管理多个mysqld进程,这些进程使用不同的socket文件并监听不同端口.mysqld_multi可以批量启动、关闭、或报告m......
  • Redis主从复制实现与原理
    一、概述主从复制,是指将一台Redis服务器的数据复制到其他的Redis服务器。前者称为主节点(Master/Leader),后者称为从节点(Slave/Follower);数据是从主节点复制到从节点的。其中,主节点负责写数据(当然有读的权限),从节点负责读数据(它没有写数据的权限)。默认的配置下,每个Redis都是主节点。一......
  • Windows环境下配置MySQL主从复制详细教程
    ​一、下载mysql下载地址:MySQL::MySQLCommunityDownloads1、点击箭头所指​编辑 2、默认页面是下载mysql8,如果想下载mysql5点击箭头所指​编辑3、点击箭头所指选择需要的版本​编辑4、点击箭头所指可以选择32位或64位​编辑5、然后点击download​编辑二、解压压......
  • Redhat7.3linux系统防火墙命令总结
    在Linux系统部署皕杰报表后,需要关闭防火墙或者开放报表工具使用的端口,才能通过浏览器访问报表。在linux中如何关闭防火墙或开启端口呢?基本上是基于命令操作。通过几天的实践,总结了有关防火墙的操作命令,现记录如下。systemctlstatusfirewalld:查看防火墙状态systemctlstartfirewa......
  • rhel 7.3安装mysql 5.7.21
    文档课题:rhel7.3安装mysql5.7.21安装包:mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar1、上传安装包[root@leo-mysql5721~]#mkdir-p/u01/setup/dbsftp>lcdF:\installmedium\mysql\MySQL-5.7.21sftp>cd/u01/setup/dbsftp>putmysql-5.7.21-1.el7.x86_64.rpm-bundle......
  • MySQL的主从复制之异步复制的简单实现
    (文章目录)前言MySQL异步复制是MySQL数据库中的一种数据复制方式,主要用于在多个MySQL实例之间复制数据。与同步复制不同,异步复制并不要求每个MySQL实例都必须立即接收新数据的更新。相反,更新在主MySQL实例上执行后,异步地传输给从MySQL实例,所以数据的同步可能需要花费一定的时间......
  • mysql主从复制(gtid模式)修改主库ip
    环境:OS:Centos7DB:mysql5.7.29 基于GTID复制的主从环境,主库修改了ip后,修改从库同步信息(不需要指定master_log_file和master_log_pos)1.停掉从库stopslave; 2.查看从库状态mysql>showslavestatus\G;***************************1.row*********************......