Redis Master-Slave synchronization configuration

Source: Internet
Author: User

Master-Slave function:

To prevent Redis disk corruption, resulting in data loss, REDIS provides replication capabilities to automatically synchronize data from one primary database to the database to prevent data loss.
At the same time can also be configured to a master more than ever share the main pressure, the main only accept the write operation, the read operation to the realization of, where from the library can also accept other synchronization requests from the library.
If the main, from the network for reasons of disconnection, the network after the normal from the automatic connection to the master, the main link will be the full data synchronization to ensure that data consistency.

Master-Slave principle:

1. After starting the database, send the SYNC command to the primary database
2. After the master database receives the SYNC command, it starts to save the snapshot, during which all commands issued to the primary database are cached.
3. After the snapshot is saved, the primary database sends the snapshot and cached commands to the database
4. Save the snapshot files from the main data from the database and execute the cache commands from the primary database sequentially.

During the synchronization process, neither the primary nor the database is blocked:

-----> Main Library can continue to accept client-submitted queries or modification requests;
-----> continue to respond to commands from the client by using data from the library by default before synchronizing

First, configure Redis master-Slave

# # Here you can use a server to start two Redis services, just specify a different configuration file, different ports can be
# # to join a firewall rule, I use two servers to complete

Redis master:192.168.1.88
Redis slave:192.168.1.80

1, Redis Master (this does not write the installation process, write to Slave location)

[Email protected] ~]# vim/usr/local/redis/redis.confdaemonize Yesport6379Timeout -loglevel Verbosepidfile/usr/local/redis/logs/Redis.pidlogfile/usr/local/redis/logs/Redis.logrequirepass redispassworddatabases -Save the 1Save - TenSave - 10000rdbcompression yesdbfilename Dump.rdbdir/usr/local/redis/data/appendonly noappendfilename appendonly.aofappendfsync everysecno-appendfsync-on-rewrite Yesauto-aof-rewrite-percentage -Auto-aof-rewrite-min-size 64mb[[email protected]~]# redis-server/usr/local/redis/Redis.conf[[email protected]~]# NETSTAT-ANPT |grepredistcp0 0 0.0.0.0:6379 0.0.0.0:* LISTEN1154/redis-Server[[email protected]~]# Iptables-i INPUT4-S192.168.1.80-P TCP--dport6379-j ACCEPT

2. Redis Slave

: https://code.google.com/p/redis/downloads/list

[Email protected] ~]# cd/usr/local/src/[email protected] src]#TarZXF redis-2.6.Ten.Tar. Gz[[email protected] src]# CD Redis-2.6.Ten[[email protected] Redis-2.6.Ten]# Make; Make Install[[Email protected]~]#mkdir/usr/local/Redis[[email protected]~]# CD!$CD/usr/local/Redis[[email protected] redis]#CP.. /src/redis-2.6.Ten/redis.conf. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-CLI. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-server. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-Sentinel. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-Benchmark. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-check-aof. [Email protected] redis]#CP.. /src/redis-2.6.Ten/src/redis-check-dump. [Email protected] redis]#Echo "vm.overcommit_memory = 1">>/etc/Sysctl.conf[[email protected] redis]# sysctl vm.overcommit_memory=1[email protected] redis]#mkdirdata logs[[email protected] redis]# vim redis.confdaemonize yesport6379Timeout -loglevel Verbosepidfile/usr/local/redis/logs/Redis.pidlogfile/usr/local/redis/logs/Redis.log#requirepass redispassworddatabases -Save the 1Save - TenSave - 10000rdbcompression yesdbfilename Dump.rdbdir/usr/local/redis/data/appendonly noappendfilename appendonly.aofappendfsync everysecno-appendfsync-on-rewrite Yesauto-aof-rewrite-percentage -Auto-aof-rewrite-min-size 64mbmasterauth redispasswordslaveof192.168.1.88 6379

# # directly copy the Master configuration file, add the last two lines

[Email protected] ~]# redis-server/usr/local/redis/Redis.conf[[email protected]~]# NETSTAT-ANPT |grepredistcp0 0 0.0.0.0:6379 0.0.0.0:* LISTEN3442/redis-servertcp0 0 192.168.1.80:50799 192.168.1.88:6379Established3442/redis-server

# # You can see that the boot was successful and a connection to Master was established

ls /usr/local/redis/data/dump.rdb

# # Data Directory also has the master synchronization (although the main library does not write data)

Second, test Redis master-Slave

[Email protected] ~]# REDIS-CLI-a Redispasswordredis127.0.0.1:6379> Keys *(empty list or set) Redis127.0.0.1:6379>Set name Wangokredis127.0.0.1:6379> SetID 8Okredis127.0.0.1:6379> Keys *1)"ID"2)"name"Redis127.0.0.1:6379> Quit

# # This is the operation on master,-a specifies the authentication password

[Email protected] ~]# redis-Cliredis127.0.0.1:6379> Keys *1)"ID"2)"name"Redis127.0.0.1:6379>Get name"Wang"Redis127.0.0.1:6379> GetID"8"Redis127.0.0.1:6379> Setcc 123(Error) READONLY you can'T write against a read only slave.Redis127.0.0.1:6379> Quit

# # This is the action from the top, where slave does not have a password so do not specify, you can find the sync over the keys, because to maintain data consistency, so cannot write.
# # If the main execution del name is also removed from the name above.
# # Master-Slave synchronization complete!

Redis Master-Slave synchronization configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.