[Server] Redis installation and usage commands (Linux, Windows version)

Table of Contents

1. Introduction to Redis

2. Redis installation

1. Linux version

1.1. Download

1.2. Import

1.3. Decompression

1.4. Installation

1.5. Modify files

1.6. Start redis

1.7. Testing

1.8. End the process

1.9. Change password access

1.10. Install client tools & connect

2. Windows version

2.1. Download

2.2. Installation

2.3. Modification

2.4. Connection

3. Redis command

1. Related commands

2. Commonly used commands

2.1. Five major strings (string)

2.2. Redis Hash

2.3. Redis list (List)

2.4. Redis collection (Set)


1. Introduction to Redis

Redis (Remote Dictionary Server) is an open source (BSD license) memory-based data structure storage system, which can be used as a database, cache and message middleman Pieces. Redis supports a variety of data structures, including strings, hash tables, lists, sets, ordered sets, etc. These data structures can be operated through a rich set of commands.

Compared with traditional database systems, Redis has higher performance and scalability. It stores data in memory, so it can achieve very low read and write latency and can handle highly concurrent requests. In addition, Redis also supports data persistence, which can regularly save data in memory to disk to prevent data loss.

Redis also has some other features, such as publish and subscribe mechanism, transaction support, key expiration, etc. It also provides some additional functions, such as real-time statistics, distributed locks, etc., making it widely used in various application scenarios.

All in all, in a word, Redis is a high-performance, flexible and easy-to-use data storage system, suitable for scenarios that require fast read and write operations and high concurrent access, such as caching, session management, message queues, etc.

2. Redis installation

1.Linux version

1.1, download

The first choice is to find our official website to select and download the version | Redis (redis.io).

1.2, import

Open the Linux system and import the installation package into it.

1.3, decompression

Unzip to our redis

Command: tar -xvf redis-5.0.0.tar.gz

1.4, installation

Go into the unzipped file to install. Installation:make

Wait for the installation to complete, let’s check the installation: make install

1.5, modify file

Modify the redis.conf file. Before modifying the file, we first make a backup to prevent change errors: cp redis.conf redis_bak.conf

Modify redis.conf command: vim redis.conf

Change daemonize no to daemonize yes

1.6, start redis

Start our redis: ./src/redis-server redis.conf

Check whether our redis port is started: lsof -i:6379

As shown in the picture, we have already enabled

1.7, test

Test whether redis startup is successful

Command: ./redis-cli
Command: ping

1.8, end process

Use the command: kill -9 PID to end the process

1.9. Change password for access

The above is a passwordless link, change it to password, and external access.

Modify redis.conf

Note: bind 127.0.0.1

Modification: requirepass 123456

Remember to make sure the firewall is turned on before setting the port

Command 1: firewall-cmd –zone=public –add-port=6379/tcp –permanent
Command 2: firewall-cmd –reload & amp; & amp; firewall-cmd –list-port

reconnect

  1. Restart: ./src/redis-server redis.conf
  2. Check whether the port is open: lsof -i:6379
  3. Login: ./src/redis-cli -h 127.0.0.1 -p 6379 -a 123456
    1. -h:host host
    2. -p:prot port number
    3. -a:authentication permission password
  4. Command: ping

1.10, Install client tools & amp;Connect

Install the client tool RedisInsight | The Best Redis GUI on the host. Download and install.

2. Windows version

2.1, download

The first choice is to find our official website to select and download the version | Redis (redis.io).

2.2, Installation

Unzip the downloaded file, write the file, put the code in it and change the suffix to bat

cd Redis-x64-3.2.100 #Enter the decompressed file
redis-server redis.windows.conf #Installation

Double click the bat file

The installation is complete

2.3. Modification

Modify the redis.windows.conf file

Note: bind 127.0.0.1

Modification: requirepass 123456

2.4, connection

3. Redis command

ping: Check whether the connection is alive
echo: Print something on the command line
quit, exit: Exit the client
shutdown: Exit the server
info: Returns redis related information
config get dir/* delivers received requests in real time
showlog: Show slow queries
select n: Switch to database n. Redis has 16 databases by default (DB 0~DB 15), and the 0th one is used by default.
dbsize: View the current database size
move key n: Data between different databases cannot be interoperable. Move moves the key to the specified database.
flushdb: Clear the key-value pairs in the current database.
flushall: Clear all database key-value pairs.

2. Common commands

2.1, five major strings (string)

  • set key value: Set the value of a key
  • setnx key value: Set only when the key does not exist
  • setex key seconds value: set the key-value pair and set the expiration time
  • mset key value [key value …]: Set multiple key values
  • msetnx key1 value1 [key2 value2…]: Set key-value pairs in batches, executed only when all keys in the parameters do not exist, atomic operations, success and failure together.
  • get key: Returns the value of key
  • mget key [key …] : Batch Get the values saved by multiple keys
  • exists key [key …]: Query whether a key exists
  • decr/incr key: Change the value of the specified key by + 1/-1 (only for numbers)
  • incrby/decrbyB key n: Add and subtract values according to the specified step size
  • incrbyfloat key n: Adds a floating point value to a number
  • append key value: Append a string to the value of the specified key
  • strlen key: Returns the length of the string type value of the key.
  • getset key value: Set the value of a key and get the value before setting. If it does not exist, return null.
  • setrange key offset value: Set the character at the specified position
  • getrange key start end: Get a substring of the value stored on key
  • type key: string Description type returns a key-value pair storage type, not a value storage type

2.2, Redis Hash (Hash)

Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects. “`

# hset key field1 value1 [field2 value2] #Set multiple field-values into the hash table key at the same time
hset user name zs age 12 sex nv

#hget key field #Get the specified field value
hget user age

#hdel key field #Delete the specified field value
hdel user age

#hgetall key #Query all fields of the specified key
hgetalluser

# hexists key field #Query whether the field in the specified key exists
hexists user name

#hlen key #Get the length in the specified key
hlen user

2.3, Redis List (List)

Redis lists are simple lists of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list

# lpush key value1 value2 value3 #Insert one or more values into the head of the list
lpush en a b c d e f g

#llen key #Get the length of the list
llen en

#lindex key index #Get elements in the list based on index
lindex en 1 #Return f, indicating that the subscript starts from 0, and at the same time, first in, last out

# lrange key start sop #View elements within the specified range
lrange en 1 3 #Return to fed, indicating that the subscript starts from 0, while first in, last out

2.4, Redis collection (Set)

Redis Set is an unordered collection of String type. Set members are unique, which means that duplicate data cannot appear in the set.

# sadd key value1 [value2] #Add one or more elements to the collection
sadd hobby lanqiu zuqiu bingpangqiu zhuoqiu

# scard key #Get the number of elements in the collection
scard hobby

# exists key #whether it exists
exists hobby

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16924 people are learning the system