redis basic learning

Posted by Zeon on Sun, 12 Apr 2020 14:52:24 +0200

redis basic learning

 

redis

Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware

yum install redis

1.yum installation

#The premise is to configure alicloud yum source and epel source
#Check whether there is redis package
yum list redis
#install redis
yum install redis -y
#Install it and start redis
systemctl start redis

2. Check whether redis works

redis-cli    #redis client tools
#After entering the interactive environment, ping will be executed, and a pong will be returned to indicate that the installation is successful
127.0.0.1:6379> ping
PONG

Source install redis, compile and install

We have used yum, which is quite easy to use, why learn the source code installation?

Some people say that the compilation and installation performance is good? Wrong

The advantages of compiling and installing are:

  • When compiling and installing, you can specify the extended module. php, apache and nginx are all the same. There are many third-party extension modules, such as mysql. When compiling and installing, you can customize the storage engine (innodb or MyIASM) if necessary
  • The installation path can be unified for compilation and installation. The linux software installation directory is under / opt /
  • Generally, the version of software warehouse is relatively low, and the latest version can be installed according to the requirements
1.download redis Source code
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
2.decompression
tar -zxf redis-5.0.2.tar.gz
3.switch redis Source directory
cd redis-5.0.2
4.Compile source file
make 
5.After compiling, src/Compiled under the directory redis instructions
6.make install Install to the specified directory, default to/usr/local/bin

redis profile

In the redis-6379.conf configuration file, customize some security parameters
Port change
Background operation
Set redis password
Set the redis start ip address, etc

# redis-6379.conf The configuration is as follows:

port 6379                     
daemonize yes                                    
pidfile /data/6379/redis.pid
loglevel notice            
logfile "/data/6379/redis.log"
dir /data/6379                    
protected-mode yes                
requirepass   haohaio
port 6379                         # Running at 6379 redis Database instance
daemonize yes                     # Background operation redis  
pidfile /data/6379/redis.pid      # Deposit redis pid Documents
loglevel notice                   # Log level
logfile "/data/6379/redis.log"    # Appoint redis Log file generation directory
dir /data/6379                    # Appoint redis Directory of data folder
protected-mode yes                # safe mode
requirepass   haohaio             # Set up redis Password
redis profile details

redis executable

./redis-benchmark  # Used for redis Tools for performance testing
./redis-check-dump  # Used to fix the problem dump.rdb file
./redis-cli  # redis Client
./redis-server  # redis Server side
./redis-check-aof  # Used to fix the problem AOF file
./redis-sentinel  # For cluster management

Start redis server

It's very easy to start redis. Directly. / redis server can start the server. You can also specify the configuration file to load with the following methods:
./redis-server ../redis.conf
By default, the redis server runs in a non daemon mode, and the default service port is 6379.

Using redis client

#Execute the client command to enter
./redis-cli
 #Or specify parameters
redis-cli  -p 6380  -a  zhuanqq
    -p set the port of redis link
    -a password displayed
    --raw uses the original format

#Test whether to connect to redis
127.0.0.1:6379 > ping
 Return pong to indicate the connection is connected

#Use set to set key and value
127.0.0.1:6379 > set name "tiger"
OK
 #Get get get the value of name
127.0.0.1:6379 > get name
"tiger"

redis data structure

redis is an advanced key: value storage system, where value supports five data types
 strings
 Hashes (hashes)
lists
 Sets (sets)
sorted sets

Sample data structure

1.strings type

  • set key
  • Get get key
  • Append append string
  • mset sets multiple key value pairs
  • mget get multiple key value pairs
  • del delete key
  • incr increment + 1
  • decr decrement-1
127.0.0.1:6379> set name 'tt'   #Set key
OK
127.0.0.1:6379> get name    #Get value
"tt"
127.0.0.1:6379> set name 'tiger'  #Overlay key
OK
127.0.0.1:6379> get name    #Get value
"tiger"
127.0.0.1:6379> append name ' dsb'   #Append string of key
(integer) 10
127.0.0.1:6379> get name  #Get value
"tiger dsb"
127.0.0.1:6379> mset user1 'alex' user2 'xiaopeiqi'    #Set multiple key value pairs
OK
127.0.0.1:6379> get user1    #Get value
"alex"
127.0.0.1:6379> get user2    #Get value
"xiaopeiqi"
127.0.0.1:6379> keys *      #Find all key s
1) "user2"
2) "name"
3) "user1"

127.0.0.1:6379> mget user1 user2 name   #Get multiple value s
1) "alex"
2) "xiaopeiqi"
3) "tiger dsb"
127.0.0.1:6379> del name        #Delete key
(integer) 1
127.0.0.1:6379> get name        #Get non-existent value, nil
(nil)
127.0.0.1:6379> set num 10    #String type actually includes not only string type, but also integer type and floating-point type. redis can operate on the whole string or a part of the string, while for integer / floating-point type, it can operate on auto increment and auto decrement.
OK    
127.0.0.1:6379> get num
"10"
127.0.0.1:6379> incr num    #Add an INCR command to num string to parse the string value into an integer, add one to it, and finally save the result as a new string value, which can be used as a counter
(integer) 11
127.0.0.1:6379> get num  
"11"

127.0.0.1:6379> decr num      #Decrease by 1  
(integer) 10
127.0.0.1:6379> decr num    #Decrease by 1
(integer) 9
127.0.0.1:6379> get num
"9"

2.list type

  • lpush inserts from the left side of the list
  • rpush inserts from the right side of the list
  • Lrange gets a certain length of element lrange key start stop
  • ltrim intercepts a list of certain length
  • lpop removes the leftmost element
  • rpop delete the rightmost element
  • If lpushx/rpushx key exists, add the value, if not, do not process
lpush duilie 'alex' 'peiqi' 'ritian'  #Create a new duilie and put three elements from the left

llen duilie  #View duelie length

lrange duilie 0 -1  #View all duilie elements

rpush duilie 'tiger'   #Insert tiger from right

lpushx duilie2  'dsb'  #Add dsb element if key exists, and do not process if key does not exist

ltrim duilie 0 2  #Intercept the value of the queue, get from index 0 to 2, and delete the remaining elements

lpop #Delete the first one on the left
rpop #Delete the first one on the right

3.sets set type

redis set is a kind of unordered set. The elements in the set have no order.

Set related operations are also rich, such as adding new elements, deleting existing elements, fetching intersection, fetching Union, fetching difference set, etc. Let's take an example:

  • sadd/srem add / remove elements
  • sismember determines whether it is an element of set
  • smembers returns all members of the collection
  • sdiff returns the difference between a set and other sets
  • Single returns the intersection of several sets
  • sunion returns the union of several sets
sadd zoo  wupeiqi yuanhao  #Add a collection with three elements, and treat it as a string without quotation marks

smembers zoo  #View collection zoo members

srem zoo  wupeiqi #Delete alex in zoo

sismember zoo wupeiqi  #Return whether it is the member information of zoo. If it does not exist, return 0. If it exists, return 1

sadd zoo wupeiqi   #Add wupeiqi to zoo

smembers zoo  #View zoo members

sadd zoo2 wupeiqi mjj #Add new set zoo 2

sdiff zoo zoo2 #Find out the elements that exist in the set zoo, but not in zoo 2

sdiff zoo2  zoo  #Find out the elements that exist in zoo 2 but not in zoo 2

sinter zoo zoo1   #Find out the intersection of zoo and zoo1, all of which have elements

sunion  zoo zoo1  #Find out the union of zoo and zoo1, all the elements that are not repeated

4. Hash data structure

Hashes are hashes. Hash is a data structure only available after redis-2.0.0.

hashes store the mapping between strings and string values. hashes are especially suitable for storing objects. For example, if a user wants to store his / her full name, last name, age, etc., they are very suitable for using hash es.

Each hash in Redis can store 232 - 1 key value pairs (more than 4 billion).

Hash, generally translated as hash, hash, or transliteration into hash, is to transform input of any length into output of fixed length through hash algorithm, which is hash value.
This transformation is a kind of compression mapping, that is, the space of hash value is usually far less than the space of input, different inputs may be hashed into the same output, so it is impossible to determine the unique input value from the hash value.
In short, it is a function that compresses messages of any length into message summaries of a fixed length.
  • hset set hash value
  • hget get hash value
  • hgetall gets all the fields and values of the key specified in the hash table
  • hmset sets many pairs of hash values
  • hmget get multi pair hash value
  • hsetnx is not set if the hash already exists (to prevent overwriting the key)
  • hkeys returns all keys
  • hvals returns all values
  • hlen returns the number of hash containing field s
  • hdel delete hash specified field
  • Exists
redis hash It is a string Type field and value Mapping table

//Syntax hset key field value  

hset news:1   title "first news title" #Set the id of the first news to 1, and the value of the added data title is "first news title"

hset news:1 content "news content"    #Add a content for content

hget news:1 title   #Get the title of news:1

hget news:1  content  #Get the content of news

hmget news:1  title content   #Get the value of many to news:1

hmset news:2 title "second news title" content "second Contents2"   #Set the second news:2 multiple field s

hmget news:2 title  content #Get multiple values for news:2

hkeys news:1   #Get all key s of news news:1

hvals news:1   #Get all values for news news:1

hlen news:1    #Get the length of news news:1

hdel news:1 title   #Delete the title of news news:1

hlen news:1     #Look at the length of news:1

hexists news:1 title    #Judge whether there is title in news 1, return 0 if there is no title, return 1 if there is title

 

redis security

 

Redis sentinel combat

 

 

 
 

Topics: Linux Redis yum Database EPEL