How to install the configuration Redis server under Linux

Source: Internet
Author: User
Tags allkeys benchmark chmod mkdir openssl php and redis redis server

Description

Operating system: CentOS

1, install the compilation tool

Yum install wget make gcc gcc-c++ zlib-devel OpenSSL openssl-devel pcre-devel kernel keyutils Patch Perl

2, install TCL package (Installation Redis need TCL support)

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

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

CD/USR/LOCAL/SRC #进入软件包存放目录

Tar zxvf tcl8.6.1-src.tar.gz #解压

CD tcl8.6.1 #进入安装目录

CD UNIX

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

Make #编译

Sed-e "s@^" (tcl_src_dir= '). *@1/usr/include ' @ '-e '/tcl_b/s@= ' (-L)? *unix@= ' 1/usr/lib@ '-i tclconfig.sh

Make install #安装

Make Install-private-headers

Ln-v-SF Tclsh8.6/usr/bin/tclsh

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

3. Installation Redis

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

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

Cd/usr/local/src

TAR-ZXVF redis-stable.tar.gz #解压

MV Redis-stable/usr/local/redis #移动文件到安装目录

Cd/usr/local/redis #进入安装目录

Make #编译

Make install #安装

Cd/usr/local/bin #查看是否有下面文件, if not, copy the following file 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/redis.conf/etc/redis.conf #添加配置文件软连接

vi/etc/redis.conf #编辑

Daemonize Yes #设置后台启动redis

: wq! #保存退出

redis-server/etc/redis.conf #启动redis服务

REDIS-CLI shutdown #关闭redis

Vi/etc/sysctl.conf #编辑, add the following code on the last line

Vm.overcommit_memory = 1

: wq! #保存退出

Sysctl-p #使设置立即生效

4, set up Redis boot

Vi/etc/init.d/redis #编辑, 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-a!-Z $redis _pid];then

Echo $prog "already running ..."

Exit 1

Fi

Echo-n $ "Starting $prog"

# Single Instance to 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 $redis _path

Echo

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

Retval=$?

Return $RETVAL

}

# How we were called.

Case "$" 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: $ {Start|stop|status|restart|condrestart}"

Exit 1

Esac

Exit $RETVAL

: wq! #保存退出

chmod 755/etc/init.d/redis #添加脚本执行权限

Chkconfig--add Redis #添加开启启动

Chkconfig--level 2345 Redis on #设置启动级别

Chkconfig--list Redis #查看启动级别

Service Redis Restart #重新启动redis

System Yun-wei Www.111cn.net warm reminder: qihang01 original content copyright, reproduced please indicate the source and the original link

5, set Redis configuration file parameters

Mkdir-p/usr/local/redis/var #创建redis数据库存放目录

vi/etc/redis.conf #编辑

Daemonize Yes #以后台daemon方式运行redis

Pidfile "/var/run/redis.pid" #redis以后台运行, default PID file path/var/run/redis.pid

Port 6379 #默认端口

Bind 127.0.0.1 #默认绑定本机所有ip地址, in order to be safe, can only monitor intranet IP

Timeout #客户端超时设置, units for seconds

LogLevel verbose #设置日志级别, supports four levels:Debug, notice, verbose, warning

LogFile stdout #日志记录方式, the default is standard output, logs does not write files, output to the empty device/deb/null

LogFile "/usr/local/redis/var/redis.log" #可以指定日志文件路径

Databases #开启数据库的数量

Save 900 1

Save 300 10

Save 60 10000

Create a local database snapshot, format: Save * *

900 seconds to perform 1 write operations

300 seconds to perform 10 write operations

60 seconds to perform 10,000 write operations

Rdbcompression Yes #启用数据库lzf压缩, or it can be set to no

Dbfilename Dump.rdb #本地快照数据库名称

Dir "/usr/local/redis/var/" #本地快照数据库存放目录

Requirepass 123456 #设置redis数据库连接密码

MaxClients 10000 #同一时间最大客户端连接数, 0 for unrestricted

MaxMemory 1024MB #设定redis最大使用内存, the value is less than the physical memory, you must set the

AppendOnly Yes #开启日志记录, the equivalent of MySQL Binlog

Appendfilename "Appendonly.aof" #日志文件名, Note: not directory path

Appendfsync everysec #每秒执行同步, there are two parameters always, no is generally set to everysec, the equivalent of MySQL things log write way

: wq! #保存退出

Service Redis Restart #重启

6. Test Redis Database

Redis-cli-a 123456 #连接redis数据库, note:-A followed by Redis database password

Set name 111cn.net #写数据

Get name #读取数据

Exit #退出redis数据库控制台

Redis-benchmark-h 127.0.0.1-p 6379-c 1000-n 100000 #1000个并发连接, 100,000 requests to test 127.0.0.1 server performance for Redis Port 6379

7, through the PHP program to connect the Redis database #php必须先安装Redis扩展

Redis Database ip:192.168.21.128

Port: 6379

Password: 123456

Test code:

<?php

$redis = new Redis ();

$redis->connect ('192.168.21.128',6379);

$redis->auth ('123456');

$redis->select (1);

$ret = $redis->set (' www.111cn.net ', ' Osyunwei ');

Var_dump ($ret);

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

Print_r ($allKeys);

?>

Save the above code as test.php and the following page will appear when you open it

BOOL (TRUE) Array ([0] => www.111cn.net)

Original from: osyunwei.com

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.