您的位置:首页 > 数据库 > Redis

CentOS 6上的redis搭建实战记录

2013-06-04 14:23 357 查看
redis 是一个基于内存的高性能key-value数据库,数据都保存在内存中定期刷新到磁盘,以极高的读写效率而备受关注。他的特点是支持各种数据结构,stirng,hashes,
list,set,和sorted sets

1、下载安装

wget http://download.redis.io/redis-stable.tar.gz tar -zxvf redis-stable.tar.gz
cd redis-stable
make
make test 检查一下是否正常,遇到2个错误
[root@localhost redis-stable]# make test

cd src && make test

make[1]: Entering directory `/usr/local/src/redis-stable/src'

which: no tclsh8.5 in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/geffzhang/bin)

You need 'tclsh8.5' in order to run the Redis test

make[1]: *** [test] 错误 1

make[1]: Leaving directory `/usr/local/src/redis-stable/src'

make: *** [test] 错误 2

[root@localhost redis-stable]#
没安装tcl
按照官网http://www.linuxfromscratch.org/blfs/view/cvs/general/tcl.html 上的安装
make install
mkdir -p /usr/local/bin

cp -pf redis-server /usr/local/bin

cp -pf redis-benchmark /usr/local/bin

cp -pf redis-cli /usr/local/bin

cp -pf redis-check-dump /usr/local/bin

cp -pf redis-check-aof /usr/local/bin

make[1]: Leaving directory `/usr/local/src/redis-stable/src'

[root@localhost redis-stable]#
好了,现在redis就安装成功了
Redis 由四个可执行文件:redis-benchmarkredis-cliredis-serverredis-stat这四个文件,加上一个redis.conf就构成了整个redis的最终可用包。它们的作用如下:

redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况

现在就可以启动redis了,redis只有一个启动参数,就是他的配置文件路径。

redis-server /etc/redis.conf

注意,默认复制过去的redis.conf文件的daemonize参数为no,所以redis不会在后台运行,这时要测试,我们需要重新开一个终端。修改为yes则为后台运行redis。另外配置文件中规定了pid文件,log文件和数据文件的地址,如果有需要先修改,默认log信息定向到stdout.

下面是redis.conf的主要配置参数的意义:

daemonize:是否以后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)

这时你可以打开一个终端进行测试了,配置文件中默认的监听端口是6379

2、建立用户和日志目录

第一次启动时建议为Redis建立用户和日志目录

[root@localhost redis-stable]# useradd redis

[root@localhost redis-stable]# mkdir -p /var/lib/redis

#db文件放在这里,需要修改redis.conf

# The working directory.

#

# The DB will be written inside this directory, with the filename specified

# above using the 'dbfilename' configuration directive.

#

# Also the Append Only File will be created inside this directory.

#

# Note that you must specify a directory here, not a file name.

dir /var/lib/redis

[root@localhost redis-stable]# mkdir -p /var/log/redis

# Specify the log file name. Also 'stdout' can be used to force

# Redis to log on the standard output. Note that if you use standard

# output for logging but daemonize, logs will be sent to /dev/null

logfile /var/log/redis/redislog

[root@localhost redis-stable]# chown redis.redis /var/lib/redis

[root@localhost redis-stable]# chown redis.redis /var/log/redis

3、配置Init脚本

Redis管理脚本基于Ubuntu 的发行版上的,Ubuntu的可以看这篇文章ubuntu安装启动redis,在Centos
linux 上并不能用,下面有个脚本可以用于CentOS 。

用这个脚本管理之前,需要先配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上:

# vi /etc/sysctl.conf

vm.overcommit_memory = 1

然后应用生效:

# sysctl –p

建立redis启动脚本:

# 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.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

然后增加服务并开机自启动:

# chmod 755 /etc/init.d/redis

# chkconfig --add redis

# chkconfig --level 345 redis on

# chkconfig --list redis

[root@localhost redis-stable]# service redis start

Starting Redis Server: [确定]

[root@localhost redis-stable]# cat /var/log/redis/redislog

[14250] 14 Jul 22:23:15 * Server started, Redis version 2.4.15

[14250] 14 Jul 22:23:15 * The server is now ready to accept connections on port 6379

[14250] 14 Jul 22:23:15 - 0 clients connected (0 slaves), 717512 bytes in use

[14250] 14 Jul 22:23:20 - 0 clients connected (0 slaves), 717512 bytes in use

[14250] 14 Jul 22:23:25 - 0 clients connected (0 slaves), 717512 bytes in use

[14250] 14 Jul 22:23:30 - 0 clients connected (0 slaves), 717512 bytes in use

[14250] 14 Jul 22:23:35 - 0 clients connected (0 slaves), 717512 bytes in use

[root@localhost redis-stable]#

本文原出处 http://www.cnblogs.com/shanyou/archive/2012/07/14/2591881.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: