本文主要給大家介紹為什么MySQL要使用主從模型,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下為什么mysql要使用主從模型吧。
在鶴城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),網(wǎng)絡(luò)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè),鶴城網(wǎng)站建設(shè)費(fèi)用合理。
主從復(fù)制的原理:主云服務(wù)器(master)上的二進(jìn)制日志(binlog)中記錄的操作,可以在從云服務(wù)器(slave)上的中繼日志(relaylog)得到重放,進(jìn)而可以實(shí)現(xiàn)數(shù)據(jù)的同步,保證數(shù)據(jù)的一致性;
主從復(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ù)器上對(duì)復(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ù)庫(kù)中;
根據(jù)從云服務(wù)器上對(duì)復(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.對(duì)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 #賦予一個(gè)id sync_binlog=1 #二進(jìn)制日志同步至磁盤上 innodb_flush_log_at_trx_commit=1 #每執(zhí)行完一個(gè)事務(wù)后,及時(shí)寫入磁盤 [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 #賦予一個(gè)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.啟動(dòng)master,將所有的數(shù)據(jù)庫(kù)進(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)一個(gè)具有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)屬性信息,并啟動(dòng)復(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)我們?cè)趍aster上進(jìn)行數(shù)據(jù)操作時(shí),slave上也會(huì)進(jìn)行記錄,并重放至本地?cái)?shù)據(jù)中;
驗(yàn)證:master上創(chuàng)建一個(gè)數(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è)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。