Introduction and installation of redis, systemd management services, data types, common operations

Redis installation

Official website: http://www.redis.cn/

Install gcc

yum install -y gcc-c++

Download the redis installation package:

wget http://download.redis.io/releases/redis-6.0.6.tar.gz

Unzip:

tar -xzvf redis-6.0.6.tar.gz

Enter the redis decompression directory:

cd redis-6.0.6

Compile and install:

make

An error occurred:

img

Solution:

Check whether the gcc version is above 5.3, centos7.6 installs 4.8.5 by default

gcc -v

Upgrade gcc to 5.3 and above, upgrade to 9.3 devtoolset-9-binutils below

yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

scl enable devtoolset-9 bash //temporarily enable. Exit the shell or restart it will restore the original gcc version

echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile //Long-term use

Compile again:

make

img

make install

img

Start the service:

1. Start directly: /usr/local/bin/redis-server

img

As shown in the figure above: redis started successfully, but this startup method needs to open the window all the time and cannot perform other operations, which is not convenient.

Press ctrl + c to close the window.

2. Set the configuration file to start redis as a background process

cp redis.conf / etc /

vim /etc/redis.conf

Configure redis to start in the background: change daemonize no to daemonize yes

Start service

redis-server /etc/redis.conf

View service process

ps to | grep redis

3. Set redis to start automatically after booting

Create a new redis directory under the /etc directory

mkdir /etc/redis

Copy the /etc/redis.conf file to the /etc/redis/ directory and name it 6379.conf

cp /etc/redis.conf /etc/redis/6379.conf

Copy the startup script in the redis installation package and place it in the /etc/init.d directory

cp /usr/local/src/redis-6.0.6/utils/redis_init_script /etc/init.d/redisd

Set redis to start

cd /etc/init.d/

chkconfig redisd on

Start: service redisd start

Shut down: service redisd stop or /usr/local/bin/redis-cli shutdown

4. Write the service management script to start

vim /usr/lib/systemd/system/redis.service

[Unit]

Description = Redis

After=network.target

[Service]

Type=forking

PIDFile=/var/run/redis_6379.pid //The configuration is consistent with /etc/redis.conf

ExecStart=/usr/local/bin/redis-server /etc/redis.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

Create a soft link:

ln -s /usr/lib/systemd/system/redis.service

/etc/systemd/system/multi-user.target.wants/redis.service

start up

systemctl daemon-reload

systemctl start redis

systemctl stop redis

Set the log storage path and level

logfile "" is empty by default, that is, under /dev/null, you can specify a path, for example: /var/log/redis.log

img

Log in to redis

redis-cli -h remote IP -p port (default port 6379) -a'passwd' password

Redis data type

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).

Redis command collection: http://doc.redisfans.com/index.html (reference )

Note: The command can be tab-completion, not case sensitive

Redis string (String):

Redis string data type related commands are used to manage redis string values

grammar:

redis 127.0.0.1:6379> COMMAND KEY_NAME

Examples:

127.0.0.1:6379> set runoobkey redis

OK

127.0.0.1:6379> get runoobkey

"redis"

Runoobkey is the key redis is the value of the key

command:

set key value Set the value of the specified key

get key Get the value of the specified key

mset key1 value1 key2 value2 key3 value3 Specify the value of multiple keys at the same time

mget key1 key2 key3 Query the value of multiple keys at the same time

Redis list (List)

List is a linked list structure, the main function is to push, pop, get all the values ​​of a range, and so on. In the operation, the key is understood as the name of the linked list. list is used for message queues.

LPUSH key value1 [value2]

Insert one or more values ​​into the head of the list

LRANGE key start stop

Get the elements in the specified range of the list

RPOP key

Remove the last element of the list, and the return value is the removed element.

LPOP key

Move out and get the first element of the list

Examples:

127.0.0.1:6379> LPUSH list1 "aaa" "bbb" "ccc" "ddd"

127.0.0.1:6379> LRANGE list1 0 -1 //** List all elements**

1) "fff"

2) "eee"

3) "ddd"

4) "ccc"

5) "bbb"

6) "aaa"

127.0.0.1:6379> LRANGE list1 0 2 //** List the first three elements**

1) "fff"

2) "eee"

3) "ddd"

127.0.0.1:6379> LPOP list1 //** Removed the first element of the above list**

"fff"

127.0.0.1:6379> RPOP list1 //** Removed the last element of the above list**

"aaa"

Redis set (Set)

A set is an unordered collection of string type. The members of the set are unique. It is similar to the concept of a set in our mathematics. The operations on the set include adding and deleting elements, and the intersection and difference of multiple sets. In the operation, the key is understood as the name of the collection.

SADD key member1 [member2]
Add one or more members to the collection

SMEMBERS key
returns all members in the set

SREM key member1 [member2]
Remove one or more members from the set

SINTER key1 [key2]
returns the intersection of all the given sets

SUNION key1 [key2]
returns the union of all the given sets

SDIFF key1 [key2]
returns the difference between the first set and the other sets.

Examples:

SADD set1 a b c

127.0.0.1:6379> sadd set1 a b c

(integer) 3

SMEMBERS set1 //Read all elements

127.0.0.1:6379> SMEMBERS set1

1) "c"

2) "b"

3) "a"

SREM set1 c //Remove element

127.0.0.1:6379> SREM set1 c

(integer) 1

SADD set2 1 ab //set2 add element

127.0.0.1:6379> SADD set2 1 a b

(integer) 3

127.0.0.1:6379> SMEMBERS set2

1) "b"

2) "a"

3) "1"

SINTER set1 set2 //Intersection, duplicate elements

127.0.0.1:6379> SINTER set1 set2

1) "b"

2) "a"

SUNION set1 set2 //Union set, all elements of the two

127.0.0.1:6379> SUNION set1 set2

1) "c"

2) "1"

3) "a"

4) "b"

SDIFF set1 set2 //Difference set, set1 has no elements in set2

127.0.0.1:6379> SDIFF set1 set2

1) "c"

Redis sorted set

A sorted set is an ordered set, and it is also a set of string type elements. It has one more weight parameter score than set, so that the elements in the set can be sorted from small to large according to the score.

ZADD key score1 member1 [score2 member2]
Add one or more members to an ordered set, or update the scores of existing members

ZRANGE key start stop [WITHSCORES]
Return the members in the specified range of the ordered set through the index range

ZREVRANGE key start stop [WITHSCORES]
returns the members in the specified interval in the ordered set, and the scores are from high to low by index

Examples:

ZADD set3 12 abc //** Set the abc element score to **12

127.0.0.1:6379> ZADD set3 12 abc

(integer) 1

ZADD set3 2 "cde 123" //** Set the score to **2

127.0.0.1:6379> ZADD set3 2 "cde 123"

(integer) 1

ZADD set3 24 "123-aaa" //** Set the score to **24

127.0.0.1:6379> ZADD set3 24 "123-aaa"

(integer) 1

ZADD set3 4 "a123a" //** Set the score to **4

127.0.0.1:6379> ZADD set3 4 "a123a"

(integer) 1

ZRANGE set3 0 -1 //** Sorted according to the score from small to large**

127.0.0.1:6379> ZRANGE set3 0 -1

1) "cde 123"

2) "a123a"

3) "abc"

4) "123-aaa"

ZREVRANGE set3 0 -1 //** Reverse order, sorted according to the score from largest to smallest**

127.0.0.1:6379> ZREVRANGE set3 0 -1

1) "123-aaa"

2) "abc"

3) "a123a"

4) "cde 123"

Redis hash (Hash)

Redis hash is a mapping table between field and value of string type. Hash is especially suitable for storing objects.

In fact, hash can be considered a multi-dimensional string.

HSET key field value
sets the value of the field field in the hash table key to value.

HMSET key field1 value1 [field2 value2]
Set multiple field-value (domain-value) pairs into the hash table key at the same time.

HGET key field
gets the value of the specified field stored in the hash table.

HGETALL key
Get all the fields and values ​​of the specified key in the hash table

Examples:

hset hash1 our name

127.0.0.1:6379> HSET hash1 our name

(integer) 1

hget hash1 name //** View the value of the name field in the hash1 table **

127.0.0.1:6379> HGET hash1 name

"aming"

hset hash1 age 30 //** Store the value of the age field in hash1 **

127.0.0.1:6379> HSET hash1 age 30

(integer) 1

hgetall hash1 // View all fields and values ​​in the ** hash1 ** table

127.0.0.1:6379> HGETALL hash1

1) "name"

2) "aming"

3) "age"

4) "30"

HMSET hash2 name "admin" age "30" bb "aa" //** Set multiple domain values ​​to the hash table at once**

127.0.0.1:6379> HMSET hash2 name "admin" age "30" bb "aa"

OK

127.0.0.1:6379> HGETALL hash2

1) "name"

2) "admin"

3) "age"

4) "30"

5) "bb"

6) "aa"

Redis common operations

keys *

Match all keys in the database

keys my*

Fuzzy matching, matching the key starting with my

exists key

Check whether the given key exists, if the key exists, return 1, otherwise return 0

del key1 key2

Delete one or more given keys, non-existent keys will be ignored, and the return value is the number of deleted keys

EXPIRE key1 100 //Expires after setting key1 100s

Set the time to live for a given key. When the key expires (the time to live is 0), it will be automatically deleted.

TTL key

In seconds, return the remaining time to live (TTL, time to live) of the given key.

return value:

When the key does not exist, -2 is returned.

When the key exists but the remaining lifetime is not set, -1 is returned.

Otherwise, return the remaining survival time of the key in seconds.

select index

Switch to the specified database, the database index number index is specified with a numeric value, with 0 as the starting index value.

Database 0 is used by default.

127.0.0.1:6379> select 0 uses the current database

OK

127.0.0.1:6379> select 1 Switch to database No. 1, there is one more command prompt below [1]

OK

127.0.0.1:6379[1]> select 3 Switch to database No. 3, the command prompt has one more [3]

OK

127.0.0.1:6379[3]>

MOVE key db

Move the key of the current database to the given database db.

return value:

Return 1 if the move is successful, and 0 if it fails.

PERSIST key

Remove the lifetime of a given key, and convert this key from "volatile" (with lifetime key) to "persistent" (a key without lifetime and never expires).

return value:

When the survival time is removed successfully, 1 is returned.

If the key does not exist or the time to live is not set for the key, 0 is returned.

RANDOMKEY

A key is randomly returned (not deleted) from the current database.

return value:

When the database is not empty, a key is returned.

When the database is empty, nil is returned.

RENAME key newkey

Rename the key to newkey.

When the key and newkey are the same, or the key does not exist, an error is returned.

When newkey already exists, the RENAME command will overwrite the old value.

return value:

It prompts OK when the name is changed successfully, and returns an error when it fails.

TYPE key

Returns the type of the value stored in the key.

return value:

none (key does not exist)

string

list (list)

set

zset (ordered set)

hash (hash table)

DBSIZE

Returns the number of keys in the current database.

INFO [section]

By giving the optional parameter section, you can make the command return only a certain part of the information, and display all the information without adding parameters

FLUSHDB

Clear all keys in the current database.

FLUSHALL

Clear the data of the entire Redis server (delete all keys of all databases).

SAVE

The save command executes a synchronous save operation, and saves all data snapshots of the current Redis instance to the hard disk in the form of RDB files.

CONFIG GET parameter

CONFIG GET accepts a single parameter parameter as the search key to find all matching configuration parameters, where the parameters and values ​​are arranged in "key-value pairs".

Data recovery: First define or determine the dir directory and dbfilename, then put the backup rdb file under the dir directory, restart the redis service to restore the data

Guess you like

Origin blog.51cto.com/11451960/2640779