为什么mysql要使用主从模型
admin
2023-04-21 09:05:28
0次

本文主要给大家介绍为什么mysql要使用主从模型,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下为什么mysql要使用主从模型吧。

主从复制的原理:主云服务器(master)上的二进制日志(binlog)中记录的操作,可以在从云服务器(slave)上的中继日志(relaylog)得到重放,进而可以实现数据的同步,保证数据的一致性;

为什么mysql要使用主从模型

主从复制的前提条件:

1.     主云服务器的数据需要先进行一次完全备份,在从云服务器上恢复;

2.     开启免密ssh登录;

3.     主云服务器上开启二进制日志,写入server_id,sync_binlog参数设置为1;

4.     从云服务器上开启中继日志,写入server_id,开启读锁(从云服务器只可读不可写);

 

从云服务器上对复制相关的线程(show slave status\G);

       Slave_IO_Running: Yes

              IO_thread:用于和Master相连接,监控和接收Master的二进制日志;

Slave_SQL_Running: Yes

       SQL_thread:用于监控,读取和重放中继日志的日志信息,并将数据写入到数据库中;

 

根据从云服务器上对复制的线程,我们就可以知道,主从复制就是,master上的操作都被记录在binlog中,然后slave上的IO_thread就将master中的binlog记录的内容复制到本地的relaylog中,最后SQL_thread就将relaylog记录的内容重放,达到数据一致的目标;

 

1.首先需要master和slave可以免密通信;

 

2.对master和slave的主配置文件进行修改;

Master的mysql主配置文件:(默认/etc/my.cnf)

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
 
innodb_file_per_table=ON
skip_name_resolve=ON
log_bin=/var/lib/mysql/binlog   #开启二进制日志
server_id=101                   #赋予一个id
sync_binlog=1                   #二进制日志同步至磁盘上
innodb_flush_log_at_trx_commit=1  #每执行完一个事务后,及时写入磁盘
 
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
 
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d


Slave的mysql的主配置文件(默认/etc/my.cnf);

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
innodb_file_per_table=ON
skip_name_resolve=ON
server_id=201    #赋予一个server_id
read_only=ON   #开启读锁;
relay_log=slavelog        #开启slave日志
 
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
 
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

3.启动master,将所有的数据库进行完全备份,并在slave上进行重放;


[root@master ~]# mysqldump -uroot -hlocalhost --all-databases -p >  alldata.sql
Enter password:

 

[root@master ~]# scp alldata.sql 172.16.75.2:/
alldata.sql                                                                100% 2803KB  22.1MB/s   00:00

 

[root@slave ~]# mysql -uroot -p < /alldata.sql
Enter password:

5.master授权一个具有replication的用户,可以让slave用于登录master进行复制日志内容,并记录当前二进制日志文件名及坐标(show master logs);

MariaDB [(none)]> grant replication slave on *.* to 'repuser'@'%' identified by 'reppass';

 

MariaDB [(none)]> show master logs;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |     30379 |
| binlog.000002 |   1038814 |
| binlog.000003 |      9598 |
| binlog.000004 |       647 |
| binlog.000005 |       285 |
| binlog.000006 |       720 |
| binlog.000007 |       264 |
| binlog.000008 |       264 |
| binlog.000009 |       264 |
| binlog.000010 |       264 |
| binlog.000011 |  12140997 |
+---------------+-----------+
11 rows in set (0.00 sec)


6.在slave上使用授权用户进行指定master的相关属性信息,并启动复制线程;

MariaDB [(none)]> change master to master_host='172.16.75.1',master_user='repuser',master_password='reppass',master_port=3306,master_log_file='binlog.000011',master_log_pos=12140997;
 
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.75.1
                  Master_User: repuser2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000011
          Read_Master_Log_Pos: 12140997
               Relay_Log_File: slavelog.000018
                Relay_Log_Pos: 12141278
        Relay_Master_Log_File: binlog.000011
             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: 12140997
              Relay_Log_Space: 12141846
              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: 101
1 row in set (0.00 sec)

至此,主从模型搭建完毕,当我们在master上进行数据操作时,slave上也会进行记录,并重放至本地数据中;


验证:master上创建一个数据表,slave上也查看;

master端:


MariaDB [hellodb]> create table stu_info(SID int auto_increment  not null primary key);
Query OK, 0 rows affected (0.10 sec)
MariaDB [hellodb]> desc stu_info;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| SID   | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.00 sec)

slave端:

MariaDB [(none)]> show tables from hellodb;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [(none)]> desc hellodb.stu_info;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| SID   | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.03 sec)

看完以上关于为什么mysql要使用主从模型,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...