redis安装和开启和关闭

redis---安装和开启和关闭

http://blog.csdn.net/xing_____/article/details/38457463

[html] view plain copy
 
  1. 系统:centos6.4  
  2. redis下载:http://www.redis.cn/download.html  
  3. redis安装步骤:  
  4. 上传源码包到/lamp  
  5. 解压:tar -zxvf redis.tar.gz  
  6. cd redis  
  7. make //编译源码,初装的linux系统到这一步可能会报以下错误  
  8.     CC adlist.o  
  9. /bin/sh: cc: command not found  
  10. 就需要装gcc  
  11. yum install gcc  
  12. 安装完后需要删除redis文件,重新解压,因为在原文件make还是会报相同的错误  
  13. rm zxvf redis  
  14. 解压:tar -zxvf redis.tar.gz  
  15. cd redis  
  16. make //编译源码  
  17. cd src   
  18. make install //安装程序  
  19. 为了便于管理可以将redis.conf redis-server redis-cli 这3个放入/usr/local/redis/目录下  
  20. 最后进入redis.conf  
  21. 修改 daemonize no 为daemonize yes #后台运行  
  22. 到此redis 安装完成  

6. 启动redis

a) $ cd /usr/local/bin

b) ./redis-server /etc/redis.conf

7. 检查是否启动成功

a) $ ps -ef | grep redis
关闭防火墙  systemctl stop firewalld.service 

3.   简单的Redis测试程序

    读者可以自行创建Eclipse项目,引入jedis的客户端包,测试程序如下:

    

[java] view plain copy
 
  1. public class RedisTest {  
  2.   
  3.     private Jedis jedis = null;  
  4.     private String key1 = "key1";  
  5.     private String key2 = "key2";  
  6.   
  7.     public RedisTest() {  
  8.         jedis = new Jedis("localhost");  
  9.     }  
  10.   
  11.     public static void main(String[] args) {  
  12.         RedisTest redisTest = new RedisTest();  
  13.         redisTest.isReachable();  
  14.         redisTest.testData();  
  15.         redisTest.delData();  
  16.         redisTest.testExpire();  
  17.     }  
  18.   
  19.     public boolean isReachable() {  
  20.         boolean isReached = true;  
  21.         try {  
  22.             jedis.connect();  
  23.             jedis.ping();  
  24.             // jedis.quit();  
  25.         } catch (JedisConnectionException e) {  
  26.             e.printStackTrace();  
  27.             isReached = false;  
  28.         }  
  29.   
  30.         System.out  
  31.                 .println("The current Redis Server is Reachable:" + isReached);  
  32.         return isReached;  
  33.     }  
  34.   
  35.     public void testData() {  
  36.         jedis.set("key1", "data1");  
  37.   
  38.         System.out.println("Check status of data existing:"  
  39.                 + jedis.exists(key1));  
  40.         System.out.println("Get Data key1:" + jedis.get("key1"));  
  41.   
  42.         long s = jedis.sadd(key2, "data2");  
  43.         System.out.println("Add key2 Data:" + jedis.scard(key2)  
  44.                 + " with status " + s);  
  45.     }  
  46.   
  47.     public void delData() {  
  48.         long count = jedis.del(key1);  
  49.   
  50.         System.out.println("Get Data Key1 after it is deleted:"  
  51.                 + jedis.get(key1));  
  52.     }  
  53.   
  54.     public void testExpire() {  
  55.         long count = jedis.expire(key2, 5);  
  56.   
  57.         try {  
  58.             Thread.currentThread().sleep(6000);  
  59.         } catch (InterruptedException e) {             
  60.             e.printStackTrace();  
  61.         }  
  62.   
  63.         if (jedis.exists(key2)) {  
  64.             System.out  
  65.                     .println("Get Key2 in Expire Action:" + jedis.scard(key2));  
  66.         } else {  
  67.             System.out.println("Key2 is expired with value:"  
  68.                     + jedis.scard(key2));  
  69.         }  
  70.     }  
  71.   
  72. }  
 
原文地址:https://www.cnblogs.com/tangou/p/7562556.html