共计 2407 个字符,预计需要花费 7 分钟才能阅读完成。
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]# ls
bin games jdk1.8.0_201 lib64 redis-3.0.0 share src
etc include lib libexec sbin soft tomcat
[root@itzhouq32 local]# cd redis-3.0.0/
[root@itzhouq32 redis-3.0.0]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING 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]# ls
bin games jdk1.8.0_201 lib64 redis sbin soft tomcat
etc include lib libexec redis-3.0.0 share src
[root@itzhouq32 local]# cd redis-3.0.0/
[root@itzhouq32 redis-3.0.0]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING 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]# ls
dump.rdb redis-check-aof redis-cli redis-sentinel
redis-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 redis
root 4653 4628 0 16:56 pts/1 00:00:00 ./redis-cli
root 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 zhangsan
OK
127.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 save
iptables:将防火墙规则保存到 /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"));// 上海
}
}
正文完