A tutorial on Redis installation and master-slave configuration in Linux

Source: Internet
Author: User
Tags hash lua redis server port truncated redis cluster install redis

Redis is an open source, support network, memory based, key value pair storage database. The main difference from other non relational databases is that the types of values in Redis are not limited to strings (Strings), but also the following abstract data types: (list) Lists, (set) Sets, (ordered set) Sorted Sets, (hash) hashes. Redis through the RDB, aof two ways to achieve the persistence of data.

Install Redis

Redis Source Package Save path/USR/LOCAL/SRC

Redis Software Installation Path/usr/local/redis

Install TCL before compiling the Redis, and you will be prompted for errors in the make test phase if not installed.

[Root@z-dig ~]# yum-q-y install Tcl
[Root@z-dig ~]# cd/usr/local/src/
[Root@z-dig src]# wget http://download.redis.io/releases/redis-3.0.5.tar.gz
[Root@z-dig src]# Tar XF redis-3.0.5.tar.gz
[Root@z-dig src]# CD redis-3.0.5
[Root@z-dig redis-3.0.5]# make
[Root@z-dig redis-3.0.5]# make Test
...
\o/all tests passed without errors!

Cleanup:may take some time ... Ok
MAKE[1]: Leaving directory '/USR/LOCAL/SRC/REDIS-3.0.5/SRC '
[Root@z-dig redis-3.0.5]# make prefix=/usr/local/redis-3.0.5 Install
[Root@z-dig redis-3.0.5]# Mkdir/usr/local/redis-3.0.5/conf-p
[Root@z-dig redis-3.0.5]# ln-s/usr/local/redis-3.0.5//usr/local/redis
[Root@z-dig redis-3.0.5]# CP redis.conf/usr/local/redis/conf/
[Root@z-dig redis-3.0.5]# Cd/usr/local/redis
[Root@z-dig redis]# pwd
/usr/local/redis
[Root@z-dig redis]# tree.
.
├──bin
│├──redis-benchmark
│├──redis-check-aof
│├──redis-check-dump
│├──redis-cli
│└──redis-server
└──conf
└──redis.conf

2 directories, 6 files
[Root@z-dig redis]#
To this redis has been installed in/usr/local/redis

Modify configuration file

Because the following configuration has been added after some configuration items, it cannot be used directly, and subsequent annotations need to be removed when used.

[Root@z-dig redis]# Grep-ev ' #|^$ ' conf/redis.conf
Daemonize Yes #run as a daemon
PIDFILE/VAR/RUN/REDIS.PID # PID File
Port 6379 # Server Port
Tcp-backlog 511 # </proc/sys/net/core/somaxconn
Bind 127.0.0.1 # Bind network interface
Timeout 0 # Close the connection after a client are idle for N seconds
Tcp-keepalive 0
LogLevel Warning # verbosity level
LogFile "/var/log/redis.log"
Databases # number of databases Begain with 0
Save 900 1 # After 900 sec (min) If at least 1 key changed
Save # After the SEC (5 min) If at least keys changed
Save 10000 # After the SEC if at least 10000 keys changed
Stop-writes-on-bgsave-error Yes
Rdbcompression Yes # compress Dump.rdb
Rdbchecksum Yes # check DUMP.RDB
Dbfilename Dump.rdb # dump file name
Dir./# Dump file path
Slave-serve-stale-data Yes
Slave-read-only Yes
Repl-diskless-sync No
Repl-diskless-sync-delay 5
Repl-disable-tcp-nodelay No
Slave-priority 100
Requirepass www.111cn.net # Password for Redis as client connect to this server
AppendOnly No # use AOF or RDB
Appendfilename "appendonly.aof" # aof file name
Appendfsync Everysec # Save Frequency
No-appendfsync-on-rewrite No
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB
aof-load-truncated Yes
Lua-time-limit 5000
Slowlog-log-slower-than 10000
Slowlog-max-len 128
Latency-monitor-threshold 0
Notify-keyspace-events ""
Hash-max-ziplist-entries 512
Hash-max-ziplist-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
Hll-sparse-max-bytes 3000
activerehashing Yes
Client-output-buffer-limit Normal 0 0 0
Client-output-buffer-limit slave 256MB 64MB 60
Client-output-buffer-limit pubsub 32MB 8MB 60
Hz 10
Aof-rewrite-incremental-fsync Yes
[Root@z-dig redis]#
Adjust kernel parameters

[Root@z-dig redis]# echo >/proc/sys/net/core/somaxconn
Start Redis

[Root@z-dig redis]#/usr/local/redis/bin/redis-server/usr/local/redis/conf/redis.conf
[Root@z-dig redis]# lsof-i:6379
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Redis-ser 20504 Root 4u IPv4 139391 0t0 TCP localhost:6379 (LISTEN)
[Root@z-dig redis]#
Test

[Root@z-dig redis]# ln-s/usr/local/redis/bin/redis-cli/usr/local/sbin/
[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net
127.0.0.1:6379> Set name Mr.zhou
Ok
127.0.0.1:6379> Get Name
"Mr.zhou"
127.0.0.1:6379>
127.0.0.1:6379> Help shutdown

SHUTDOWN [Nosave] [SAVE]
summary:synchronously Save the dataset to disk and then shut down the server
since:1.0.0
Group:server

127.0.0.1:6379>
127.0.0.1:6379> Shutdown Save
Not connected>
Not connected> get name
Could not connect to Redis at 127.0.0.1:6379:connection refused
Not connected>
Not connected> quit
[Root@z-dig redis]# lsof-i:6379
[Root@z-dig redis]#
[Root@z-dig redis]#/usr/local/redis/bin/redis-server/usr/local/redis/conf/redis.conf
[Root@z-dig redis]# lsof-i:6379
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Redis-ser 20675 Root 4u IPv4 143036 0t0 TCP localhost:6379 (LISTEN)
[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net get Name
"Mr.zhou"
[Root@z-dig redis]#
The basic test is complete. Configure tuning according to the actual environment.

Multi-instance master and slave configuration

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net Save
[Root@z-dig redis]# CP conf/redis.conf conf/redis-1.conf
[Root@z-dig redis]# Grep-ev ' #|^$ ' conf/redis-1.conf
Daemonize Yes
Pidfile/var/run/redis-1.pid
Port 16379
Tcp-backlog 511
Bind 127.0.0.1
Timeout 0
Tcp-keepalive 0
LogLevel Warning
LogFile "/var/log/redis-1.log"
Databases 16
Save 900 1
Save 300 10
Save 60 10000
Stop-writes-on-bgsave-error Yes
Rdbcompression Yes
Rdbchecksum Yes
Dbfilename Dump-1.rdb
Dir./
slaveof 127.0.0.1 6379
Masterauth www.111cn.net
Slave-serve-stale-data Yes
Slave-read-only Yes
Repl-diskless-sync No
Repl-diskless-sync-delay 5
Repl-disable-tcp-nodelay No
Slave-priority 100
Requirepass www.111cn.net
AppendOnly No
Appendfilename "Appendonly.aof"
Appendfsync everysec
No-appendfsync-on-rewrite No
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB
aof-load-truncated Yes
Lua-time-limit 5000
Slowlog-log-slower-than 10000
Slowlog-max-len 128
Latency-monitor-threshold 0
Notify-keyspace-events ""
Hash-max-ziplist-entries 512
Hash-max-ziplist-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
Hll-sparse-max-bytes 3000
activerehashing Yes
Client-output-buffer-limit Normal 0 0 0
Client-output-buffer-limit slave 256MB 64MB 60
Client-output-buffer-limit pubsub 32MB 8MB 60
Hz 10
Aof-rewrite-incremental-fsync Yes
[Root@z-dig redis]#
Start two instances

[Root@z-dig redis]#/usr/local/redis/bin/redis-server/usr/local/redis/conf/redis.conf
[Root@z-dig redis]#/usr/local/redis/bin/redis-server/usr/local/redis/conf/redis-1.conf
[Root@z-dig redis]# lsof-i:6379
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Redis-ser 20930 Root 4u IPv4 145992 0t0 TCP localhost:6379 (LISTEN)
Redis-ser 20930 root 5u IPv4 146031 0t0 TCP localhost:6379->localhost:39445 (established)
Redis-ser 20938 root 5u IPv4 146029 0t0 TCP localhost:39445->localhost:6379 (established)
[Root@z-dig redis]# lsof-i:16379
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Redis-ser 20938 Root 4u IPv4 146024 0t0 TCP localhost:16379 (LISTEN)
[Root@z-dig redis]#
[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net get Name
"Mr.zhou"
[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.111cn.net get Name
"Mr.zhou"
[Root@z-dig redis]#
Test Master

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net Info Replication
# replication
role:masterconnected_slaves:1
Slave0:ip=127.0.0.1,port=16379,state=online,offset=337,lag=1
master_repl_offset:337
Repl_backlog_active:1
repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
Repl_backlog_ histlen:336
[Root@z-dig redis]#

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.111cn.net Info replication
# Replication
Role:slave
master_host:127.0.0.1
master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:351
slave_priority:100
Slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[Root@z-dig redis]#
[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.111cn.net get Age
(nil)
[Root@z-dig redis]#

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.111cn.net set age 23
(Error) READONLY can ' t write against a read only slave.
[Root@z-dig redis]#

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net get Age
(nil)
[Root@z-dig redis]#

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.111cn.net set age 23
Ok
[Root@z-dig redis]#

[Root@z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.111cn.net get Age
"23"
[Root@z-dig redis]#
Multiple instances of master and subordinate have been configured to complete and test successfully. In the configuration process, you need to be aware that the ports, PID files, and dump files of each instance cannot be the same. If password access is configured, be aware that the appropriate password configuration is available from the instance. Whether the library is read-only, and so on.

There are also many advanced configuration items in the configuration file that can be tuned for configuration and make up the Redis cluster. RDB, AOF can be configured according to the actual situation. The latter will be introduced.



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.