主從復(fù)制: 當mysql數(shù)據(jù)庫的數(shù)據(jù)量太大的時候,查詢數(shù)據(jù)就很吃力了,無論怎么優(yōu)化都會產(chǎn)生瓶頸,這時我們需要增加服務(wù)器設(shè)備來實現(xiàn)分布式數(shù)據(jù)庫,實現(xiàn)多機熱備份,要想實現(xiàn)多機的熱備,首先要了解主從數(shù)據(jù)庫服務(wù)器的版本的需求,主從mysql的安裝運行版本需一致。因此,我們利用mysql自帶的REPLICATION來實現(xiàn)mysql多機熱備的功能,mysql版本為5.7進行演示。
讀寫分離: 就是把對數(shù)據(jù)庫的讀操作和寫操作分離開,將讀寫壓力分擔(dān)到多臺服務(wù)器上,通常用于讀遠大于寫的場景。讀寫分離的基本原理是讓主數(shù)據(jù)庫處理事務(wù)性增、改、刪操作(INSERT、UPDATE、DELETE),而從數(shù)據(jù)庫處理SELECT查詢操作。數(shù)據(jù)庫復(fù)制被用來把事務(wù)性操作導(dǎo)致的變更同步到集群中的從數(shù)據(jù)庫。數(shù)據(jù)多了之后,對數(shù)據(jù)庫的讀、寫就會很多。寫庫就一個,讀庫可以有多個,利用主從復(fù)制負責(zé)主庫和多個讀庫的數(shù)據(jù)同步。
上一章我們講了再Laravel中配置讀寫分離和負載均衡,那么有點小小的問題還需要在本節(jié)中完成,就是mysql的主從復(fù)制,如果沒有主從復(fù)制的話,數(shù)據(jù)會有些問題,所以本節(jié)也把最重要的內(nèi)容分享出來,大家學(xué)習(xí)。
配置環(huán)境
操作系統(tǒng):兩臺CentOS 7.6的Linux系統(tǒng)(虛擬機)
數(shù)據(jù)庫版本:MySQL 5.7
主服務(wù)器IP:192.168.1.100
從服務(wù)器IP:192.168.1.101
安裝Mysql數(shù)據(jù)庫(后面單獨出一篇mysql的安裝教程)
我們現(xiàn)在就簡單安裝一下mysql,新手按步驟進行操作:
1、先檢查系統(tǒng)是否安裝有mysql
[root@localhost ~]#yum list installed mysql*[root@localhost ~]#rpm –qa | grep mysql*
2、查看有沒有安裝包
[root@localhost ~]#yum list mysql*
3、安裝mysql客戶端
[root@localhost ~]#yum install mysql
4、安裝mysql服務(wù)端
[root@localhost ~]#yum install mysql-server[root@localhost ~]#yum install mysql-devel
5、開啟端口
vi /etc/sysconfig/iptables-A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT
6、重啟防火墻
systemctl restart iptables.service -- 重啟防火墻systemctl stop iptables.service -- 關(guān)閉防火墻
配置主(Master)數(shù)據(jù)庫
1、修改數(shù)據(jù)庫配置文件
[root@localhost ~]# vi /etc/my.cnf
更改配置文件
[mysqld]#開啟二進制日志log-bin=mysql-bin#標識唯一id(必須),一般使用ip最后位server-id=100#不同步的數(shù)據(jù)庫,可設(shè)置多個binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysql#指定需要同步的數(shù)據(jù)庫(和slave是相互匹配的),可以設(shè)置多個binlog-do-db=test
注:日志的存儲容量設(shè)置的大小是根據(jù)你的服務(wù)器資源實際情況修改的。
2、重啟數(shù)據(jù)庫服務(wù)(mysqld)
service mysqld restart
3、登陸MySQL數(shù)據(jù)庫允許從庫獲得主庫日志
[root@localhost ~]# mysql -u root -p'你的密碼'
注:第一次登陸是不需要輸入root的密碼的,有密碼就要輸入哦。
進入后做如下配置:
#給從庫放權(quán)限mysql>GRANT FILE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY 'repl password'; #創(chuàng)建用戶mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.101' IDENTIFIED BY 'repl password'; #修改用戶權(quán)限mysql>select host,user,password from mysql.user; #查看是否修改成功mysql>FLUSH PRIVILEGES; #刷新權(quán)限
4、重啟MySQL服務(wù),登錄MySQL,查看主庫信息
[root@localhost ~]# service mysqld restart #重啟mysql服務(wù)[root@localhost ~]# mysql -u root -p #登陸mysqlmysql> show master status; #查看master狀態(tài)
顯示大概如下內(nèi)容
+------------------+----------+--------------+----------------------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+----------------------------------+-------------------+| mysql-bin.000007 | 120 | ufind_db |information_schema,performance_schema,mysql | |+------------------+----------+--------------+----------------------------------+-------------------+1 row in set (0.00 sec)
注:如果執(zhí)行這個步驟始終為Empty set(0.00 sec),那說明前面的my.cnf沒配置對,請回去重新檢查配置步驟。
配置從(Slave)數(shù)據(jù)庫
1、修改從庫的數(shù)據(jù)庫配置文件
[root@localhost ~]# vi /etc/my.cnf
將里面的內(nèi)容修改為
#開啟二進制日志log-bin=mysql-binserver-id=101binlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=mysql#與主庫配置保持一致replicate-do-db=testreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allslave-net-timeout=60
2、重啟MySQL服務(wù),登錄MySQL
[root@localhost ~]# service mysqld restart[root@localhost ~]# mysql -u root -p'你的密碼'
并作如下修改:
#關(guān)閉Slavemysql> stop slave; #設(shè)置連接主庫信息mysql> change master to master_host='192.168.1.100',master_user='repl',master_password='repl password',master_log_file='mysql-bin.000007', master_log_pos=120;#開啟Slavemysql> start slave;
注:上面的master_log_file是在配置Master的時候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一對應(yīng)。
3、查看從庫狀態(tài)信息
mysql> show slave status \G;
如下信息:
************************* 1. row ************************* Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.100 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 120 Relay_Log_File: localhost-relay-bin.000007 Relay_Log_Pos: 520 Relay_Master_Log_File: mysql-bin.000007 Slave_IO_Running: Yes //顯示yes為成功 Slave_SQL_Running: Yes //顯示yes為成功,如果為no,一般為沒有啟動master Replicate_Do_DB: test Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息 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: 357 Relay_Log_Space: 697 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: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: //如果為no,此處會顯示錯誤信息 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 01 row in set (0.00 sec)
至此整個過程就配置好了。現(xiàn)在可以在主服務(wù)器上創(chuàng)建一個表,然后在從服務(wù)器上查詢剛創(chuàng)建的這個表,看是否存在就可以啦。
運行測試
1、關(guān)于增刪改查,主從數(shù)據(jù)不一致問題:原因:在主庫的logbin中的確有執(zhí)行刪除語句,但是在從庫的logbin中卻沒有刪除語句。解決:使用 use database 選取當前數(shù)據(jù)庫架構(gòu)中的需要操作的數(shù)據(jù)庫,然后在執(zhí)行刪除,OK同步成功。
2、查詢binlog主從日志的方法
查看binlog全部文件mysql>show binary logs;#查看binlog是否開啟NO為開啟mysql> show variables like 'log_bin%';#詳細信息mysql> show variables like 'binlog%';#查看binlog日志mysql> show binlog events in'mysql-bin.000007';#或者使用mysqlbinlog,如果報錯使用--no-defaults(使用全路徑)[root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019
3、手動清理master日志,最好關(guān)閉日志,在/etc/my.cnf
#手動刷新日志mysql> show master status;#刪除全部mysql> reset slave; #或 rest master;#刪除MySQL-bin.004mysql> PURGE MASTER LOGS TO 'MySQL-bin.004';
4、基本命令
mysql> show master status; #查看主的狀態(tài)mysql> show slave status\G; #查看從的狀態(tài)mysql> show processlist; #查看mysql的進程狀態(tài)信息mysql> show master logs; #查看主的日志mysql> reset slave;#(慎用,清除日志同時會清空從的配置信息)