上一章我們講了再Laravel中配置讀寫分離和負(fù)載均衡,那么有點(diǎn)小小的問題還需要在本節(jié)中完成,就是mysql的主從復(fù)制,如果沒有主從復(fù)制的話,數(shù)據(jù)會有些問題,所以本節(jié)也把最重要的內(nèi)容分享出來,大家學(xué)習(xí)。 配置環(huán)境 操作系統(tǒng):兩臺CentOS 7.6的Linux系統(tǒng)(虛擬機(jī)) 數(shù)據(jù)庫版本:MySQL 5.7 主服務(wù)器IP:192.168.1.100 從服務(wù)器IP:192.168.1.101 安裝Mysql數(shù)據(jù)庫(后面單獨(dú)出一篇mysql的安裝教程) 我們現(xiàn)在就簡單安裝一下mysql,新手按步驟進(jìn)行操作: 1、先檢查系統(tǒng)是否安裝有mysql [root@localhost ~]#yum list installed mysql*[root@localhost ~]#rpm –qa | grep mysql* 2、查看有沒有安裝包
3、安裝mysql客戶端 [root@localhost ~]#yum install mysql 4、安裝mysql服務(wù)端
5、開啟端口 vi /etc/sysconfig/iptables-A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT 6、重啟防火墻
配置主(Master)數(shù)據(jù)庫 1、修改數(shù)據(jù)庫配置文件 [root@localhost ~]# vi /etc/my.cnf 更改配置文件
2、重啟數(shù)據(jù)庫服務(wù)(mysqld) service mysqld restart 3、登陸MySQL數(shù)據(jù)庫允許從庫獲得主庫日志
進(jìn)入后做如下配置: #給從庫放權(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,查看主庫信息
顯示大概如下內(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)
配置從(Slave)數(shù)據(jù)庫 1、修改從庫的數(shù)據(jù)庫配置文件
將里面的內(nèi)容修改為 #開啟二進(jìn)制日志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
并作如下修改: #關(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;
3、查看從庫狀態(tài)信息
如下信息: ************************* 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,此處會顯示錯(cuò)誤信息 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) 至此整個(gè)過程就配置好了?,F(xiàn)在可以在主服務(wù)器上創(chuàng)建一個(gè)表,然后在從服務(wù)器上查詢剛創(chuàng)建的這個(gè)表,看是否存在就可以啦。 運(yùn)行測試 1、關(guān)于增刪改查,主從數(shù)據(jù)不一致問題:原因:在主庫的logbin中的確有執(zhí)行刪除語句,但是在從庫的logbin中卻沒有刪除語句。解決:使用 use database 選取當(dāng)前數(shù)據(jù)庫架構(gòu)中的需要操作的數(shù)據(jù)庫,然后在執(zhí)行刪除,OK同步成功。 2、查詢binlog主從日志的方法
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、基本命令
|
|