Quick Guide: Install Redis on CentOS 7 to Build a High-Performance Key-Value Store Database

Navigation directory

    • 1. Install and use Redis
      • 1.1. Download the installation package
      • 1.2. Rename and move files
      • 1.3, redis compilation and installation
      • 1.4, redis start
      • 1.5. Set the background to start redis
      • 1.6. Set redis password
      • 1.7, set redis service remote access
      • 1.8. Client connection test of redis service
      • 1.9. Using the command line
      • 1.10, clear the cache

Redis (Remote Dictionary Server) is an open source high-performance key-value storage database that supports a variety of data structures, including strings, hashes, lists, sets, ordered sets, etc. Redis is widely concerned and used for its fast, reliable and flexible features.

As an in-memory data storage system, Redis is designed to provide high performance and low latency data access. It stores data in memory, enabling fast read and write operations, and ensures data persistence by writing data to disk using an asynchronous method. Redis also has advanced features such as publish/subscribe, transactions, and persistent snapshots, making it a powerful and flexible data storage solution.

Redis is widely used in many scenarios. It can be used as a cache layer to speed up the response speed of dynamic websites and applications; it can be used as a message broker to implement real-time data processing and publish/subscribe mechanism; it can also be used as a data store to provide persistent data storage and efficient data access.

1. Install and use Redis

1.1, download the installation package

Official website download address: Download | Redis

## Download the installation package
[root@localhost softpackage]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz

## Unzip the compressed file
[root@localhost softpackage]# tar -zxvf redis-6.2.6.tar.gz

1.2, renaming and moving files

## Move the installation directory to the /usr/local/ directory,
## Rename redis-6.2.6 to the redis directory, use the mv /home/redis-6.2.6 /usr/local/redis command, so the following operations are performed.
[root@localhost softpackage]# mv redis-6.2.6 /usr/local/redis
[root@localhost softpackage]# cd /usr/local/redis
[root@localhost redis]# ll
Total usage 236
-rw-rw-r-- 1 root root 33624 Oct 4 2021 00-RELEASENOTES
-rw-rw-r-- 1 root root 51 Oct 4 2021 BUGS
-rw-rw-r-- 1 root root 5026 Oct 4 2021 CONDUCT
-rw-rw-r-- 1 root root 3384 Oct 4 2021 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Oct 4 2021 COPYING
drwxrwxr-x 7 root root 145 Oct 4 2021 deps
-rw-rw-r-- 1 root root 11 Oct 4 2021 INSTALL
-rw-rw-r-- 1 root root 151 Oct 4 2021 Makefile
-rw-rw-r-- 1 root root 6888 Oct 4 2021 MANIFESTO
-rw-rw-r-- 1 root root 21567 Oct 4 2021 README.md
-rw-rw-r-- 1 root root 93724 Oct 4 2021 redis.conf
-rwxrwxr-x 1 root root 275 Oct 4 2021 runtest
-rwxrwxr-x 1 root root 279 Oct 4 2021 runtest-cluster
-rwxrwxr-x 1 root root 1079 Oct 4 2021 runtest-moduleapi
-rwxrwxr-x 1 root root 281 Oct 4 2021 runtest-sentinel
-rw-rw-r-- 1 root root 13768 Oct 4 2021 sentinel.conf
drwxrwxr-x 3 root root 4096 Oct 4 2021 src
drwxrwxr-x 11 root root 182 Oct 4 2021 tests
-rw-rw-r-- 1 root root 3055 Oct 4 2021 TLS.md
drwxrwxr-x 9 root root 4096 Oct 4 2021 utils
[root@localhost redis]#

1.3, redis compilation and installation

## redis compile, execute make command
[root@localhost redis]# make
## redis installation Execute the make install command
[root@localhost redis]# make install
cd src & & make install
make[1]: enter the directory "/usr/local/redis/src"
    CC Makefile.dep
make[1]: Leaving directory "/usr/local/redis/src"
make[1]: enter the directory "/usr/local/redis/src"

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: Leaving directory "/usr/local/redis/src"

1.4, redis startup

## For the convenience of management, move the conf configuration file and common commands in the Redis6.2.6 directory to the unified directory.
## Switch to the redis root directory and create bin and etc files
## Move the redis.conf file to the /usr/local/redis/etc/ directory, and move the mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server file to /usr/local /redis/bin/directory
[root@localhost redis]# mkdir etc
[root@localhost redis]# mkdir bin
[root@localhost redis]# mv redis.conf /usr/local/redis/etc/
[root@localhost redis]# cd src/
[root@localhost src]# mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /usr/local/redis/bin/
[root@localhost src]# ll ../bin/
Total usage 37512
-rwxrwxr-x 1 root root 735 Oct 4 2021 mkreleasehdr.sh
-rwxr-xr-x 1 root root 4830464 Nov 18 18:18 redis-benchmark
-rwxr-xr-x 1 root root 9521712 Nov 18 18:18 redis-check-aof
-rwxr-xr-x 1 root root 9521712 Nov 18 18:18 redis-check-rdb
-rwxr-xr-x 1 root root 5004792 Nov 18 18:18 redis-cli
-rwxr-xr-x 1 root root 9521712 Nov 18 18:18 redis-server
[root@localhost src]#

## Execute the redis-server command to start the redis service
[root@localhost src]# cd ..
[root@localhost redis]#
[root@localhost redis]# ls
00-RELEASENOTES BUGS CONTRIBUTING deps INSTALL MANIFESTO runtest runtest-moduleapi sentinel.conf tests utils
bin CONDUCT COPYING etc Makefile README.md runtest-cluster runtest-sentinel src TLS.md
[root@localhost redis]# ./bin/redis-server
9193:C 18 Nov 2022 18:27:26.765 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9193:C 18 Nov 2022 18:27:26.765 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=9193, just started
9193:C 18 Nov 2022 18:27:26.765 # Warning: no config file specified, using the default config. In order to specify a config file use ./bin/redis-server /path/to/redis.conf
9193:M 18 Nov 2022 18:27:26.766 * Increased maximum number of open files to 10032 (it was originally set to 1024).
9193:M 18 Nov 2022 18:27:26.766 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```. ```\/ _.,_ ''-._
 ( ' , .-` | `, ) Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
 | `-._ `._ / _.-' | PID: 9193
  `-._ `-._ `-./ _.-' _.-'
 |`-._`-._ `-.__.-' _.-'_.-'|
 | `-._`-._ _.-'_.-' | https://redis.io
  `-._ `-._`-.__.-'_.-' _.-'
 |`-._`-._ `-.__.-' _.-'_.-'|
 | `-._`-._ _.-'_.-' |
  `-._ `-._`-.__.-'_.-' _.-'
      `-._ `-.__.-' _.-'
          `-._ _.-'
              `-.__.-'

9193:M 18 Nov 2022 18:27:26.766 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9193:M 18 Nov 2022 18:27:26.766 # Server initialized
9193:M 18 Nov 2022 18:27:26.766 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9193: M 18 Nov 2022 18:27:26.766 * Ready to accept connections

1.5, set the background to start redis

## Edit the redis.conf file, change the daemonize attribute to yes, and execute the vim /usr/local/redis/etc/redis.conf command
[root@localhost redis]# vim /usr/local/redis/etc/redis.conf
## Execute the ./bin/redis-server /usr/local/redis/etc/redis.conf command to start the redis service
[root@localhost redis]# ./bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost redis]#

1.6, set redis password

## Edit the redis.conf file and find the parameter requirepass. If the requirepass parameter is not added and the password is set, refer to the figure below and execute the vim /usr/local/redis/etc/redis.conf command
[root@localhost redis]# vim /usr/local/redis/etc/redis.conf
## After saving, restart the redis service to take effect.
[root@localhost redis]# ps -ef|grep redis
root 9193 2154 0 18:27 pts/0 00:00:00 ./bin/redis-server *:6379
root 24406 9201 0 18:35 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]# kill -9 9193
[root@localhost redis]# ./bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost redis]# ps -ef|grep redis
root 24408 1 0 18:35 ? 00:00:00 ./bin/redis-server 127.0.0.1:6379
root 24414 9201 0 18:35 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]#

1.7, set redis service remote access

## Edit the redis.conf file, find the bind 127.0.0.1 -::1 configuration, and comment it out with #, as shown in the figure below.
[root@localhost redis]# vim /usr/local/redis/etc/redis.conf
## Then execute the ps -ef|grep redis command to find the started redis service, and use the kill command to kill the redis service process. Then execute the ./bin/redis-server /usr/local/redis/etc/redis.conf command to start the redis service.
[root@localhost redis]# ps -ef|grep redis
root 9193 2154 0 18:27 pts/0 00:00:00 ./bin/redis-server *:6379
root 24406 9201 0 18:35 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]# kill -9 9193
[root@localhost redis]# ./bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost redis]# ps -ef|grep redis
root 24408 1 0 18:35 ? 00:00:00 ./bin/redis-server 127.0.0.1:6379
root 24414 9201 0 18:35 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]#

1.8, client connection test of redis service

redis client download

Redis has 16 warehouses by default, numbered from 0 to 15. The number of warehouses can be set through the configuration file, but not more than 16, and the warehouse name cannot be customized.

1.9, command line usage

## Switch to the redis directory and run:
[root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ls
mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server
[root@localhost bin]# ./redis-cli
127.0.0.1:6379>auth 123456
OK
127.0.0.1:6379> set mykey rucoding
OK
127.0.0.1:6379> get mykey
"rucoding"
127.0.0.1:6379>

1.10, clear cache

Redis cleans the cache and operates with caution according to the actual situation

[root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ls
mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server
[root@localhost bin]# ./redis-cli
127.0.0.1:6379>auth 123456
OK
127.0.0.1:6379>
### Query the number of database keys
127.0.0.1:6379> dbsize
(integer) 1
127.0.0.1:6379>
### Clear cache
127.0.0.1:6379> flushall # Clear the keys and data of the entire Redis server (delete all keys in all databases)
OK
127.0.0.1:6379>
### Delete the value of the specified index
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379>
### Use keys * to verify whether it is empty
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> exit # Exit