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

Linux Redis安装及使用

2016-05-28 02:02 645 查看

转载请标明出处 http://coderknock.com

获取Redis包[可以在官网获取最新的下载路径]

wget http://download.redis.io/releases/redis-3.2.0.tar.gz[/code] 

解压、编译

tar xzf redis-3.2.0.tar.gz
cd redis-3.2.0
make
make test
make install


make test时可能会报如下错误:

cd src && make test
make[1]: Entering directory `/root/redis-3.2.0/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.2.0/src'
make: *** [test] Error 2


我们安装一下 tcl 支持,然后在进行编译操作

yum install tcl


编译的时间可能会比较长,大家耐心等待一会儿

!!! WARNING The following tests failed:

*** [err]: Server is able to generate a stack trace on selected systems in tests/integration/logging.tcl
expected stack trace not found into log file
Cleanup: may take some time... OK
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.2.0/src'
make: *** [test] Error 2


上面的警告只是某个测试没有通过,可以忽略。

编译完成后会在src文件夹中生成
redis-server
redis-benchmark
redis-cli


新建一个自己的文件夹,将上面的文件放入其中,并且将redis安装包解压目录下的redis.conf也拷贝入新建文件夹

mkdir /etc/redis
cp redis-server /etc/redis
cp redis-benchmark /etc/redis
cp redis-cli /etc/redis
cd ..
cp redis.conf /etc/redis
cd /etc/redis


转到目录,执行

redis-server redis.conf


如果出现



则安装成功,但是这样子每次运行后不能关闭命令窗口,不然进程就会断掉。

将redis做成一个服务

redis-3.2.0/utils/redis_init_script是redis初始化脚本

将其复制到etc服务中

cp redis-3.2.0/utils/redis_init_script /etc/rc.d/init.d/redis


如果这时添加注册服务:

chkconfig --add redis


将报以下错误:

service redis does not support chkconfig


我们需要修改一下启动的脚本:

#!/bin/sh
#chkconfig: 2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/etc/redis/redis-server
CLIEXEC=/etc/redis/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF &
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac


修改的内容:

添加上面代码中的第二行
chkconfig: 2345 80 90
内容

按情况修改7、8行

EXEC=/etc/redis/redis-server
CLIEXEC=/etc/redis/redis-cli


第20行添加&表示服务在后台运行[这样关闭命令行之后也会继续运行]

我们看到第11行指定的配置文件是
CONF="/etc/redis/${REDISPORT}.conf"
以端口为名的conf[这样改变端口时不同端口可以使用不同conf]

所以

cp /etc/redis/redis.conf /etc/redis/6379.conf


执行注册
chkconfig --add redis
便不会报错了

启动服务
service redis start

Python

安装Python相关包

pip install redis


然后就可以使用了

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
True
r.get('foo')


Java

下载jedis的包 Github地址 https://github.com/xetorthio/jedis

import redis.clients.jedis.*


使用方法如下:

Jedis jedis = new Jedis("localhost");
jedis.set("foo", "bar");
String value = jedis.get("foo");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis linux java python