Install and configure redis on the first day

Source: Internet
Author: User
Tags redis server

I. redis Introduction

Redis is a high-performance Key-value database. The emergence of redis largely compensates for the shortage of keyValue storage such as memcached, and can play a good complementary role in relational databases in some cases. It is similar to memcached, but data can be persistent and supports a wide range of data types. There are strings, linked lists, sets, and ordered sets. Allows you to compute, merge, and merge sets (difference) on the server side. It also supports multiple sorting functions. So redis can also be seen as a data structure server.

All redis data is stored in the memory and then asynchronously stored to the disk (this is called the "semi-persistent mode "); you can also write every data change to an append only file (AOF) (this is called "Full persistence mode "). It provides python, Ruby, Erlang, and PHP clients for ease of use. The problem is that this project is still quite new and may not be stable enough, and there are no actual instances for large-scale system applications. In addition, the lack of batch get in MC is also a big problem. The network overhead of batch get is different from that of multiple get.


Ii. redis Installation

Official website address: http://www.redis.io/

Redis: http://www.redis.io/download


1. Compile and install

# wget http://download.redis.io/releases/redis-2.8.17.tar.gz# tar xf redis-2.8.17.tar.gz # cd redis-2.8.17# make# make test # make install
[[Email protected] redis-2.8.17] # Make testcd SRC & make testmake [1]: entering directory '/root/redis/redis-2.8.17/src' You need TCL 8.5 or newer in order to run the redis testmake [1]: * ** [test] Error 1 make [1]: Leaving directory '/root/redis/redis-2.8.17/src 'make: *** [test] Error 2 work und: tar xf tcl8.6.2-src.tar.gz # cd tcl8.6.2 # export srcdir = 'pwd' & cd unix &&. /configure -- prefix =/usr -- without-tzdata -- Mandir =/usr/share/man $ ([$ (uname-m) = x86_64] & Echo -- enable-64bit) & make & sed-e "s # $ srcdir/Unix #/usr/lib #"-e "s # $ srcdir #/usr/include #"-I tclconfig. sh & sed-e "s # $ srcdir/Unix/pkgs/tdbc1.0.1 #/usr/lib/tdbc1.0.0 #"-e "s # $ srcdir/pkgs/tdbc1.0.1/generic # /usr/include # "-e" s # $ srcdir/pkgs/tdbc1.0.1/library #/usr/lib/tcl8.6 # "-e" s # $ srcdir/pkgs/tdbc1.0.1 # /usr/include # "-I pkgs/tdbc1.0.1/tdbcconfig. sh & sed-e "s # $ srcdir/Unix/pkgs/itcl4.0.1 #/usr/lib/itcl4.0.0 #"-e "s # $ srcdir/pkgs/itcl4.0.1/generic # /usr/include # "-e" s # $ srcdir/pkgs/itcl4.0.1 #/usr/include # "-I pkgs/itcl4.0.1/itclconfig. sh & unset srcdir # make install & make install-private-headers & ln-v-SF tclsh8.6/usr/bin/tclsh & chmod-V 755/usr/lib /libtcl8.6.so # mkdir-v-P/usr/share/doc/tcl-8.6.2 & CP-v-R .. /html/*/usr/share/doc/tcl-8.6.2 //////////////////////////// //////////////////////////////////////// ////////////////////////////

2. Copy the redis configuration file

# mkdir /etc/redis# cp /root/redis/redis-2.8.17/redis.conf /etc/redis/


3. Create a redis service to start users

# useradd -s /sbin/nologin redis

4. simple modification of common configuration options

# Vim/etc/redis. conf daemonize yes # redis runs in the daemprocess mode. No indicates not running in the daemprocess mode (a terminal will be occupied) Timeout 300 # How long after the client is idle, disconnect, the default value is 0. disable this function loglevel verbose # Set the redis Log Level logfile stdout # Set the log file output mode. If you run redis as a daemon and set the log output to stdout, then the log information is output to/dev/null.

5. Provide the redis sysv script

Note: before using this script for management, you must configure the following kernel parameters. Otherwise, an error will be reported when the redis script restarts or stops redis, data cannot be automatically synchronized to the disk before the service is stopped.

# Echo "VM. overcommit_memory = 1 ">/etc/sysctl. conf the value available for this parameter is 0, 1, and 20 indicates that when the user space requests more memory, the kernel tries to estimate the available memory. 1 indicates that the kernel allows excessive memory usage until the memory is used up. 2 indicates that the entire memory address space cannot exceed swap + (VM. overcommit_ratio) % Ram value # sysctl-P # effective immediately

# Vim/etc/init. d/redis

#!/bin/bash # # Init file for redis # # chkconfig: - 80 12 # description: redis daemon # # processname: redis # config: /etc/redis.conf # pidfile: /var/run/redis.pid source /etc/init.d/functions#BIN="/usr/local/bin" BIN="/usr/local/bin"CONFIG="/etc/redis/redis.conf"PIDFILE="/var/run/redis.pid"### Read configuration [ -r "$SYSCONFIG" ] && source "$SYSCONFIG"RETVAL=0prog="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=1esacexit $RETVAL

Add to service list and set startup

# chmod +x /etc/init.d/redis # chkconfig --add redis# chkconfig --level 345 redis on

6. Start the redis Service

# redis-server# redis-server /etc/redis/redis.conf &# service redis start

7. view the address and port of the redis service listener

# netstat -tnlp |grep redistcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      25350/redis-server


Iii. Test the redis service on the client

Method 1: Run redis-cli # redis-cli-H 192.168.1.127-P 62.16127.0.0.1: 6379> Set Name zhengyanshengok127.0.0.1 on the redis client: 6379> get name "zhengyansheng" 127.0.0.1: 6379> quit # redis-cli set mykey 'Hello world! 'OK # redis-cli get mykey "Hello world! "# Redis-cli type mykeystring # redis-cli strlen mykey (integer) 12
Method 2: Telnet # telnet 127.0.0.1 6379 trying 127.0.0.1... connected to 127.0.0.1.escape character is '^]'. set test "Welcome to Beijing. "+ Okget test $19 welcome to Beijing. strlen test: 19 quit + okconnection closed by foreign host.



Iv. phpredis extension Installation

1. Download The phpredis extension package

wget https://github.com/nicolasff/phpredis/archive/master.zip

2. Install phpredis Extension

# unzip phpredis-master.zip # cd phpredis-master# /usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make # make install
# After the installation is complete, the output information, prompt redis. So generated directory path installing shared extensions:/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20100525/

3. Add redis Extension

# Vim/usr/local/PHP/etc/PHP. ini add a line extension = "redis. So"

4. Restart the nginx service.

# service nginx restart

5. Access phpinfo to view details about redis extensions.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/7E/wKiom1Q-gEeB_aOQAABpXOd0WbI218.jpg "Title =" 1.png" alt = "wKiom1Q-gEeB_aOQAABpXOd0WbI218.jpg"/>

Iv. redis User Authentication

Redis does not implement access control, but it provides a lightweight authentication method. You can edit the redis. conf configuration to enable authentication.

There are two ways to authenticate redis users

1. Temporary user authentication Password

2. Permanent user authentication Password

Temporarily set redis user authentication. After the redis service is restarted, it will become invalid [[email protected] ~] # Redis-cli 127.0.0.1: 6379> config set requirepass zhengyanshengok127.0.0.1: 6379> config get requirepass (error) noauth authentication required.127.0.0.1: 6379> auth authentication: 6379> config get requirepass1) "requirepass" 2) "zhengyansheng" 127.0.0.1: 6379> quit # restart the redis service [[email protected] ~] # Service redis restartstop redis server: [OK] Starting redis server: [OK] # Do I Have To authenticate again? [[Email protected] ~] # Redis-cli127.0.0.1: 6379> Set Name tomcatok127.0.0.1: 6379> get name "Tomcat" 127.0.0.1: 6379> quit
Set a redis user permanently. It will still take effect after redis service is restarted. 1. Set the redis password # Vim/etc/redis. conf requirepass zhengyansheng132600719872, reload redis service # service redis restart3, log on to redis for access testing # redis-cli 127.0.0.1: 6379> Set Name zhengyansheng (error) noauth authentication required. # error: failed to pass 127.0.0.1: 6379> auth authentication # ok127.0.0.1: 6379> Set Name zhengyanshengok127.0.0.1: 6379> get name "zhengyansheng" 127.0.0.1: 6379> quit


V. Comparison with memcache

Memcache and redis are both memory-type databases. data is stored in the memory and directly accessed through TCP. memcached has the advantages of fast speed and high concurrency. The disadvantage is that the data type is limited and the query function is not strong, it is generally used as a cache. In our team's project, we started with memcached and later replaced it with redis.

Compared with memcached:

1. redis has a persistence mechanism that regularly persists data in the memory to the hard disk.

2. redis has the BINLOG function to write all operations into logs. When redis fails, data can be restored according to BINLOG.

3. redis supports Virtual Memory, which can limit the memory usage. When the data exceeds the threshold, the least common data in the memory is saved to the page file of the hard disk using an LRU-like algorithm.

4. apsaradb for redis supports more data types and has a larger space for imagination.

5. The consistent hash mentioned by a friend above is used in redis sharding. It is generally used when the load is very high and requires horizontal scaling. We haven't used this function yet. In general projects, a single machine is enough to support concurrency. Redis 3.0 will launch cluster with more powerful functions



Vi. redis-clie command line parameters

Set to create a key; get to get the value of a key; del *** a key; type to get the type of a key; exists to determine whether a key exists, 0: Yes, 1, does not exist; keys gets the key of the given fuzzy match; expire sets the number of seconds for a key to expire; perstst *** sets the number of seconds for a key to expire; pexpire sets the number of milliseconds for a key to expire; rename: rename a key. renamenx renames a key, and the new key must not exist. TTL obtains the key validity period;


This article from "Zheng Yansheng" blog, please be sure to keep this source http://467754239.blog.51cto.com/4878013/1564581

Install and configure redis on the first day

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.