(Power Section) Redis7- Chapter 2 Installation and Configuration of Redis

2 Redis installation and configuration

Here is to install Redis into the Linux system.

2.1 Redis installation

2.1.1 Clone and configure the host

  • Modify the hostname: /etc/hostname
  • Modify network configuration: /etc/sysconfig/network-scripts/ifcfg-ens33

2.1.2 Preparations before installation

2.1.2.1 Install gcc

Since Redis is written in C/C++ language, and the Redis installation package downloaded from the official website needs to be compiled before it can be installed, so the relevant compiler must be used to compile it. For C/C++ language compilers, gcc and gcc-c++ are the most used, and these two compilers are not installed in CentOS7, so these two compilers must be installed first.
GCC, GNU Compiler Collection, GNU Compiler Collection.

2.1.2.2 Download Redis

The official website of redis is: http://redis.io . Click the link below to download directly.


2.1.2.3 Upload to Linux

Upload the downloaded compressed package to the /opt/tools directory of Linux.

2.1.3 Install Redis

2.1.3.1 Decompress Redis

Unzip Redis into the /opt/apps directory.

Go to the /opt/apps directory and rename the Redis decompression package directory to redis (it doesn’t matter if you don’t change the name).

2.1.3.2 Compile

The compilation process is carried out according to the Makefile file, and the file already exists in the Redis decompression package. So it can be compiled directly.

Enter the decompression directory, and then execute the compilation command make.

When you see the following prompt, it means the compilation is successful.

2.1.3.3 Installation

Execute make install to install the compiled installation package in Linux.

It can be seen that three components are installed: redis server, client and a performance testing tool benchmark.

2.1.3.4 View the bin directory

After the installation is complete, open the /usr/local/bin directory, and you can see that many files have appeared.

You can see through echo $PATH that the /usr/local/bin directory exists in this system variable, so that these commands can be executed in any directory.

2.1.4 Redis start and stop

2.1.4.1 Foreground start

Execute the redis-server command in any directory to start Redis. This startup method will occupy the current command line window.

Open another session window, you can view the current Redis process, the default port number is 6379.

Redis can be stopped by the Ctrl + C command.

2.1.4.2 Imperative background startup

Use the nohub command and add an & symbol at the end to make the program to be started run as a daemon process in the background. The advantage of this is that after the process starts, it will not occupy a session window, and it will also create a nohup.out file in the current directory, that is, the current directory where the startup command is run, to record the Redis operation log.

2.1.4.3 Redis stop

Redis can be stopped through the redis-cli shutdown command.

2.1.4.4 Configuration background startup

Use the nohup command to start Redis in the background, but it is troublesome to type nohup and & every time. The purpose of background startup can be achieved by modifying the core configuration file redis.conf of Redis in Linux. The redis.conf file is under the root of the Redis installation directory.

Change the daemonize attribute value from no to yes to make the Redis process run as a daemon process.

After modifying and starting Redis, there is no need to type nohup and &, but the Redis configuration file used for starting must be specified. Why is this?
When using the nohup redis-server & command to start Redis, the default values ​​of Redis parameters have been set in the startup item, and Redis will start according to these set parameters. However, these parameters can be modified in the configuration file. After modification, the configuration file to be loaded needs to be specified in the startup command, so that the parameter values ​​in the configuration file will override the original default values.
Redis has provided us with a configuration file template, which is the redis.conf file in the root directory of the Redis installation directory. Since the redis.conf configuration file has just been modified, it is necessary to display the configuration file to be loaded when starting Redis. The configuration file should follow immediately after redis-server.

2.2 Configuration before connection

Redis is an in-memory database server. Just like MySQL, its operations also need to be performed through the client. To enable the client on the remote host to connect to and access Redis on the server, the server must first configure as follows.

2.2.1 Bind client IP

Redis can limit the client IP that can access itself by modifying the configuration file.

After the above settings, only the current host is allowed to access the current Redis, and other hosts are not allowed to access. So, if you don't want to limit the clients to access, just comment out this line.

2.2.2 Disable protected mode

默认保护模式是开启的。其只允许本机的客户端访问,即只允许自己访问自己。但生产中应该关闭,以确保其它客户端可以连接Redis。

2.2.3 Set access password

Set an access password for Redis, which can authenticate users who want to read/write Redis. Users without a password can log into Redis, but cannot access it.

2.2.3.1 Password setting

The access password is set in the redis.conf configuration file. The default is commented out, with no password.

Users who do not log in with a password cannot read/write Redis.

2.2.3.2 Using passwords

There are two ways to use the password: if you do not use a password when you log in, enter the password first when you access; when you log in, you directly use the password to log in, and you do not need to enter the password again when you access.

2.2.3.2.1 Login without password

2.2.3.2.2 Using a password when logging in

2.2.3.2.3 Use password on logout

2.2.3.3 Attention

In order to facilitate the later study, we will not set the access password here, just comment it out.

2.2.4 Disable/Rename Commands

There are two very dangerous commands to learn later: flushal and flushdb. They are all used to directly delete the entire Redis database. Making them freely available to users may compromise data security. Redis can disable the use of these commands by modifying the configuration file, or rename these commands. The following configuration disables the flushall and flushdb commands.
Of course, do not disable them for the time being during the learning process.

2.2.5 Start Redis

Of course, to enable the client to connect to Redis, you must enable Redis on the server.

2.3 Classification of Redis clients

Redis clients also have multiple types like MySQL clients: command line clients, graphical interface clients, and Java code clients.

2.3.1 Command line client

Redis provides a basic command-line client. The command to open the command line client is redis-cli.

  • -h: Specifies the IP of the Redis server to be connected.
  • -p: Specify the port number of Redis to connect to.

If the local Redis is connected, and the port number has not changed, keeping the default 6379, the -h and -p options can be omitted.

2.3.2 Graphical interface client

2.3.2.1 Redis Desktop Manager

There are many graphical interface clients for Redis, the most famous of which is the client of Redis Desktop Manager.

2.3.2.2 RedisPlus

RedisPlus is an open source and free desktop client software developed for Redis visual management. It supports the three major system platforms of Windows, Linux and Mac. RedisPlus provides a more efficient, convenient and fast user experience, and has a more modern user interface style.

2.3.3 Java code client

The so-called Java code client is a set of APIs for operating Redis, and its function is like JDBC, so the Java code client is actually one or more Jar packages that provide an operation interface for Redis.
There are many APIs for Redis operations, such as jdbc-redis, jredis, etc., but the most commonly used and famous one is Jedis.

2.4 Detailed explanation of Redis configuration file

The core configuration file redis.conf of Redis is in the installation root directory and contains more than 2000 lines by default. These contents are divided into many parts according to the functions. Some important parts are introduced below.

2.4.1 Basic description


This part is mainly to give some instructions, including three parts:

  • Lines 1-6 are used to illustrate that if you want to start Redis, you need to point out the path of the configuration file.
  • Lines 8-16 are used to explain the capacity units and meanings that can be used in the current configuration file.
  • Line 18 is used to indicate that these capacity units are not case sensitive.

2.4.2 includes


Specifies the profiles to include in the current profile. The purpose of this is mainly to facilitate configuration information management: the configurations of different scenarios can be defined separately, and then selected and included in different configuration files according to different scenarios in the current core configuration file.

2.4.3 modules


Different third-party modules can be loaded in the Redis configuration file to enhance and extend the functions of Redis.

2.4.4 network


The Network configuration module is a more important part, mainly for network-related configuration. Among the more important ones are:

2.4.4.1 bind


Specify the client IP that can access the current Redis service. By default, only local access is allowed, that is, the current Redis can access itself. It is generally commented out to make it accessible to all other clients.

2.4.4.2 protected-mode


Protected mode is enabled by default. It only allows the client of this machine to access, that is, only allows itself to access itself. But it should be closed in production to ensure that other clients can connect to Redis.

2.4.4.3 port


The connection port number that Redis monitors, the default is 6379.

2.4.4.4 tcp-backlog


tcp-backlog is a queue of TCP connections, which is mainly used to solve the problem of slow client connections in high concurrency scenarios. The value set here is the length of the queue. This queue is related to the three-way handshake of the TCP connection. Different Linux kernels have different types of elements (client connections) stored in the backlog queue.

  • Before the Linux kernel version 2.2, all client connections that have completed the first handshake are stored in this queue, including client connections that have completed the three-way handshake. Of course, the connection in the backlog queue at this time also has two states: the connection state that has not completed the three-way handshake is SYN_RECEIVED, and the connection state that has completed the three-way handshake is ESTABLISHED. Only connections in the ESTABLISHED state will be processed by Redis.
  • After version 2.2 of the Linux kernel, two queues are maintained in the TCP system: SYN_RECEIVED queue and ESTABLISHED queue. The SYN_RECEIVED queue stores the connections that have not completed the three-way handshake, and the ESTABLISHED queue stores the connections that have completed the three-way handshake. The backlog at this time is the ESTABLISHED queue.

Check the Linux kernel version:

The length of the backlog queue in TCP is determined by the kernel parameter somaxconn in Linux. Therefore, the length of the queue in Redis is determined by the Redis configuration file settings and somaxconn: take the minimum value among them.
Check the value of somaxconn in the current Linux kernel.

In a production environment (especially in a high-concurrency scenario), the value of the backlog should be larger, otherwise system performance may be affected.
Modify the /etc/sysctl.conf file, and add the following content at the end of the file:

After the modification, the virtual machine can be restarted, or the new modification can take effect by executing the following command.

2.4.4.5 timeout


Idle timeout. When the idle time between the client and Redis exceeds this time, the connection is automatically disconnected. The unit is second. The default value is 0, which means never timeout.

2.4.4.6 tcp-keepalive


This configuration is mainly used to set the survivability time interval for Redis to detect all clients connected to it, in seconds. Generally, it is configured when the idle timeout timeout is set to 0.

2.4.5 general

2.4.5.1 daemonize


This configuration can control whether Redis starts in daemon mode, that is, whether it is started in the background. yes is to start in the background.

2.4.5.2 pidfile


This configuration is used to specify the file written by the pid when Redis is running. Regardless of whether Redis is started in daemon mode or not, the pid will be written to the configured file.
Note that if the pid file is not configured, the effect of the pid file will be different for different startup methods:

  • Start with a daemon process (start in the background, daemonize is yes): the pid file is /var/run/redis.pid.
  • Start in the foreground (daemonize is no): do not generate pid files

2.4.5.3 loglevel


Configure the logging level. There are four levels in Redis, from low to high:

  • debug: A lot of information can be obtained, and it is generally used during development and testing.
  • verbose: You can get a lot of less useful information, but not as much as the debug level.
  • notice: You can get as much information as you want in production, the default level.
  • warning: Only log very important/critical information.

2.4.5.4 logfile


Specifies the log file. If set to an empty string, forces logging to the standard output device (monitor). If you are using the daemon startup method, setting it to an empty string means that the log will be sent to the device /dev/null (null device).

2.4.5.5 databases


Set the number of databases. The default database is database number 0. A different database can be selected on a per-connection basis using select, where dbid is a number between 0 and 'databases'-1'.

2.4.6 security


User setting ACL permission, Redis access password related configuration. The most commonly used in this module is the requirepass attribute.

Set the client access password. When commented out, there is no password.

2.4.7 clients


This module is used to set client-related properties, which contains only one property maxclients.
maxclients is used to set the number of client connections that Redis can handle concurrently, and the default value is 10000. If the maximum number of connections is reached, new connections will be rejected and an exception message will be returned: the maximum number of connections has been reached.
Note that this value cannot exceed the threshold of the maximum number of open file descriptors supported by the Linux system. The way to view this threshold is as follows. To modify this value, you can modify the /etc/secutiry/limits.conf file (check it yourself).

2.4.8 memory management


This configuration controls the maximum available memory and related content removal issues.

2.4.8.1 maxmemory


Sets the memory usage limit to the specified number of bytes. When the memory limit is reached, Redis will try to delete eligible keys according to the selected eviction policy maxmemory-policy.
If the key cannot be removed according to the eviction policy, an error will be returned to the write operation command, but it has no effect on the read-only command.

2.4.8.2 maxmamory-policy


This property is used to set how Redis will choose what to remove when maxmemory is reached. Of course, if there is no content to be deleted that matches the corresponding policy, an errors response will be given when the write command is executed. Redis supports 8 removal strategies:

  • volatile-lru: Use the approximate LRU algorithm to remove, only applicable to keys with an expiration time set.
  • allkeys-lru: Use the approximate LRU algorithm to remove, applicable to all types of keys.
  • volatile-lfu: Use the approximate LFU algorithm to remove, only applicable to keys with an expiration time set.
  • allkeys-lfu: Use the approximate LFU algorithm to remove, applicable to all types of keys.
  • volatile-random: Randomly remove a key, only applicable to keys with an expiration time set.
  • allkeys-random: Randomly remove a key, applicable to all types of keys.
  • volatile-ttl: Remove the key closest to the expiration time.
  • noeviction: Do not remove anything, just return an error during the write operation, the default value.

2.4.8.3 maxmemory-samples


This attribute is used to specify the number of samples to pick the key to delete. The selection of samples adopts the LRU algorithm, which cannot be modified. However, if the key to be removed is selected from the sample, the strategy specified by maxmamory-policy is adopted.

2.4.8.4 maxmemory-eviction-tenacity


Set removal tolerance. The smaller the value, the lower the tolerance, and the smaller the removal delay of the data to be removed; the larger the value, the higher the tolerance, and the greater the removal delay of the data to be removed.

2.4.9 threaded I/O


This configuration module is used to configure Redis' support for the multi-threaded IO model.

2.4.9.1 io-threads


This property is used to specify the number of threads to use when the multi-threaded IO model is enabled.
View the number of CPUs contained in the current system:

2.4.9.2 io-threads-do-reads


This attribute is used to enable multi-threaded processing of read requests in the multi-threaded IO model.

Guess you like

Origin blog.csdn.net/f5465245/article/details/130861996