Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

A list,

Redis is a NoSql database, with 16 libraries by default, ranging from 0 to 15. The initial database is DB0. Data is stored in memory and supports persistence, which is mainly used for backup and recovery. Supports a variety of data structures, including list, hash, set, and zset in addition to key-value structures.

It can be used mainly but not limited to the following scenarios:

  • Do relational database caching
  • Session sharing in distributed scenarios
  • Leaderboards, TopN, etc., using zset ordered sets
  • Counter, second kill, using INCR, DECR atomicity
  • Queue, list
  • Publish subscriptions, pub/sub

Second, the installation

This article will show you how to install Redis 6.2.6 on centos 7.6.

Official address: redis. IO /, we download the installation file from the official website:

  • Upload the downloaded file to /opt on the server

  • Install the C compilation environment

    yum install centos-release-scl scl-utils-build
    Copy the code
    yum install -y devtoolset-8-toolchain
    Copy the code
    scl enable devtoolset-8 bash
    Copy the code
  • Take a look at the GCC version:

    [root@hecs-402944 opt]# gcc --versionGCC (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is Free Software; see thesource for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Copy the code
  • Decompress redis-6.2.6.tar.gz and upload it to /opt

    The tar - ZXVF redis - 6.2.6. Tar. GzCopy the code
  • Enter /opt/redis-6.2.6 and run make to compile the file

    cdRedis - 6.2.6 makeCopy the code
  • isntall

    [root @ hecs - 402944 redis - 6.2.6]# make install
    cdSRC && make install make[1]: enter "/opt/redis-6.2.6/ SRC" CC makefile. dep Hint: It's a good idea to run 'make test';) INSTALL redis-server INSTALL redis-benchmark INSTALL redis-cli make[1]: leave directory "/opt/redis-6.2.6/ SRC"Copy the code
  • Check the installation directory. The default directory is /usr/local/bin

    cd /usr/local/bin
    Copy the code

    See the following for redis directly related content:

    • Redis-benchmark: a performance test tool

    • Redis-check-aof: Fixes faulty AOF files

    • Redis-check-dump: Repair the dump. RDB file

    • Redis-sentinel: used by the Redis cluster

    • Redis-server: indicates the command used to start the redis server

    • Redis -cli: client

  • Go to /opt/redis-6.2.6 and copy the redis.conf file to the folder we created

    [root@hecs-402944 opt]# CD redis - 6.2.6[root @ hecs - 402944 redis - 6.2.6]# mkdir /opt/myredis[root @ hecs - 402944 redis - 6.2.6]# cp redis.conf /opt/myredis/redis.conf
    Copy the code
  • Modify the copied redis.conf file

    vi /opt/myredis/redis.conf
    Copy the code

    Daemonize no = daemonize yes = Daemonize yes

  • Set the password

    In redis.conf, add the following configuration to set the password.

    Requirepass passwordCopy the code
  • Configuring log Files

    In redis. Conf, add the following configuration:

    logfile "/opt/myredis/logs/redis.log"
    Copy the code

    Note that you need to manually create the log file directory if the startup error occurs.

  • Start the redis

    redis-server /opt/myredis/redis.conf
    Copy the code

    Check whether to start:

    [root @ hecs - 402944 redis - 6.2.6]# ps -ef|grep redisroot 4712 1 0 10:51 ? 00:00:00 redis - server 127.0.0.1:6379Copy the code
  • Run redis-cli to check whether the startup is successful

    [root @ hecs - 402944 redis - 6.2.6]# redis-cli 127.0.0.1:6379 > ping PONGCopy the code
  • External IP address access is required. Check whether the redis server firewall starts port 6379. Configure security group policies for cloud servers. At the same time, pay attention to the following two changes to redis. Conf:

    • The bind 127.0.0.1 - : : 1If this configuration is enabled, you need to comment it out to open the Internet access
    • protected-mode yes, this configuration needs to be changed to No, which is to protect the local access mode. Even if this configuration is enabled, Internet access is still unavailable

    Restart Redis and view:

    [root @ hecs - 402944 redis - 6.2.6]# ps -ef|grep redisroot 4712 1 0 10:51 ? 00:00:00 redis-server 127.0.0.1:6379 root 8635 27628 0 10:54 PTS /0 00:00:00 grep --color=auto redis [root@hecs-402944 Redis - 6.2.6]# vi /opt/myredis/redis.conf [root @ hecs - 402944 redis - 6.2.6]# kill -9 4712[root @ hecs - 402944 redis - 6.2.6]# redis-server /opt/myredis/redis.conf [root @ hecs - 402944 redis - 6.2.6]# ps -ef|grep redis
     root     21392     1  0 11:05 ?        00:00:00 redis-server *:6379
     root     21485 27628  0 11:05 pts/0    00:00:00 grep --color=auto redis
    Copy the code

    The preceding information shows that the system changes from 127.0.0.1:6379 to *:6379. At this time, the Internet access is enabled.

    Test connection success using tools:

Common operation commands for keys

Redis has a series of common commands, no matter what type of operation you can not do without, I listed them below for easy reference, at the same time, you can view the following two websites redis command:

www.runoob.com/redis/redis…

www.redis.cn/commands.ht…

  • View all keys of the current library

    keys * 
    Copy the code
  • Check whether a key exists. Returns 1 if it does, and 0 if it does not

    exists key
    Copy the code
  • What is the return type of your key

    type key 
    Copy the code
  • Delete the specified key data

    del key       
    Copy the code
  • Select non-blocking delete based on value, removing only keys from the Keyspace metadata, and the actual deletion will be done asynchronously later.

    unlink key   
    Copy the code
  • Set the expiration time for the given key as follows: 10 seconds

    expire key 10
    Copy the code
  • Check how many seconds remain to expire. -1 indicates that it will never expire, and -2 indicates that it has expired

    ttl key 
    Copy the code
  • Switch the database. The value db_index is 0 to 15

    select db_index
    Copy the code
  • View the number of keys in the current database

    dbsize
    Copy the code
  • Clear the current library

    flushdb
    Copy the code
  • Clear all libraries

    flushall
    Copy the code