Centos6.5安装Redis3.0

1. 安装C编译环境

首先需要安装编译Redis的C环境,在命令行执行以下命令:

[root@itzhouq32 tools] yum install gcc-c++

2. 将redis3.0上传到Linux上

3. 解压redis,我这里解压到/usr/local下

[root@itzhouq32 tools]# tar -xvf redis-3.0.0.tar.gz -C /usr/local

4.编译

进入解压的目录,使用make编译

[root@itzhouq32 tools]# cd /usr/local/[root@itzhouq32 local]# lsbin  games    jdk1.8.0_201  lib64    redis-3.0.0  share  srcetc  include  lib           libexec  sbin         soft   tomcat[root@itzhouq32 local]# cd redis-3.0.0/[root@itzhouq32 redis-3.0.0]# ls00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  testsBUGS             deps     MANIFESTO  runtest          sentinel.conf     utilsCONTRIBUTING     INSTALL  README     runtest-cluster  src[root@itzhouq32 redis-3.0.0]# make

5. 安装

在原来的目录下安装,当前目录为/usr/local/redis

[root@itzhouq32 redis-3.0.0]# make PREFIX=/usr/local/redis install

6. 修改配置文件

进入redis-3.0.0的目录,找到redis.conf配置文件,将其拷贝到redis/bin目录下

[root@itzhouq32 local]# lsbin  games    jdk1.8.0_201  lib64    redis        sbin   soft  tomcatetc  include  lib           libexec  redis-3.0.0  share  src[root@itzhouq32 local]# cd redis-3.0.0/[root@itzhouq32 redis-3.0.0]# ls00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  testsBUGS             deps     MANIFESTO  runtest          sentinel.conf     utilsCONTRIBUTING     INSTALL  README     runtest-cluster  src[root@itzhouq32 redis-3.0.0]# cp redis.conf ../redis/bin/

返回redis/bin目录,修改redis.conf配置文件

[root@itzhouq32 redis-3.0.0]# cd ../redis/bin/[root@itzhouq32 bin]# lsdump.rdb         redis-check-aof   redis-cli   redis-sentinelredis-benchmark  redis-check-dump  redis.conf  redis-server[root@itzhouq32 bin]# vi redis.conf

将redis.conf文件中的daemonize从false修改成true表示后台启动

7. 后台启动测试

[root@itzhouq32 bin]# ./redis-server redis.conf[root@itzhouq32 bin]# ps -ef | grep redisroot       4653   4628  0 16:56 pts/1    00:00:00 ./redis-cliroot       4702      1  0 17:11 ?        00:00:02 ./redis-server *:6379    root       4782   1631  0 17:37 pts/0    00:00:00 grep redis[root@itzhouq32 bin]#

进程中有redis,说明后台启动成功。

8. 客户端登录及测试

[root@itzhouq32 bin]# ./redis-cli 127.0.0.1:6379> set username zhangsanOK127.0.0.1:6379> get username"zhangsan"127.0.0.1:6379> exit[root@itzhouq32 bin]#

9. 远程访问配置

如果需要远程访问redis,需要在Linux防火墙中开放6379端口,并将规则保存到防火墙中。

[root@itzhouq32 bin]# /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT[root@itzhouq32 bin]# /etc/rc.d/init.d/iptables saveiptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定][root@itzhouq32 bin]#

10. Java连接测试

在Eclipse中导入jar包,编写一个测试类。

package com.itzhouq.jedis;import org.junit.Test;import redis.clients.jedis.Jedis;public class JedisTest {    @Test    public void test01() {        //1. 获得连接对象        Jedis jedis = new Jedis("192.168.146.132", 6379);                //2. 获得数据        String username = jedis.get("username");        System.out.println(username);//zhangsan                //3. 存储        jedis.set("addr", "上海");        System.out.println(jedis.get("addr"));//上海    }}