Redis is one of the most popular NoSQL databases. So I decided to share the installation and configuration of Redis on Linux.
1. Download the Redis source code and decompress it
Download the source code on the official website download page, as shown in figure: standby address, extract code 2333
I downloaded a redis-6.0.8.tar.gz file here, decompressed it to the current directory and entered the decompressed folder (the operation name in the command depends on what you downloaded, this command takes my own as an example) :
Tar -xvf redis-6.0.8.tar.gz CD redis-6.0.8Copy the code
2. Compile the source code
The first thing we need to make sure is that we have installed GCC,libc6,libc6-dev on our machine (if we are 32-bit Linux or need to compile 32-bit redis, we need to install g++-multilib). If not, run the following command to install it (skip this step if you have installed these packages) :
sudo apt install gcc gcc-multilib g++ g++-multilib make libc6 libc6-dev
Copy the code
Then start compiling Redis. Now that you have unpacked and entered its source directory, run the following command to compile:
make
Copy the code
Hint: It’s a good idea to run ‘make test’; “Indicates successful compilation.
Then run the following command to install Redis on the system:
make install
Copy the code
To install Redis in the specified location, run the following command:
Make PREFIX= Location (absolute path) to which you want to installCopy the code
For example to install to /home/swsk33/redis:
make PREFIX=/home/swsk33/redis install
Copy the code
At this point, Redis is compiled and installed!
3. Write the configuration file and start Redis
At this point, you can start directly by typing redis-server. Redis can be run by specifying a profile, preferably starting from a profile.
Create a new file, such as redis-config.conf, and add the configuration content.
Common configurations are as follows:
The serial number | Configuration items | instructions |
---|---|---|
1 | daemonize yes/no | By default, Redis does not run as a daemon process. You can modify this configuration item by running yes to enable the daemon process |
2 | Pidfile pid Indicates the file location | When Redis is running as a daemon, the pid is written to the /var/run/redis.pid file by default, which can be specified using the pidfile |
3 | Port User-defined port number | Specifies the Redis listening port. The default port is 6379 |
4 | Bind Indicates the IP address to be bound | Address of the host to be bound. If you do not write this line, then all computers on the Internet can connect to the Redis of this server |
5 | The timeout milliseconds | How many seconds after the client is idle to close the connection? If 0 is specified, the function is disabled |
6 | Loglevel loglevel | The Redis supports four log levels: DEBUG, verbose, Notice, and Warning. The default level is Notice |
7 | logfile stdout | The default logging mode is stdout. If Redis is configured to run in daemon mode and the logging mode is set to stdout here, logs are sent to /dev/null |
8 | Databases Number of databases | Set the number of databases. The default database is 0. You can specify the database ID on the connection using the SELECT command |
9 | save | The default Redis profile provides three conditions:save 900 1 save 300 10 save 60 10000 Represents one change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10,000 changes in 60 seconds, respectively. |
10 | rdbcompression yes/n | Specifies whether to compress data when stored to a local database. The default value is yes. Redis uses LZF compression. This option can be enabled when there is insufficient space |
11 | Dbfilename Specifies the database name. RDB | Specifies the name of the local database file, which defaults to dump.rdb |
12 | Dir Directory for storing the database | Specifies a directory for storing the local database. The default directory is the current directory |
13 | Requirepass password | Set the Redis connection password. If the password is configured, the client needs to pass the password to connect to RedisAuth password For example, to set the password to 123456:requirepass 123456 |
14 | Maxclients Maximum number of connections | Set the maximum number of client connections that Redis can open at the same time. The default value is unlimited. The maximum number of client connections that Redis can open at the same time is the maximum number of file descriptors that Redis can open at the same time. When the number of client connections reaches the limit, Redis closes new connections and returns a Max number of clients reached error message to the client |
15 | Maxmemory Maximum memory usage in bytes | Specifies the maximum memory limit for Redis. Redis loads data into memory when it starts. When the maximum memory is reached, Redis will first try to clear the expired or expended keys. Redis – new VM mechanism will store Key in memory and Value in swap |
16 | appendonly yes/no | Specifies whether to log data after each update. By default, Redis asynchronously writes data to disk. If this function is disabled, data may be lost for a period of time during a power failure. Because redis synchronizes data files according to the save condition above, some data will only exist in memory for a period of time. The default value is no |
17 | Appendfilename Updates the log file name. aof | Specifies the name of the update log file, which defaults to appendonity.aof |
18 | Appendfsync Updates the log condition | Specifies the update log condition. There are three optional values:No: wait for the operating system to synchronize cache data to disk (fast). Always: write data to disk manually by calling fsync() after each update operation (slow, safe) Everysec: synchronizes once per second (intermediate, default) |
19 | vm-enabled yes/no | Specifies whether to enable the virtual memory mechanism. The default value is no. For a brief introduction, the VM mechanism stores data in separate pages. Redis swaps pages with less access to disk, namely cold data, and pages with more access to disk are automatically swapped out to the memory |
20 | Vm-swap-file Specifies the path of the virtual file | Virtual memory file path. The default value is/TMP /redis.swap |
21 | vm-max-memory 0 | All data larger than vM-max-memory is stored in virtual memory. No matter how small vm-max-memory is set, all index data is stored in memory (Redis index data is keys). That is, when vm-max-memory is set to 0, All values actually exist on disk. The default value is 0 |
22 | vm-page-size 32 | The Redis swap file is divided into many pages. One object can be stored on multiple pages, but one page cannot be shared by multiple objects. The vM-page size should be set according to the size of the data stored. The page size is best set to 32 or 64bytes. If you store a large object, you can use a larger page, or if you’re not sure, use the default |
23 | vm-pages 134217728 | Set the number of pages in the swap file. Since page tables are stored in memory, every 8 pages on disk consumes 1byte of memory |
24 | vm-max-threads 4 | Set the number of threads that can access the swap file to no more than the number of cores on the machine. If set to 0, all operations on the swap file will be serial, which may cause a long delay. The default value is 4 |
25 | glueoutputbuf yes | Set whether to combine smaller packets into one packet when sending a reply to the client. This parameter is enabled by default |
26 | hash-max-zipmap-entries 64 | Specifies a special hash algorithm to be used when a certain number or maximum element exceeds a critical value |
27 | activerehashing yes/no | Specifies whether to enable the reset hash. It is enabled by default |
28 | include /path/to/local.conf | You can use the same profile across multiple Instances of Redis on the same host, while each instance has its own specific profile |
You do not need to write all the above configurations. For example, my configuration file looks like this:
port 25002
dir /root/Temp/db
requirepass 12345678
Copy the code
It is recommended that RequirePass be written to guarantee security. If necessary, you can use bind to configure the bound IP address, so that only the bound IP address can access the Redis database.
Then start the Redis server:
Redis-server Location of the configuration fileCopy the code
For example, mine:
redis-server /home/swsk33/redis/redis-config.conf
Copy the code
If you use a custom location for make install (using the PREFIX argument), go to the bin folder in your installation directory and run the following command:
./redis-server Configuration file pathCopy the code
If this screen is displayed, the startup is successful:
On the server, it is recommended to use screen software to create a new window and run the Redis server in it, so that it can be hung on the server and run. The use of Screen is not covered here.
4. Remotely connect to the Redis server
Make sure that the bind value is not configured or that the bind value is your IP address and the port is open. Remote connections also require compiling and installing Redis on your own computer. Windows compilation can be baidu, the method is similar.
In this example, run the following command to remotely connect to a Linux PC:
Redis-cli -h Server IP -p Port number of RedisCopy the code
Local connection is the same command, with IP address 127.0.0.1
Connect to the redis command line:
Then use the AUTH command to enter password authentication:
Auth redis passwordCopy the code
The password is the configuration value of RequirePass in the previous configuration file.
If OK is displayed, the connection authentication is successful!
Finally, run the quit or exit command to disconnect the connection.