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 startPython
安装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/jedisimport redis.clients.jedis.*
使用方法如下:Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo");
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Linux socket 初步
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Linux Kernel 4.0 RC5 发布!
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 介绍一款信息管理系统的开源框架---jeecg