Home >Backend Development >PHP Tutorial >Related operations of Redis master-slave synchronization and read-write separation settings
This article introduces the use of the master-slave synchronization function (master, slave) of Redis to enable the program to separate reading and writing, avoid io bottlenecks, and improve data reading and writing efficiency.
Redis supports one master server to synchronize multiple slave servers, and the synchronization uses the publish/subscribe mechanism.
One master can also be layered for multiple slaves. Each slave can synchronize the slave again and expand it into a tree structure.
The default port of Redis is 6379. In order not to affect the original Redis, we use the new port
master Configurationredis_master.conf
port 6300requirepass 123456masterauth 123456daemonize yes
slave1 Configurationredis_slave1.conf Set as master’s slave
port 6301slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize yes
slave2 Configurationredis_slave2.conf Set slave
port 6302slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize yes
daemonize to the master to indicate background startup.
requirepass Password for host authentication.
masterauth Verify the password for the slave to access the host, which needs to be consistent with the host's requirepass.
Since master-slave switching needs to be demonstrated later, the verification passwords of the three sets of conf are the same.
Start master, slave1, slave2 in sequence
redis-server redis_master.conf redis-server redis_slave1.conf redis-server redis_slave2.conf
After execution, check whether the startup is successful
ps aux|grep redis root 1858 Ss 3:55 0:00.01 redis-server *:6302 root 1849 Ss 3:54 0:00.01 redis-server *:6301 root 1842 Ss 3:54 0:00.02 redis-server *:6300
Entermaster, set the value of key abc to 123
redis-cli -p 6300127.0.0.1:6300> auth 123456OK127.0.0.1:6300> set abc 123OK127.0.0.1:6300> get abc"123"
Enter slave1 and slave2 respectively to check whether the data is synchronized
slave1:
redis-cli -p 6301 127.0.0.1:6301> auth 123456OK127.0.0.1:6301> get abc"123" 127.0.0.1:6301>
slave2:
redis-cli -p 6302 127.0.0.1:6302> auth 123456OK127.0.0.1:6302> get abc"123" 127.0.0.1:6302>
EntermasterModify the value of key abc to 456
127.0.0.1:6300> set abc 456OK127.0.0.1:6300> get abc"456"
Check whether slave1 and slave2 are synchronized
slave1:
127.0.0.1:6301> get abc"456"
slave2:
127.0.0.1:6302> get abc"456"
During the operation process, if there is a problem with the master, we can set up another slave The machine is automatically set to master. The sentinel function of Redis is mainly used here to implement master-slave switching.
sentinel1.conf
port 26301sentinel monitor master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log"daemonize yes
sentinel2.conf
port 26302sentinel monitor master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log"daemonize yes
sentinel monitor master 127.0.0.1 6300 2# The 2 in ## indicates that master-slave switching will be performed only when more than 2 sentinel services detect master failure.
Start two sentinel processes
redis-server sentinel1.conf --sentinel redis-server sentinel2.conf --sentinel ps aux|grep redis root 2643 Ss 4:28 0:00.02 redis-server *:26302 [sentinel] root 2636 Ss 4:28 0:00.02 redis-server *:26301 [sentinel]Redis log can be seen, the startup is successful and start monitoring
Running mode=sentinel, port=26301. Sentinel ID is 3a23343948cd7f26662ccba1d01b92955311ef52 +monitor master master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0.0.1 6302 @ master 127.0.0.1 6300Running mode=sentinel, port=26302. Sentinel ID is ce0ee2af6b454205a3e475763945f505a10a7d6a +monitor master master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0.0.1 6302 @ master 127.0.0.1 6300+sentinel sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 @ master 127.0.0.1 6300+sentinel sentinel ce0ee2af6b454205a3e475763945f505a10a7d6a 127.0.0.1 26302 @ master 127.0.0.1 6300
Terminate the master, test the master and slave After switching the
kill master process, sentinel determines that the master is invalid and performs master-slave switching processing.
The log is as follows:+failover-state-reconf-slaves master master 127.0.0.1 6300+slave-reconf-sent slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+config-update-from sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 +switch-master master 127.0.0.1 6300 127.0.0.1 6302+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6302+slave slave 127.0.0.1:6300 127.0.0.1 6300 @ master 127.0.0.1 6302-odown master master 127.0.0.1 6300+slave-reconf-inprog slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave-reconf-done slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+failover-end master master 127.0.0.1 6300+switch-master master 127.0.0.1 6300 127.0.0.1 6302+convert-to-slave slave 127.0.0.1:6300 127.0.0.1 6300 @ master 127.0.0.1 6302
As can be seen from the log, the master-slave switch performed the following operations:
1. Switchslave2 For the new master, slaveof 127.0.0.1 6300 in redis_slave2.conf is automatically deleted.
2. Automatically update slaveof 127.0.0.1 6300 in redis_slave1.conf to slaveof 127.0.0.1 6302, and use slave2 as the new master. 3.After the original master is restarted, it will be used as a slave, and slaveof 127.0.0.1 6302 will be added to redis_master.conf automatically.
Conduct master-slave synchronization test after restarting the original master
Original masterUpdate key abc to 888, because it is now a slave. So the update failed.
127.0.0.1:6300> set abc 888(error) READONLY You can't write against a read only slave.
slave2 Update key abc to 888
127.0.0.1:6302> set abc 888OK127.0.0.1:6302> get abc"888"Original master, slave1 checks whether it is synchronized
Original master
127.0.0.1:6300> get abc"888"
slave1
127.0.0.1:6301> get abc"888"After inspection, after the master-slave switch, slave2 serves as the new master and other servers serve as slaves and can be used normally. This article explains the related operations of Redis master-slave synchronization and read-write separation settings. For more related content, please pay attention to the PHP Chinese website. related suggestion:
Introducing the method of mysql rebuilding table partition and retaining data
Relevant content of PHP generating unique RequestID class
php json_encode does not support the solution to object private attributes
The above is the detailed content of Related operations of Redis master-slave synchronization and read-write separation settings. For more information, please follow other related articles on the PHP Chinese website!