主從複製:
當mysql數據庫的數據量太大的時候,查詢數據就很吃力了,無論怎麼優化都會產生瓶頸,這時我們需要增加服務器設備來實現分佈式數據庫,實現多機熱備份,要想實現多機的熱備,首先要了解主從數據庫服務器的版本的需求,主從mysql的安裝運行版本需一致。因此,我們利用mysql自帶的REPLICATION來實現mysql多機熱備的功能,mysql版本爲5.7進行演示。
讀寫分離:
就是把對數據庫的讀操作和寫操作分離開,將讀寫壓力分擔到多臺服務器上,通常用於讀遠大於寫的場景。讀寫分離的基本原理是讓主數據庫處理事務性增、改、刪操作(INSERT、UPDATE、DELETE),而從數據庫處理SELECT查詢操作。數據庫複製被用來把事務性操作導致的變更同步到集羣中的從數據庫。數據多了之後,對數據庫的讀、寫就會很多。寫庫就一個,讀庫可以有多個,利用主從複製負責主庫和多個讀庫的數據同步。
上一章我們講了再Laravel中配置讀寫分離和負載均衡,那麼有點小小的問題還需要在本節中完成,就是mysql的主從複製,如果沒有主從複製的話,數據會有些問題,所以本節也把最重要的內容分享出來,大家學習。
配置環境
操作系統:兩臺CentOS 7.6的Linux系統(虛擬機)
數據庫版本:MySQL 5.7
主服務器IP:192.168.1.100
從服務器IP:192.168.1.101
安裝Mysql數據庫(後面單獨出一篇mysql的安裝教程)
我們現在就簡單安裝一下mysql,新手按步驟進行操作:
1、先檢查系統是否安裝有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服務端
[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 -- 關閉防火牆
配置主(Master)數據庫
1、修改數據庫配置文件
[root@localhost ~]# vi /etc/my.cnf
更改配置文件
[mysqld]#開啓二進制日誌
log-bin=mysql-bin
#標識唯一id(必須),一般使用ip最後位
server-id=100#不同步的數據庫,可設置多個
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#指定需要同步的數據庫(和slave是相互匹配的),可以設置多個
binlog-do-db=test
注:日誌的存儲容量設置的大小是根據你的服務器資源實際情況修改的。
2、重啓數據庫服務(mysqld)
service mysqld restart
3、登陸MySQL數據庫允許從庫獲得主庫日誌
[root@localhost ~]# mysql -u root -p"你的密碼"
注:第一次登陸是不需要輸入root的密碼的,有密碼就要輸入哦。
進入後做如下配置:
#給從庫放權限
mysql>GRANT FILE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY 'repl password'; #創建用戶
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.101' IDENTIFIED BY 'repl password'; #修改用戶權限
mysql>select host,user,password from mysql.user; #查看是否修改成功
mysql>FLUSH PRIVILEGES; #刷新權限
4、重啓MySQL服務,登錄MySQL,查看主庫信息
[root@localhost ~]# service mysqld restart #重啓mysql服務[root@localhost ~]# mysql -u root -p #登陸mysql
mysql> show master status; #查看master狀態
顯示大概如下內容
+------------------+----------+--------------+----------------------------------+-------------------+
| 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)
注:如果執行這個步驟始終爲Empty set(0.00 sec),那說明前面的my.cnf沒配置對,請回去重新檢查配置步驟。
配置從(Slave)數據庫
1、修改從庫的數據庫配置文件
[root@localhost ~]# vi /etc/my.cnf
將裏面的內容修改爲
#開啓二進制日誌
log-bin=mysql-bin
server-id=101binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#與主庫配置保持一致
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2、重啓MySQL服務,登錄MySQL
[root@localhost ~]# service mysqld restart
[root@localhost ~]# mysql -u root -p"你的密碼"
並作如下修改:
#關閉Slave
mysql> stop slave; #設置連接主庫信息
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;#開啓Slave
mysql> start slave;
注:上面的master_log_file是在配置Master的時候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一對應。
3、查看從庫狀態信息
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)
至此整個過程就配置好了。現在可以在主服務器上創建一個表,然後在從服務器上查詢剛創建的這個表,看是否存在就可以啦。
運行測試
1、關於增刪改查,主從數據不一致問題:原因:在主庫的logbin中的確有執行刪除語句,但是在從庫的logbin中卻沒有刪除語句。解決:使用 use database 選取當前數據庫架構中的需要操作的數據庫,然後在執行刪除,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日誌,最好關閉日誌,在/etc/my.cnf
#手動刷新日誌
mysql> show master status;#刪除全部
mysql> reset slave; #或 rest master;
#刪除MySQL-bin.004
mysql> PURGE MASTER LOGS TO 'MySQL-bin.004';
4、基本命令
mysql> show master status; #查看主的狀態
mysql> show slave status\G; #查看從的狀態
mysql> show processlist; #查看mysql的進程狀態信息
mysql> show master logs; #查看主的日誌
mysql> reset slave;#(慎用,清除日誌同時會清空從的配置信息)
瞭解更多請關注公衆號(Laravel技術社區)