Install and configure the Redis server in CentOS

Source: Internet
Author: User

Install and configure the Redis server in CentOS

Note:

Operating System: CentOS

1. Install the compilation tool

Yum install wget make gcc-c ++ zlib-devel openssl-devel pcre-devel kernel keyutils patch perl

2. Install the tcl package (the installation of Redis requires tcl Support)

Download: http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz

Upload tcl8.6.1-src.tar.gz to the/usr/local/src directory.

Cd/usr/local/src # enter the package storage directory

Tar zxvf tcl8.6.1-src.tar.gz # Extract

Cd tcl8.6.1 # enter the installation directory

Cd unix

. /Configure -- prefix =/usr -- without-tzdata -- mandir =/usr/share/man $ ([$ (uname-m) = x86_64] & echo -- enable-64bit) # configure

Make # compile

Sed-e "s @ ^ \ (TCL_SRC_DIR = '\). * @ \ 1/usr/include '@ "-e"/TCL_ B/s @ =' \ (-L \)\?. * Unix @ = '\ 1/usr/lib @ "-I tclConfig. sh

Make install # install

Make install-private-headers

Ln-v-sf tclsh8.6/usr/bin/tclsh

Chmod-v 755/usr/lib/libtcl8.6.so

3. Install Redis

Download: http://download.redis.io/redis-stable.tar.gz

Upload redis-stable to the/usr/local/src directory

Cd/usr/local/src

Tar-zxvf redis-stable.tar.gz # Extract

Mv redis-stable/usr/local/redis # Move the file to the installation directory

Cd/usr/local/redis # enter the installation directory

Make # compile

Make install # install

Cd/usr/local/bin # Check whether the following files exist. If not, copy the following files to the/usr/local/bin directory.

Cd/usr/local/redis

Mkdir-p/usr/local/bin

Cp-p redis-server/usr/local/bin

Cp-p redis-benchmark/usr/local/bin

Cp-p redis-cli/usr/local/bin

Cp-p redis-check-dump/usr/local/bin

Cp-p redis-check-aof/usr/local/bin

Ln-s/usr/local/redis. conf/etc/redis. conf # Add a soft connection to the configuration file

Vi/etc/redis. conf # Edit

Daemonize yes # Set the background to start redis

: Wq! # Save and exit

Redis-server/etc/redis. conf # Start the redis Service

Redis-cli shutdown # disable redis

Vi/etc/sysctl. conf # edit and add the following code in the last line

Vm. overcommit_memory = 1

: Wq! # Save and exit

Sysctl-p # Make the settings take effect immediately

4. Set redis startup

Vi/etc/init. d/redis # edit and add the following code

#! /Bin/sh

# Chkconfig: 2345 90 10

# Description: Redis is a persistent key-value database

# Redis Startup script for redis processes

# Processname: redis

Redis_path = "/usr/local/bin/redis-server"

Redis_conf = "/etc/redis. conf"

Redis_pid = "/var/run/redis. pid"

# Source function library.

./Etc/rc. d/init. d/functions

[-X $ redis_path] | exit 0

RETVAL = 0

Prog = "redis"

# Start daemons.

Start (){

If [-e $ redis_pid-! -Z $ redis_pid]; then

Echo $ prog "already running ...."

Exit 1

Fi

Echo-n $ "Starting $ prog"

# Single instance for all caches

$ Redis_path $ redis_conf

RETVAL =$?

[$ RETVAL-eq 0] & {

Touch/var/lock/subsys/$ prog

Success $ "$ prog"

}

Echo

Return $ RETVAL

}

# Stop daemons.

Stop (){

Echo-n $ "Stopping $ prog"

Killproc-d 10 $ redis_path

Echo

[$ RETVAL = 0] & rm-f $ redis_pid/var/lock/subsys/$ prog

RETVAL =$?

Return $ RETVAL

}

# See how we were called.

Case "$1" in

Start)

Start

;;

Stop)

Stop

;;

Status)

Status $ prog

RETVAL =$?

;;

Restart)

Stop

Start

;;

Condrestart)

If test "x 'pidof redis '"! = X; then

Stop

Start

Fi

;;

*)

Echo $ "Usage: $0 {start | stop | status | restart | condrestart }"

Exit 1

Esac

Exit $ RETVAL

: Wq! # Save and exit

Chmod 755/etc/init. d/redis # Add the script execution permission

Chkconfig -- add redis # add enable startup

Chkconfig -- level 2345 redis on # Set the startup level

Chkconfig -- list redis # view the startup level

Service redis restart # restart redis

System O & M www.osyunwei.com reminder: qihang01 original content is copyrighted. For more information, see the source and original article links.

5. Set redis configuration file Parameters

Mkdir-p/usr/local/redis/var # create a directory for storing redis Databases

Vi/etc/redis. conf # Edit

Daemonize yes # Run redis in daemon mode later

Pidfile "/var/run/redis. pid" # run on apsaradb for redis later. The default pid file path is/var/run/redis. pid.

Port 6379 # default port

Bind 127.0.0.1 # All ip addresses of the local machine are bound by default. To ensure security, you can only listen to intranet ip addresses.

Timeout 300 # client timeout settings, in seconds

Loglevel verbose # Set the log level. Four levels are supported: debug, notice, verbose, and warning.

Logfile stdout # log record method. The default value is standard output. logs does not write files and is output to an empty device/deb/null.

Logfile "/usr/local/redis/var/redis. log" # You can specify the log file path

Databases 16 # Number of enabled databases

Save 900 1

Save 300 10

Save 60 10000

Create a local database snapshot in the format of save **

1 write operation in 900 seconds

10 write operations performed within 300 seconds

10000 write operations performed within 60 seconds

Rdbcompression yes # enable lzf compression or set it to no

Dbfilename dump. rdb # Name of the local snapshot Database

Dir "/usr/local/redis/var/" # local snapshot database storage directory

Requirepass 123456 # Set the redis database connection password

Maxclients 10000 # maximum number of client connections at the same time, 0 is unlimited

Maxmemory 1024 MB # set the maximum memory used by redis. The value must be smaller than the physical memory.

Appendonly yes # Enable logging, equivalent to MySQL binlog

Appendfilename "appendonly. aof" # log file name. Note: it is not a directory path.

Appendfsync everysec # executes synchronization every second. Two other parameters always and no are generally set to everysec, which is equivalent to the write method of MySQL transaction logs.

: Wq! # Save and exit

Service redis restart # restart

6. Test the redis Database

Redis-cli-a 123456 # connect to the redis database. Note:-a is followed by the redis Database Password

Set name osyunwei.com # Write Data

Get name # Read data

Exit # exit the apsaradb for redis Console

Redis-benchmark-h 127.0.0.1-p 6379-c 1000-n 100000 #1000 concurrent connections and 100000 requests, test the performance of the redis server with 127.0.0.1 port 6379

7. Connect to the redis database through the php Program # Redis extension must be installed in php first

Redis database IP Address: 192.168.21.128

Port 6379

Password 123456

Test code:

<? Php

$ Redis = new Redis ();

$ Redis-> connect ('192. 168.21.128 ', 192 );

$ Redis-> auth ('123 ');

$ Redis-> select (1 );

$ Ret = $ redis-> set ('www .osyunwei.com ', 'osyunwei ');

Var_dump ($ ret );

$ AllKeys = $ redis-> keys ('*');

Print_r ($ allKeys );

?>

Save the above Code as test. php. The following page is displayed after it is opened.

Bool (true) Array ([0] => www.osyunwei.com)

Now, the installation and configuration of the Redis server in Linux are complete.

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.