Redis database Installation and master-slave configuration in Linux

Source: Internet
Author: User
Tags 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 P ORT 6379 # Server Port Tcp-backlog 511 # </proc/sys/net/core/somaxconn bind 127.0.0.1 # Bind network interface 0 # Close the connection after a-client is 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 ke Y changed save # After the SEC (5 min) If at least keys changed save 10000 # after the SEC if at least 10000 K Eys changed stop-writes-on-bgsave-error Yes rdbcompression Yes # compress Dump.rdb Yes # check rdbchecksum Dump.rdb Name Dump.rdb # dump file name dir./# dump file path Slave-serve-stale-data Yes Slave-read-only Yes Repl-diskless-sync n o repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority requirepass www.z-dig.com # password for Redis When the client connect toThis server appendonly no # use AOF or RDB appendfilename ' appendonly.aof ' # aof file name Appendfsync everysec # Save Fre Quency no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 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 hash-max-ziplist-value list-max-ziplist-entries 512 List-max-ziplist-value set-max-intset-entries 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 25 6MB 64mb client-output-buffer-limit pubsub 32mb 8mb Hz 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 63 79-a www.z-dig.com 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 n the server since:1.0.0 group:server 127.0.0.1:6379> 127.0.0.1:6379> shutdown save not connected> not con Nected> get name could don't 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/loc al/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 637 9-a www.z-dig.com 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.z-dig.com shutdown Save [root@z-dig redis]# CP conf/redis.conf C onf/redis-1.conf [root@z-dig redis]# Grep-ev ' #|^$ ' conf/redis-1.conf daemonize Yes pidfile/var/run/redis-1.pid Port 16 379 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 10000 stop-writes-on-bgsave-error Yes Rdbcompression Yes rdbchecksum Yes Dbfilename dump-1. Rdb dir./slaveof 127.0.0.1 6379 masterauth www.z-dig.com slave-serve-stale-data Yes slave-read-only Yes repl-diskless-s Ync no Repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority requirepass www.z-dig.com no A Ppendfilename "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 hash-max-ziplist-value list-max-ziplist-entries 512 List-max-ziplist-value set-max-intset-entries 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 25 6MB 64mb client-output-buffer-limit pubsub 32mb 8mb Hz 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 4uipv4 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.z-dig.com get Name "Mr.zhou" [Root@z-dig redis]# redis-cli-h 12 7.0.0.1-p 16379-a www.z-dig.com 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.z-dig.com Info Replication # Replicatio n role:master connected_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.z-dig.com Info Replication # replication R Ole: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 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.z-dig.com get Age (nil) [Root@z-dig redis]#] [root@z-dig Red is]# redis-cli-h 127.0.0.1-p 16379-a www.z-dig.com set Age (Error) READONLY your can ' t write against a read only Slav E. [Root@z-dig redis]# [Root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.z-dig.com get age (nil) [Root@z-dig R edis]# [root@z-dig redis]# redis-cli-h 127.0.0.1-p 6379-a www.z-dig.com set age OK [Root@z-dig redis]# [root @z-dig redis]# redis-cli-h 127.0.0.1-p 16379-a www.z-dig.com get Age "a" [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.