The master-slave architecture can be implemented between native multi-instance databases, and also between multiple instances of different machines.
The master is readable and writable, and is read-only so that the architecture of read-write separation can be realized.
Features of Redis master-slave replication:
1. One master can have multiple slave (1-to-many relationships)
2. Multiple slave can connect to the same master, and can also connect to other slave
The reason for this is that if master hangs up, one of the slave can immediately act as master's role
The entire service flow can be unaffected
3. The copy process does not block master, and master can continue to process client requests while synchronizing the data.
4. Improve the scalability of the system
Redis master-slave replication process:
1.slave Connect with master and send Sync Sync command.
2.master starts a background process, saves the database snapshot to a file, and the master master process starts collecting new write commands and caches
3. After the background is finished saving, send the file to slave
4.slave saving files to your hard disk
Redis Master-slave replication configuration and usage are simple.
Master-slave replication allows multiple slave servers to have the same database copy as master server.
Here are some of the features of Redis master-slave replication:
1.master can have multiple slave
2. In addition to multiple slave connected to the same master, slave can also connect other slave to form a graphic structure
3. Master-slave replication does not block primary. This means that when one or more slave and master synchronize data for the first time, master can continue to process requests from the client.
Conversely, when the data is first synchronized, it blocks requests that cannot process the client slave.
4. Master-slave replication can be used to improve the scalability of the system, we can use multiple slave dedicated to client read requests,
For example, the sort operation can be handled using slave. can also be used to do simple data redundancy.
5. You can disable data persistence in master, just comment out all the save configurations in the master configuration file and configure data persistence on slave only.
Configuration process:
Here, we use two different instances of the same server to prepare the experiment.
Main: 6378
Preparation: 6379
1. Start the master Redis instance
[Email protected] ~]#/etc/init.d/redis_6378 restartstopping ... Redis stoppedstarting Redis Server ...
2. Add Spare instance Parameters
[Email protected] ~]# vi/etc/redis/6379.conf Add the following slaveof 127.0.0.1 6378 #表示主节点是127.0.0.1 6378 Port
3. Start a Standby instance
[Email protected] ~]#/etc/init.d/redis_6379 restartstopping ... Redis stoppedstarting Redis Server ...
4. View role Status
[[Email protected] ~]# redis-cli -p 6378 info....# replicationrole:master #角色是masterconnected_slaves: 1 # The currently connected slaves has 1 slave0:ip=127.0.0.1,port=6379,state=online,offset=85,lag=1 # slave0 is the port 6379 of 127.0.0.1, with a status of Onlinemaster_repl_offset:85repl_backlog_active:1repl_backlog_size : 1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:84 .... [[Email protected] ~]# redis-cli -p 6379 info....# replicationrole:slave #角色状态是slavemaster_host:127.0.0.1 #master的地址是127.0.0.1master_ port:6378 #master端口号是6378master_link_status:up #跟master连接的状态是up的master_last_io_seconds_ago: 0master_sync_in_progress:0slave_repl_offset : 323slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlOg_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 .... This shows that the master-slave has been configured, very simple.
5. Verifying synchronization
[[email protected] ~]# redis-cli-p 6378127.0.0.1:6378> set S1 ABC #在master创建两个值, s1 s2ok127.0.0.1:6378> set S2 bcdok127.0.0.1:6378> mget s1 S21) "ABC" 2) "BCD" 127.0.0.1:6378> quit[[email protected] ~]# redis-cli-p 6379127.0 .0.1:6379> get s1 #在slave中也能读取刚才的s1 s2 two value "abc" 127.0.0.1:6379> Get S2 "BCD" 127.0.0.1:6379> set S1 AAA #这里slave无法写, can only read (error) READONLY you can ' t write against a read only slave.
6. Further experiments, Slave-slave
Initializes an instance of 6380 [[email protected] ~]# cd /root/redis-2.8.13/utils/[[email protected] utils]# ./install_server.sh Welcome to the redis service installerthis script will help you easily set up a running redis serverplease select the redis port for this instance: [6379 ] 6380PLEASE SELECT THE REDIS CONFIG FILE NAME [/ETC/REDIS/6380.CONF] selected default - /etc/redis/6380.confplease select the redis log file name [/var/log/redis_6380.log] selected default - /var/log/redis_ 6380.logplease select the data directory for this instance [/var/lib/ Redis/6380] selected default - /var/lib/redis/6380please select the redis executable path [/usr/local/bin/redis-server] selected config:port : 6380Config file : /etc/redis/6380.confLog file : /var/log/redis_6380.logData dir : /var/lib/redis/6380executable : /usr/local/bin/ Redis-servercli executable : /usr/local/bin/redis-cliis this ok? then press enter to go on or ctrl-c to abort. Copied /tmp/6380.conf => /etc/init.d/redis_6380installing service ... successfully added to chkconfig! successfully added to runlevels 345! Starting redis server ... Installation successful! also modifies the configuration file [[email protected] utils]# vi /etc/redis/6380.conf Add the following content SLAVEOF 127.0.0.1 6379[[EMAIL PROTECTED] UTILS]# /ETC/INIT.D/REDIS_6380 RESTARTSTOPPING&NBSP, ..... Redis stoppedstarting redis server ... [[Email protected] utils]# redis-cli -p 6380 info...# replicationrole: Slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:8master_sync_in_ Progress:0slave_repl_offset:29slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_ Backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 ... [[Email protected] utils]# redis-cli -p 6379 info...# replicationrole: Slavemaster_host:127.0.0.1master_port:6378master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_ progress:0slave_repl_offset:1092slave_priority:100slave_read_only:1connected_slaves:1slave0:ip=127.0.0.1,port= 6380,state=online,offset=85,lag=1 #在备状态下, and connected to a standby master_repl_offset:85repl_backlog_active:1repl_. backlog_size:1048576Repl_backlog_first_byte_offset:2repl_backlog_histlen:84 ....
7. Verifying synchronization
[[EMAIL PROTECTED] UTILS]# REDIS-CLI -P 6380 MGET S1 S21] "ABC" 2) "BCD" [[email protected] utils]# redis-cli -p 6378 shutdown[[email Protected] utils]# redis-cli -p 6379 info...# replicationrole:slavemaster_host :127.0.0.1master_port:6378master_link_status:down #跟master的连接已经挂掉了master_last_io_ Seconds_ago:-1master_sync_in_progress:0slave_repl_offset:1470master_link_down_since_seconds:7slave_priority : 100slave_read_only:1connected_slaves:0master_repl_offset:477repl_backlog_active:1repl_backlog_size : 1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:476 ... [[Email protected] utils]# redis-cli -p 6380 info...# replicationrole: slavemaster_host:127.0.0.1master_port:6379master_link_status:down #表示跟6379的连接也挂掉了 Master_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:1mastEr_link_down_since_seconds:65slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_ Backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 ... After discovering that the master is hung up, the library is still readable from the [email protected] utils]# redis-cli -p 6380 get s1 "ABC"
8. Adding certifications
If someone else knows my IP and port number, it can be used directly to synchronize with Redis. Unsafe, let's look at how to set a password
[[email protected] utils]# vi/etc/redis/6378.conf Add a line requirepass redis #这里表示密码就是redis [[email protected] utils]# /etc/init.d/redis_6378 startstarting Redis Server ... [[email protected] utils]# redis-cli-p 6378127.0.0.1:6378> get s1 #下次登录直接使用get命令就无法获得值了 (Error) Noauth Authenticat Ion required.127.0.0.1:6378> Auth Redis #输入密码后, you can use the Get command just now. Ok127.0.0.1:6378> get S1 "abc"
9. Configure authentication from the library
[[Email protected] utils]# redis-cli -p 6379 info# replicationrole: slavemaster_host:127.0.0.1master_port:6378master_link_status:down # From this place to see the connection to the Lord or the Master_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:1master_link_down_ Since_seconds:1076slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:1961repl_backlog_ active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:1960. Open configuration file add authentication information [[ email protected] utils]# vi /etc/redis/6379.conf Add the following information Masterauth redis reboot the repository, You must restart the repository here [[email protected] utils]# /etc/init.d/redis_6379 restartstopping .... Redis stoppedstarting redis server ... The state becomes up. [[email protected] utils]# redis-cli -p 6379 info | grep mastermaster_host:127.0.0.1master_port:6378master_link_status:upmaster_last_io_seconds_ago:9master_sYnc_in_progress:0master_repl_offset:1
10. Synchronous verification
[Email protected] utils]# redis-cli-p 6378127.0.0.1:6378> auth redisok127.0.0.1:6378> set S3 abcd123ok127.0.0.1 :6378> [[email protected] utils]# redis-cli-p 6379127.0.0.1:6379> get S3 "abcd123"
Describes the following parameters
Slave-read-only Yes #表示从库只读
Repl-ping-slave-period #从库会发送ping确认跟主库是否还可以互通
This article is from the "long name will be good to remember" blog, please be sure to keep this source http://xiaoyiyi.blog.51cto.com/1351449/1705933
(d) Redis master-Slave synchronization data