CentOS 6.5 下安装 Redis 2.8.19,
1)下载Redis
wget http://download.redis.io/redis-stable.tar.gz
或者:http://download.redis.io/releases/ 下载最新版本
- wget http://download.redis.io/releases/redis-2.8.19.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make MALLOC=libc
make test
make install
这3个命令应该没有问题,主要的问题是执行make的时候,出现了异常。
异常一:
make[2]: cc: Command not found
异常原因:没有安装gcc
解决方案:yum install gcc-c++
异常二:
zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory
异常原因:一些编译依赖或原来编译遗留出现的问题
解决方案:make distclean。清理一下,然后再make。
在make成功以后,需要make test。在make test出现异常。
异常三:
couldn’t execute “tclsh8.5”: no such file or directory
异常原因:没有安装tcl
解决方案:yum install -y tcl。
异常四:运行make test 报了这样的一个错误:
!!! WARNING The following tests failed:
*** [err]: Test replication partial resync: ok psync (diskless: yes, reconnect: 1) in tests/integration/replication-psync.tcl
Expected condition ‘[s -1 sync_partial_ok] > 0’ to be true ([s -1 sync_partial_ok] > 0)
Cleanup: may take some time… OK
make[1]: *** [test] Error 1
make[1]: Leaving directory `/usr/local/src/redis-3.2.1/src’
make: *** [test] Error 2
■ 解决办法:
1,只用单核运行 make test:
taskset -c 1 sudo make test
2,更改 tests/integration/replication-psync.tcl 文件:
vi tests/integration/replication-psync.tcl
把对应报错的那段代码中的 after后面的数字,从100改成 500。我个人觉得,这个参数貌似是等待的毫秒数。
编辑文件tests/integration/replication-psync.tcl
然后找到after 100 把此值修改成200或者300。重新执行make test就可以了
在make成功以后,会在src目录下多出一些可执行文件:redis-server,redis-cli等等。
方便期间用cp命令复制到usr目录下运行。
cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/
然后新建目录,存放配置文件
mkdir /etc/redis
mkdir /var/redis
mkdir /var/redis/log
mkdir /var/redis/run
mkdir /var/redis/6379
在redis解压根目录中找到配置文件模板,复制到如下位置。
cd ..
cp redis.conf /etc/redis/6379.conf
通过vim命令修改
daemonize yes
pidfile /var/redis/run/redis_6379.pid
logfile /var/redis/log/redis_6379.log
dir /var/redis/6379
最后运行redis:
$ redis-server /etc/redis/6379.conf
配置自启动:
/etc/init.d/redis-server
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/etc/redis.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
(2)将redis加入自启动服务
sudo chkconfig --add /etc/init.d/redis-server
sudo chkconfig redis-server --level 345 on
使用chkconfig --list查看添加的服务
(4)启动服务
sudo service redis-server start
三、使用
redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> keys *
(empty list or set)