Redis Installation and Configuration
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 (requires TCL support to install Redis)
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--enable-64bit)??? #配置
Make? #编译
Sed-e "[Email protected]^\ (tcl_src_dir= '). *@\1/usr/include ' @"? -E "/tcl_b/[email protected]=" \ (-l\) \?. *[email protected]= ' \1/usr/[email protected] "? -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. Installing 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 files to the/usr/local/bin directory
Cd/usr/local/redis
Mkdir-p/usr/local/redis/bin
Cp-p Redis-server/usr/local/redis/bin
Cp-p Redis-benchmark/usr/local/redis/bin
Cp-p Redis-cli/usr/local/redis/bin
Cp-p Redis-check-dump/usr/local/redis/bin
Cp-p Redis-check-aof/usr/local/redis/bin
Ln-s? /usr/local/redis/etc/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 to 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/redis/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"
# 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 $redis _path
Echo
[$RETVAL = 0] && rm-f $redis _pid/var/lock/subsys/$prog
Retval=$?
Return $RETVAL
}
# See 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
5. Setting Redis profile 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地址, for security, you can only listen to the intranet IP
Timeout #客户端超时设置, unit of seconds
LogLevel verbose??
#设置日志级别, four levels supported: Debug, notice, verbose, warning
LogFile stdout?? #日志记录方式, default is standard output, logs does not write file, output to empty device/deb/null
LogFile "/usr/local/redis/var/redis.log"? #可以指定日志文件路径
Databases 16?? #开启数据库的数量
Save 900 1
Save 300 10
Save 60 10000
Create a local database snapshot, format: Save * *
1 write operations in 900 seconds
10 write operations in 300 seconds
10,000 write operations in 60 seconds
Rdbcompression Yes #启用数据库lzf压缩, can also be set to No
Dbfilename Dump.rdb?? #本地快照数据库名称
Dir "/usr/local/redis/var/"??? #本地快照数据库存放目录
Requirepass 123456?? #设置redis数据库连接密码
MaxClients 10000? #同一时间最大客户端连接数, 0 is unrestricted
MaxMemory 1024MB? #设定redis最大使用内存, the value is less than the physical memory and must be set
AppendOnly yes?? #开启日志记录, the equivalent of MySQL Binlog
Appendfilename "Appendonly.aof"??? #日志文件名, note: Not a directory path
Appendfsync everysec? #每秒执行同步, there are two parameters always, no is generally set to everysec, the equivalent of the MySQL thing log write way
: wq!? #保存退出
Service Redis restart? #重启
6. Test Redis Database
Redis-cli-a 123456?? #连接redis数据库, note:-A followed by a Redis database password
Set name Wdd?? #写数据
Get name?? #读取数据
Exit? #退出redis数据库控制台
Redis-benchmark-h 127.0.0.1-p 6379-c 1000-n 100000?? #1000个并发连接, 100,000 requests, testing Redis server performance with a 127.0.0.1 port of 6379
7. Connect to Redis database via PHP program?? #php必须先安装Redis扩展
Redis Database ip:192.168.21.128
Port: 6379
Password: 123456
Test code:
<?php
$redis = new Redis ();
$redis->connect (' 192.168.131.138 ', 6379);
$redis->auth (' 123456 ');
$redis->select (1);
$ret = $redis->set (' www.taobao.com ', ' Yunwei ');
Var_dump ($ret);
$allKeys = $redis->keys (' * ');
Print_r ($allKeys);
?>
Save the above code as test.php, and the following page will appear after opening
BOOL (TRUE) Array ([0] = www.taobao.com)
Redis Installation and Configuration