Redis installation and testing in centos6.9

1. Install gcc, redis compilation requires c language related environment, gcc belongs to c/c++ compiler
yum -y install gcc gcc-c++ libstdc++-devel tcl -y

2. Download the installation package (version 4.0.9 can be modified by yourself)

wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar -zxvf redis-4.0.9.tar.gz -C ../servers
cd /export/servers/
mv redis-4.0.9 redis

3. Compile redis

cd /export/servers/redis/
make MALLOC=libc
make PREFIX=/usr/local/redis install

4. Prepare the configuration file

cd /usr/local/redis/bin/
cd /usr/local/redis
mkdir conf
cd conf
vi redis_6379.conf

The content of the configuration file is as follows:

bind 127.0.0.1(用当前机器的ip地址替代)
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /export/data/redis/6379/redis_6379.pid
loglevel notice
logfile "/export/data/redis/6379/log.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /export/data/redis/6379/
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb mb 60
hz 10
aof-rewrite-incremental-fsync yes

5. Start the service

mkdir -p /export/data/redis/6379/
cd /usr/local/redis/bin/
./redis-server ../conf/redis_6379.conf

6. Use the client to connect

cd /usr/local/redis/bin/
./redis-cli(连接不上,那就 ./redis-cli -h 主机ip地址)

7. Test

127.0.0.1>ping
如果返回PONG的话就意味着响应成功

8. Test with Java code

8.1 Add pom dependencies

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

8.2 Writing main method tests

public class RedisTest {

    public static void main(String[] args) {
        // 操作之前保证redis服务启动,第一个参数为安装redis所在主机的ip,第二个参数为默认端口
        Jedis jedis = new Jedis("192.168.60.129", 6379);
        String pong = jedis.ping();
        System.out.println(pong);
        // 测试string set get map 
        jedis.set("name", "小明");
        String name = jedis.get("name");
        System.out.println(name);
        jedis.hset("mymap", "k1", "v1");
        String hget = jedis.hget("mymap", "k1");
        System.out.println(hget);
        Set<String> hkeys = jedis.hkeys("mymap");
        System.out.println("hkeys: " + hkeys);

        // list
        jedis.lpush("mylist", "a", "b", "c");
        jedis.rpush("mylist", "a", "b", "c");
        List<String> lrange = jedis.lrange("mylist", 0, -1);
        for (String string : lrange) {
            System.out.println(string);
        }


    }

}

9. Redis common commands
Reference : http://how2j.cn/k/redis/redis-commands/1369.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818705&siteId=291194637