redis 启动,测试及关闭方法

主页 > 网页制作 > JavaScript > redis 启动,测试及关闭方法
启动redis: [root@docker ~]# redis-server /usr/local/src/redis-3.2.11/redis.conf 这里加载刚才修改的默认的redis配置文件redis.conf 查看是否启动成功: [root@docker ~]# ps -ef | grep redis root 30284 1 0 16:48 ? 00:00:00

启动redis:

[root@docker ~]# redis-server /usr/local/src/redis-3.2.11/redis.conf
这里加载刚才修改的默认的redis配置文件redis.conf

查看是否启动成功:

[root@docker ~]# ps -ef | grep redis
root 30284 1 0 16:48 ? 00:00:00 redis-server 127.0.0.1:6379
root 30289 25916 0 16:48 pts/0 00:00:00 grep redis

测试redis:

[root@docker ~]# redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit

关闭redis:

[root@docker redis-3.2.11]# redis-cli shutdown
 

redis安全模式访问:

测试程序报错为:

    DENIED Redis is running protected mode because protected mode is enabled,
    no bind address was specified, no authentication password is requested to clients.
    In this mode connections are only accepted from the loopback interface.

 需要给redis配置密码

    启动redis客户端

    执行  redis-cli指令

    然后执行    config set requirepass 777888(密码)

简单的Redis测试程序:

public class RedisTest {

    private Jedis jedis = null;
    private String key1 = "key1";
    private String key2 = "key2";

    public RedisTest() {
        jedis = new Jedis("127.0.0.1");  // redis的IP地址
        jedis.auth("redis");   // redis密码
    }

    public static void main(String[] args) {
        RedisTest redisTest = new RedisTest();
        redisTest.isReachable();   // redis是否访问成功 返回结果true/false
        redisTest.testData();    // 数据测试
        redisTest.delData();     // 删除数据
        redisTest.testExpire();
    }

    public boolean isReachable() {
        boolean isReached = true;
        try {
            jedis.connect();
            jedis.ping();
            // jedis.quit();
        } catch (JedisConnectionException e) {
            e.printStackTrace();
            isReached = false;
        }

        System.out
                .println("The current Redis Server is Reachable:" + isReached);
        return isReached;
    }

    public void testData() {
        jedis.set("key1", "data1");
        jedis.set("maxm", "MMM");
        System.out.println(jedis.get("maxm"));
        System.out.println("Check status of data existing:"
                + jedis.exists(key1));
        System.out.println("Get Data key1:" + jedis.get("key1"));

        long s = jedis.sadd(key2, "data2");
        System.out.println("Add key2 Data:" + jedis.scard(key2)
                + " with status " + s);
    }

    public void delData() {
        long count = jedis.del(key1);

        System.out.println("Get Data Key1 after it is deleted:"
                + jedis.get(key1));
    }

    public void testExpire() {
        long count = jedis.expire(key2, 5);

        try {
            Thread.currentThread().sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (jedis.exists(key2)) {
            System.out
                    .println("Get Key2 in Expire Action:" + jedis.scard(key2));
        } else {
            System.out.println("Key2 is expired with value:"
                    + jedis.scard(key2));
        }
    }

}

相关文章