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