Install and use redis

Source: Internet
Author: User
Tags redis server redis tutorial

Introduction
Redis is an open-source key-value database. Its value not only includes the basic string type, but also supports the list, set, sorted set, and hash types. Official redis Website:On the official redis website, the redis client is available in multiple languages. C # Tutorial: servicestack. redis tutorial.

1. Installation

 # Wget HTTP: //  Redis.googlecode.com/files/redis-2.6.10.tar.gz  # Tar Xzf redis- 2.6 . 10 . Tar  . GZ# CdRedis - 2.6 . 10  # Make  # Mkdir -P/usr/local/ Bin  # Cp Src/redis-server/usr/local/bin/ # Cp Src/redis-cli/usr/local/bin/ # Cp Src/redis-check-Aof/usr/local/bin/ # Cp Src/redis-check-dump/usr/local/bin/ # Cp Src/redis-benchmark/usr/local/bin/ # Mkdir -P/etc/redis/ # Cp Redis. CONF/etc/redis/

Possible problems during installation:

Zmalloc. O: In function 'zmalloc _ used_memory ':
[Solution]: Add cflags =-March = i686 at the beginning of SRC/makefile, edit opt in src/. Make-settings, and change it to OPT =-O2-March = i686

 # Redis makefile # copyright (c)  2009 Salvatore sanfilippo <antirez at gmail dot com> # This  File Is released under the BSD license, see the copying File  # The makefile composes the final final_cflags and final_ldflags using # What is needed  For Redis plus the standard cflags and ldflags passed. # However when building the dependencies (jemalloc, Lua, hiredis ,...) # cflags and ldflags are propagated to the dependencies, so to pass # flags only to be used when compiling / Linking redis itself redis_cflags # And redis_ldflags are used instead (this is  Case Of '  Make gcov  '  ). ## Dependencies are stored  In The makefile. Dep File . To rebuild this File # Just use  '  Make Dep  '  , But this is only needed by developers.Cflags  =-March =  I686Release_hdr: = $ (Shell Sh -C '  ./Mkreleasehdr. Sh  '  ) Uname_s: = $ (Shell Sh -C'  Uname-S 2>/dev/null | ECHO not  '  ) Optimization ? =- O2dependency_targets = Hiredis linenoise Lua # default settingsstd =-STD = c99-pedantic
 STD =-STD = c99- pedanticwarn  =- wallopt  =-O2 -March =    i686  malloc  =  jemalloccflags  =  ldflags  =  redis_cflags  =  redis_ldflags  =  prev_final_cflags  =-STD = c99-pedantic-wall-O2-g-rdynamic-ggdb-I .. /deps/ hiredis -I .. /deps/linenoise-I .. /deps/Lua/src-duse_jemalloc-I .. /deps/jemalloc/ includeprev_final_ldflags  =-g-rdynamic-ggdb 

You need 'tclsh8. 5' in order to run the redis Test
[Solution]: because Tcl is not installed or the installed version is incorrect, pressHttp://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.htmlthe latest installation license is provided.

2. Configuration File
Create a user and log file directory, and then modify the configuration file.

 
# Useradd redis #Mkdir-P/var/lib/Redis #Mkdir-P/var/log/Redis #ChownRedis. redis/var/lib/Redis #ChownRedis. redis/var/log/redis

Modify the configuration file, set the process to run in the background, bind the Host IP address, set the listening port, and set the database storage path.

# Whether to run as a daemon. The default value is no.Daemonize Yes# If a later process runs, you must specify a PID. The default value is /Var/run/ Redis. pidpidfile/var/run/redis. pid # bind the Host IP address. The default value is 127.  0.0 . 1  Bind 127.0.0.1  # Redis default listening port  6379  # How many seconds after the client is idle, disconnect. The default value is 300 (seconds) Timeout.  300  # Log record level, which has four optional values: Debug, verbose (default), notice, and warningloglevel verbose # specify the log output file name. The default value is stdout. It can also be set /Dev/Null shield logsLogfile/var/log/redis/redislog# Number of available databases; default value: 16; default value: 0 Databases  16  # Policy for saving data to disk # If one of the keys data is changed, refresh the data to disk once in 900 seconds. Save  900   1  # When 10 pieces of keys data are changed, refresh to disk once in 300 seconds to save  300   10  # When one million pieces of keys data are changed, the data is refreshed to disk once every 60 seconds. Save  60   10000  # Whether to compress the Data Object rdbcompression yes when using the dump. RDB database # The local database file name. The default value is dump. rdbfilename dump. RDB # The local database storage path. The default value is. /DIR/usr/lib/redis  ########### Replication ###################### redis replication configuration # slaveof <Masterip> <masterport> When the local machine is a slave service, set the master service IP address and port # masterauth <Master-Password> When the local machine is a slave service, set the master service connection password # requirepass foobared # the maximum number of client connections, which is unlimited by default # maxclients  128  # Maximum memory usage settings. When the maximum memory is reached, redis will first try to clear expired or expiring keys. After this method is processed, any key that has reached the maximum memory settings will be cleared, no more write operations can be performed. # Maxmemory <Bytes>
Maxapsaradb for memory 1024000000# Whether to record logs after each update operation. If not enabled, data may be lost for a period of time during power failure. Because redis synchronizes data files according to the Save conditions above, some data will only exist in the memory for a period of time. The default value is noappendonly no # Update log file name. The default value is appendonly. aof # appendfilename # Update log conditions. There are three optional values. "No" indicates that data is cached and synchronized to the disk by the operating system. "Always" indicates that data is manually written to the disk by calling fsync () after each update operation. "everysec" indicates that data is synchronized once per second (default ). # Appendfsync alwaysappendfsync everysec # appendfsync no ################# virtual memory ############ whether to enable VM function, the default value is novm. - Enabled no # VM - Enabled yes # virtual memory file path, default value: /Tmp/ Redis. Swap, which cannot be shared by multiple redis instances -Swap- File Logs/ Redis. Swap # Replace all -Max-memory data is stored in the virtual memory. No matter how small the VM-max-memory settings are, all index data is stored in the memory (redis index data is keys). That is to say, when VM-max- When memory is set to 0, all values exist on the disk. The default value is 0. VM -Max-memory 0 VM -Page-size 32 VM -Pages 134217728 VM -Max-threads 4 ############ Advanced config ################ glueoutputbuf yeshash -Max-zipmap-entries 64 Hash -Max-zipmap-Value512 # Reset hash table activerehashing Yes Note: The official redis documentation provides some suggestions for VM usage: ** When your key is very small and the value is very large, it will be better to use the VM, because the memory saved is relatively large. ** When your key is not hour, you can consider using some very method to convert a large key into a large value. For example, you can consider combining the key and value into a new value. ** It is best to use Linux ext3 and other file systems that support sparse files to save your swap files. ** The VM-max-threads parameter can be used to set the number of threads accessing the swap file. It is recommended that the number of threads accessing the swap file not exceed the number of machine cores. if it is set to 0, all operations on swap files are serial. it may cause a long delay, but it guarantees data integrity.

after modifying the configuration file, you can start redis, #/usr/local/bin/redis-server/etc/redis. conf. Possible problems:
warning overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl VM. overcommit_memory = 1' for this to take effect.
[solution]: Type sysctl VM. overcommit_memory = 1
This file specifies the kernel's memory allocation policy. The value can be 0, 1, or 2.
0 indicates that the kernel checks whether there is sufficient available memory for use by the process. If there is sufficient available memory, the memory application is allowed. Otherwise, the memory application failed and the error was returned to the application process.
1 indicates that the kernel allows all physical memories to be allocated regardless of the current memory status.
2, indicating that the kernel can allocate more memory than the total physical memory and swap space
warning: 32 bit instance detected but no memory limit set. setting 3.5 GB maxmemory limit with 'noeviction 'policy now.
[solution]: modify the configuration file redis. conf, set maxmemory to maxmemory 1024000000 # allocate MB of memory

3. Configure the init script to run automatically upon startup

Before creating the redis STARTUP script, you must configure the kernel parameters. Otherwise, an error will be reported when you restart or stop redis, and data cannot be automatically synchronized to the disk before stopping the service:
# Vi/etc/sysctl. conf, Adds a line of VM. overcommit_memory = 1, Save and exit: WQTo make settingsThe application takes effect,# Sysctl-P
Create a redis STARTUP script:
# Vi/etc/init. d/redis

#! /Bin/ Bash ## init  File   For  Redis ## chkconfig: - 80   12 # Description: redis daemon # processname: redis # config: /Etc/redis/ Redis. conf # pidfile: /Var/run/ Redis. PID Source /Etc/init. d/ Functions # Bin = "  /Usr/local/bin  "  Bin = "  /Usr/local/bin  "  Config = " /Etc/redis. conf  "  Pidfile = "  /Var/run/redis. PID  "  ### Read configuration [ -R "  $ Sysconfig  " ] & Source "  $ Sysconfig  "  Retval = 0 Prog = "  Redis-Server  "  Desc = "  Redis Server  "  Start (){  If [-E $ pidfile]; Then                Echo   "  $ DESC already running ....  "  Exit 1           Fi           Echo -N $ "  Starting $ Desc:  "  Daemon $ Bin / $ Prog $ config retval = $? Echo  [$ Retval -EQ 0 ] & Touch /Var/lock/subsys/ $ Prog return $ retval} STOP (){ Echo -N $ "  Stop $ Desc:  "  Killproc $ prog retval = $? Echo  [$ Retval -EQ 0 ] & Rm -F/var/lock/subsys/ $ Prog $ pidfile return $ retval} restart () {stop start}  Case   "  $1 "   In  Start) Start; stop) Stop; restart) restart; condrestart )[ -E/var/lock/subsys/$ prog] & Restart retval = $? ; Status) Status $ prog retval = $? ;; * )  Echo $ "  Usage: $0 {START | stop | restart | condrestart | status}  " Retval = 1   Esac  Exit $ retval
 
Set auto-start when the service is started:
 
# Chmod 755/Etc/init. d/Redis # chkconfig--Add redis # chkconfig-- Level345Redis on # chkconfig-- List redis

Possible problems:
Use the client tool (redis-cli)There is no problem in the local connection test, but the remote connection fails. After looking for reasons for half a day, we found that SELinux and the firewall had blocked the service. If it was just for development and testing, we recommend that you disable these two items to avoid some inexplicable problems.

 
 

 

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.