您好,登錄后才能下訂單哦!
本文主要給大家介紹為什么mysql要使用主從模型,文章內容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下為什么mysql要使用主從模型吧。
主從復制的原理:主云服務器(master)上的二進制日志(binlog)中記錄的操作,可以在從云服務器(slave)上的中繼日志(relaylog)得到重放,進而可以實現數據的同步,保證數據的一致性;
主從復制的前提條件:
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要使用主從模型,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業知識信息 ,可以持續關注我們的行業資訊欄目的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。