[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 & connection

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) based on memory A data structure storage system that can be used asdatabase, cache, and messaging middleware. Redis supports a variety of data structures, includingstrings, hash tables, lists, sets, ordered sets, etc. These data structures can be accessed through a rich set of commands set to operate.

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 the versionDownload | Redis (redis.io).

1.2. Import

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

1.3. Decompression

Unzip to our redis

命令:tar -xvf redis-5.0.0.tar.gz

1.4. Installation

Go to the decompressed file to install. Installation:make

Waiting for the installation to complete, we check the installation status:make install

1.5. Modify files

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

Revisedredis.confInstruction:vim redis.conf

daemonize no 改为 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. Testing

Test whether redis startup is successful

Instruction:./redis-cli
Instruction:ping

1.8. End the process

Using command:kill -9  PID Consequence progress

1.9. Change password access

The above link iswithout password, modified towith password, andfor external access.

Repair redis.conf Another technique

Notebind 127.0.0.1

修改requirepass 123456

Setting the portRemember to make sure the firewall is turned on

命令1:firewall-cmd --zone=public --add-port=6379/tcp --permanent
命令2:firewall-cmd --reload && firewall-cmd --list-port

reconnect

  1. 重启:./src/redis-server redis.conf
  2. Check whether the port is open:lsof -i:6379
  3. Register:./src/redis-cli -h 127.0.0.1 -p 123456-a6379
    1. -h:host     
    2. -p:prot     Port number
    3. -a:authentication      Authorization password
  4. Command:ping

1.10. Install client tools & connection

Install client tools on the hostRedisInsight | The Best Redis GUI. Download and install.

2. Windows version

2.1. Download

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

2.2. Installation

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

cd Redis-x64-3.2.100 #进入解压文件
redis-server redis.windows.conf #安装

Double click the bat file

The installation is complete

2.3. Modification

Revised redis.windows.conf Text item

Notebind 127.0.0.1

修改requirepass 123456

2.4. Connection

3. Redis command

1. Related commands

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

2. Commonly used 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 key-value pair And set the expiration time
  • mset key value [key value …] 设置多个key value
  • msetnx key1 value1 [key2 value2…]< /span> Set key-value pairs in batches, executed only when all keys in the parameters do not exist, atomic operations, successful together, failed together:
  • get key: 返回key的value
  • mget key [key …]  :
  • exists key [key ...]:  Query whether a key exists
  • decr/incr key: Increase 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 value
  • append key value: appends 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 indicates that type returns the key-value pair storage type, not the value storage type

2.2. Redis 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]  #同时将多个field-value设置到哈希表key中
hset user name zs age 12 sex nv

# hget key field                          #获取指定的字段值
hget user age

# hdel key field                          #删除指定的字段值
hdel user age

# hgetall key                             #查询指定key的所有字段
hgetall user

# hexists key field                       #查询指定key中的字段是否存在
hexists user name

# hlen key                                #获取指定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          #将一个或多个值插入到列表头部
lpush en a b c d e f g

# llen key                                #获取列表的长度
llen en

# lindex key index                        #根据索引获取列表中的元素
lindex en 1     #返回f,说明下标从0开始,同时先进后出

# lrange key start sop                    #查看指定范围内的元素
lrange en 1 3    #返回fed,说明下标从0开始,同时先进后出

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]                #向集合添加一个或多个元素
sadd hobby lanqiu zuqiu bingpangqiu zhuoqiu

# scard key                               #获取集合中的元素数量
scard hobby

# exists key                              #是否存在
exists hobby


 

Guess you like

Origin blog.csdn.net/weixin_74383330/article/details/134185901