溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

為什么mysql要使用主從模型

發(fā)布時間:2020-05-09 10:38:05 來源:億速云 閱讀:333 作者:三月 欄目:MySQL數(shù)據(jù)庫

本文主要給大家介紹為什么mysql要使用主從模型,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下為什么mysql要使用主從模型吧。

主從復(fù)制的原理:主云服務(wù)器(master)上的二進(jìn)制日志(binlog)中記錄的操作,可以在從云服務(wù)器(slave)上的中繼日志(relaylog)得到重放,進(jìn)而可以實現(xiàn)數(shù)據(jù)的同步,保證數(shù)據(jù)的一致性;

為什么mysql要使用主從模型

主從復(fù)制的前提條件:

1.     主云服務(wù)器的數(shù)據(jù)需要先進(jìn)行一次完全備份,在從云服務(wù)器上恢復(fù);

2.     開啟免密ssh登錄;

3.     主云服務(wù)器上開啟二進(jìn)制日志,寫入server_id,sync_binlog參數(shù)設(shè)置為1;

4.     從云服務(wù)器上開啟中繼日志,寫入server_id,開啟讀鎖(從云服務(wù)器只可讀不可寫);

 

從云服務(wù)器上對復(fù)制相關(guān)的線程(show slave status\G);

       Slave_IO_Running: Yes

              IO_thread:用于和Master相連接,監(jiān)控和接收Master的二進(jìn)制日志;

Slave_SQL_Running: Yes

       SQL_thread:用于監(jiān)控,讀取和重放中繼日志的日志信息,并將數(shù)據(jù)寫入到數(shù)據(jù)庫中;

 

根據(jù)從云服務(wù)器上對復(fù)制的線程,我們就可以知道,主從復(fù)制就是,master上的操作都被記錄在binlog中,然后slave上的IO_thread就將master中的binlog記錄的內(nèi)容復(fù)制到本地的relaylog中,最后SQL_thread就將relaylog記錄的內(nèi)容重放,達(dá)到數(shù)據(jù)一致的目標(biāo);

 

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

 

2.對master和slave的主配置文件進(jìn)行修改;

Master的mysql主配置文件:(默認(rèn)/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   #開啟二進(jìn)制日志
server_id=101                   #賦予一個id
sync_binlog=1                   #二進(jìn)制日志同步至磁盤上
innodb_flush_log_at_trx_commit=1  #每執(zhí)行完一個事務(wù)后,及時寫入磁盤
 
[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的主配置文件(默認(rèn)/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,將所有的數(shù)據(jù)庫進(jìn)行完全備份,并在slave上進(jìn)行重放;


[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授權(quán)一個具有replication的用戶,可以讓slave用于登錄master進(jìn)行復(fù)制日志內(nèi)容,并記錄當(dāng)前二進(jìn)制日志文件名及坐標(biāo)(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上使用授權(quán)用戶進(jìn)行指定master的相關(guān)屬性信息,并啟動復(fù)制線程;

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)

至此,主從模型搭建完畢,當(dāng)我們在master上進(jìn)行數(shù)據(jù)操作時,slave上也會進(jìn)行記錄,并重放至本地數(shù)據(jù)中;


驗證:master上創(chuàng)建一個數(shù)據(jù)表,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)

看完以上關(guān)于為什么mysql要使用主從模型,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI