爲什麼要對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)
總結:
1.一定要完全備份,在slave端進行重放;
2. 授權用戶時,所記錄的日誌文件和座標要明確;
3.書寫要準確。
主從模型有利用提升數據庫性能,給用戶最優的體驗,當然更深層級的讀寫分離就是基於主從模型構建。