Redis Master-slave replication

Source: Internet
Author: User
Tags redis server

Master configuration:

wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
Tar zxvf redis-2.4.5.tar.gz
CD redis-2.4.5
Make
Make Prefix=/usr/local/redis Install #设置redis服务目录
Mkdir/etc/redis #创建redis主配置文件目录
Mkdir-p/var/lib/redis
CP redis.conf/etc/redis/
cd/etc/redis/
CP redis.conf Redis.conf.bak
Vim/etc/redis/redis.conf

# # #Master config###
# # #General Configuration
Daemonize Yes #使用daemon way to run the program, default to non-daemon mode
Pidfile/var/run/redis.pid #pid文件位置
Port 6379 #使用默认端口
Timeout # time for client idle disconnect
LogLevel warning #日志记录级别, the default is notice, my side uses warning, is Convenient for monitoring logs. With warning, the log is generated only when an alarm occurs, which is convenient for monitoring the alarm by judging whether the log file is empty.
Logfile/var/log/redis.log #日志产生的位置
Databases #默认是0, that is, only 1 db, my side is set to 16, convenient for multiple applications to use The same Redis server. Use the Select N command to confirm the use of Redis db so that different applications will not have a problem even with the same key.

# # #下面是SNAPSHOTTING持久化方式的策略. In order to keep the data relatively safe, in the following settings, the more frequent the change, the more frequent the snapshotting, that is, the greater the pressure, the more resources you spend on persistence. So I chose the master-slave mode and turned off the snapshotting in master.
#save 1 #在900秒之内, Redis takes at least 1 modifications to Redis capture snapshot To disk
#save #在300秒之内, Redis takes at least 100 modifications and Redis catches fast Take a picture of the disk
#save 10000 #在60秒之内, Redis takes at least 10,000 modifications and Redis catches fast Take a picture of the disk
Rdbcompression Yes #使用压缩
Dbfilename Dump.rdb #SNAPSHOTTING的文件名
Dir./#SNAPSHOTTING文件的路径
# # #REPLICATION Settings,
#slaveof #如果这台机器是台redis Slave, you can open this setting 。 If I use Master-slave mode, I will turn off the snapshotting on master, so I can do it on the slave instead of persistence on master, which can greatly improve the master memory usage and system performance.
#slave-serve-stale-data Yes #如果slave cannot be synchronized with master and read
# # SECURITY Settings
#requirepass foobared #redis性能太好, it doesn't make much sense to use a passwd.
#rename-command flushall "" #可以用这种方式关掉非常危险的命令
# # #LIMIT Settings
# # #APPEND only MODE settings
AppendOnly no #不使用AOF, AoF is another way of persistence, and the reason I'm not using it is this Mode does not guarantee data availability in the event of server or disk corruption.
Appendfsync everysec
No-appendfsync-on-rewrite No
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB
# # #SLOW LOG settings
Slowlog-log-slower-than 10000 #如果操作时间大于0.001 sec, log slow log, this log is recorded in memory , you can use the REDIS-CLI slowlog get command to view
Maximum length of Slowlog-max-len #slow log
# # #VIRTUAL MEMORY settings
Vm-enabled no #不使用虚拟内存, in the Redis 2.4 version, the author has been very much advised not to use Vm.
Vm-swap-file/tmp/redis.swap
Vm-max-memory 0
Vm-page-size 32
Vm-pages 134217728
Vm-max-threads 4
# # #ADVANCED CONFIG settings, the following settings are mainly used to save memory, I did not modify them
Hash-max-zipmap-entries 512
Hash-max-zipmap-value 64
List-max-ziplist-entries 512
List-max-ziplist-value 64
Set-max-intset-entries 512
Zset-max-ziplist-entries 128
Zset-max-ziplist-value 64
activerehashing Yes
# # #INCLUDES settings, using the following configuration, you can configure some other settings, such as the configuration of slave
#include/path/to/local.conf
#include/path/to/other.conf
#include/opt/redis/etc/slave.conf

* * If the memory situation is very tense, you need to set the kernel parameters:
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf

The kernel parameters are described below:
The Overcommit_memory file specifies the kernel's policy for memory allocation, which can be 0, 1, 2.
0, indicates that the kernel will check for sufficient available memory to be used by the process, and if sufficient memory is available, the memory request is allowed; otherwise, the memory request fails and the error is returned to the application process.
1, which means that the kernel allows all physical memory to be allocated regardless of the current memory state.
2, which indicates that the kernel allows allocating more memory than the sum of all physical memory and swap space

Sysctl-p

/usr/local/redis/bin/redis-server/etc/redis/redis.conf #启动redis服务

[Email protected] redis]# NETSTAT-TANP | grep 6379
TCP 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 20862/redis-server
TCP 0 0 10.0.2.209:6379 10.0.2.210:41097 established 20862/redis-server

Slave configuration:

wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
Tar zxvf redis-2.4.5.tar.gz
CD redis-2.4.5
Make
Make Prefix=/usr/local/redis Install #设置redis服务目录
Mkdir/etc/redis #创建redis主配置文件目录
Mkdir-p/var/lib/redis
CP redis.conf/etc/redis/
cd/etc/redis/
CP redis.conf Redis.conf.bak
Vim/etc/redis/redis.conf
# # #Slave config###
slaveof 10.0.2.209 6379 #如果这台机器是台redis Slave, you can open this setting. If I use Master-slave mode, I will turn off the snapshotting on master, so I can do it on the slave instead of persistence on master, which can greatly improve the master memory usage and system performance.
Slave-serve-stale-data no #如果slave cannot sync with master, set to Slav e unreadable for easy monitoring of script discovery issues.
AppendOnly Yes #在slave上使用了AOF to ensure the availability of data.
/usr/local/webserver/redis/bin/redis-server/etc/redis/redis.conf & #后台启动redis服务
[Email protected] redis]# NETSTAT-TANP | grep Redis
TCP 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 14098/redis-server
TCP 0 0 127.0.0.1:6379 127.0.0.1:39689 established 14098/redis-server
TCP 0 0 10.0.2.210:41097 10.0.2.209:6379 established 14098/redis-server

Master:

/usr/local/webserver/redis/bin/redis-cli
Redis 127.0.0.1:6379> Set Hello Redis
Ok
Redis 127.0.0.1:6379> Get Hello
"Redis"
Redis 127.0.0.1:6379>

Slave:
/usr/local/webserver/redis/bin/redis-cli
Redis 127.0.0.1:6379> Get Hello
"Redis"
Redis 127.0.0.1:6379> Info
.
.
.
master_host:10.0.2.209
master_port:6379
Master_link_status:up

Close Service method

$ redis-cli Shutdown

#关闭指定端口的redis-server

$redis-cli-p Port shutdown

Redis Master-slave replication

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.