Little linux knowledge -- the whole process of CentOS7 installation, configuration and Redis development

Previously, mysql was used to store data as a persistent database. The data is stored in the hard disk. It is read and written frequently, and the rate is naturally not high;

It's natural to come into contact with redis. As a Nosql database, the data stored in redis is placed in the cache, and the reading and writing speed is greatly accelerated. Especially for the data with simple data relationship, redis is simply a dream database.

Here's how to use Redis.

Install Redis

It's recommended to use yum. It's simple and fast. Now, yum has to be used. You can't work overtime at night!

Installation command

yum install redis

Start redis

systemctl start redis.service

test

redis-cli ping

Seeing pong means success
Power on

systemctl enable redis.service

Restart redis

systemctl restart redis.service

Stop redis

systemctl stop redis.service

View redis running status

systemctl status redis.service

Redis installed this set with a bang.

Configure Redis

The configuration file after yum installation defaults to / etc/redis.conf. The original configuration file should not be explained in too detail. Although it is in English, there is a large description in front of each configuration field, which is enough for you to translate.

The following describes the configuration slightly related to security. Since the performance problem has not been involved, the performance configuration will be supplemented later
For the configuration of production environment, please refer to this blog Redis configuration details (production environment)

#By default, redis does not run in the background mode. If it needs to run in the background process, change the value of this item to yes, which is no by default,
#In the background mode, the PID file / var/run/redis.pid will be written 
daemonize no

#Specify that redis only receives requests from this IP address. If it is not set, all requests will be processed by default
bind 127.0.0.1

# Specify the redis listening port. The default value is 6379
port 6379 

# Configure the password. If this password is configured, you need to execute auth password before performing other query operations
# redis's query speed is very fast. How many 150K passwords can external users try in one second; So the password should be as long as possible
requirepass password

bind, which can limit who is allowed to connect.
Port can be modified to an uncommon port to avoid being scanned.
requirepass, you can do a simple login verification.
It solves the problems of who can connect, where to connect, and password. It is enough. If you want to be more secure, you can encrypt the stored data. But what valuable data can you have here?

Basic knowledge points

Why to use redis is fast. How to judge whether it is suitable for redis depends on the data type you store. Here is a brief introduction and a list of scenarios, which is enough for you to judge whether to use redis or mysql.

data structure

An important knowledge point of redis is the data structure, that is, the data types suitable for redis storage. Different storage data types can be adopted according to different use scenarios

In short,
Simple configuration parameters, such as one name, one value, and one-to-one, can be stored using a String
For example, multiple sets of parameters or data, a name, some values, and a pair of N, it is recommended to use a Hash dictionary to store them
Like the collection of multiple string data, list is used in order, set is used in disorder, and ordered set is used in decentralization

Operation command line window

When getting started, it is recommended to use the command line to familiarize yourself with how to use redis for data addition, deletion, modification and query.
In the shell, we use redis cli to connect to the redis server. By default, we will connect to the local 127.0.0.1:6379

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth password
OK
127.0.0.1:6379> 

Come on, the door of the new world has been opened! Get ready to accept the impact of redis.

Simple example

This usage is used to count ip traffic. mysql was originally used. Now we are going to use redis instead of mysql. The original definition in mysql is

ipaddruploaddownload
192.168.1.112
192.168.1.234
192.168.1.356

......
IP address is a fixed key value and name. It has two parameters: upload and download. This kind of data is stored, modified and read. It is recommended to use Hash dictionary to replace mysql table.

IP traffic storage creation (Zeroing)

127.0.0.1:6379> HMSET 192.168.32.202 upload 0 download 0
OK

Query whether an IP exists

127.0.0.1:6379> EXISTS 192.168.32.202
(integer) 1
127.0.0.1:6379> EXISTS 192.168.32.204
(integer) 0

IP traffic read

127.0.0.1:6379> HGET 192.168.32.202 upload
"0"
127.0.0.1:6379> HGET 192.168.32.202 download
"0"

IP traffic data accumulation

127.0.0.1:6379> HINCRBY 192.168.32.202 upload 10
(integer) 10
127.0.0.1:6379> HGET 192.168.32.202 upload
"10"
127.0.0.1:6379> HINCRBY 192.168.32.202 upload 10
(integer) 20
127.0.0.1:6379> HGET 192.168.32.202 upload
"20"

IP device record deletion

127.0.0.1:6379> DEL 192.168.32.202
(integer) 1
127.0.0.1:6379> EXISTS 192.168.32.202
(integer) 0

In addition, if the IP address needs to be stored, this group of string data in the same format belongs to the same storage unit and does not need to be sorted, it is recommended to use the Set to store it.

New IP record

127.0.0.1:6379> SADD ALLIPS 192.168.32.202
(integer) 1
127.0.0.1:6379> SADD ALLIPS 192.168.32.203
(integer) 1
127.0.0.1:6379> SMEMBERS ALLIPS
1) "192.168.32.202"
2) "192.168.32.203"

Query whether an IP is in it

127.0.0.1:6379> SISMEMBER ALLIPS 192.168.32.202
(integer) 1
127.0.0.1:6379> SISMEMBER ALLIPS 192.168.32.204
(integer) 0

After a brief introduction to the use of the command line, other operation commands can be found in Redis usage tutorial In-depth learning.

It is said that a man with clothes, a horse with a saddle, a dog with a bell can run happily. Only by selecting the right method can the data storage run fast.

The next chapter will discuss how to store data with redis in C language environment. Please look forward to it.
Tips for today:
Looking at the recruitment information every day and learning about everyone's recruitment needs is the fastest way to improve

Tags: Linux Database Redis

Posted on Wed, 20 Oct 2021 00:02:01 -0400 by kuleraen