Centos6.5安裝Redis3.0備忘記錄

Centos6.5安裝Redis3.0

1. 安裝C編譯環境

首先須要安裝編譯Redis的C環境,在命令行執行如下命令:java

[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編譯c++

[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/redisredis

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

6. 修改配置文件

進入redis-3.0.0的目錄,找到redis.conf配置文件,將其拷貝到redis/bin目錄下tomcat

[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配置文件tcp

[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端口,並將規則保存到防火牆中。code

[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包,編寫一個測試類。server

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"));//上海
    }
}
相關文章
相關標籤/搜索