Redis installation (Linux and Docker)

What is a redis

Redis is a high performance key-value in-memory database. It supports five commonly used data structures: String String, Hash table, List List, Set Set, Zset ordered Set and so on.

What problems does Redis solve

  1. performance

A database read usually takes tens of milliseconds, while a Redisd read usually takes less than 1 millisecond. Usually, you can get tens or even hundreds of times of performance improvement by caching database data into Redis.

  1. concurrent

In the case of large concurrency, all the requests directly access the database, the database will appear connection exception, or even stuck in the database. In order to solve the problem of large and dead, it is common practice to use Redis to do a buffer operation, so that the request first access redis, rather than directly access the database.

Install and deploy Redis for Linux

Official website to download

1. Download and compile Redis

$ wget https:/ / download. Redis. IO/releases/redis - 6.2.5. Tar. Gz
$ tar xzf redis-6.2. 5.tar.gz
$ cd redis-6.2. 5
$ make
Copy the code

2. Start

./src/redis-server
Copy the code

3. Connect the client to the server

/redis-cli -h {host} -p {port}, then all operations are implemented in interactive mode, if the remote connection is not possible, modify the configuration file bind attribute

[root@172 redis-6.2. 5]# ./src/redis-cli
127.0. 01.:6379> set key1 value1
OK
127.0. 01.:6379> get key1
"value1"
127.0. 01.:6379> 
Copy the code

Start Redis in background process mode

1. Modify the redis.conf file

will

daemonize no
Copy the code

Modified to

daemonize yes
Copy the code

2. Start the redis.conf file

[root@172 redis-6.2. 5]# ./src/redis-server  /app/redis/redis-6.2. 5/redis.conf
[root@172 redis-6.2. 5] #Copy the code

3. The test

[root@172 redis-6.2. 5]# ./src/redis-cli
127.0. 01.:6379> set user1 ljw
OK
127.0. 01.:6379> get user1
"ljw"
127.0. 01.:6379> 
Copy the code

4. Stop the Redis service

[root@172 redis-6.2. 5]# ./src/redis-cli shutdown
[root@172 redis-6.2. 5] #Copy the code

Docker installed redis

1. Create a workspace

mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
Copy the code

2. Pull the Redis image

docker pull redis
Copy the code

3. Create a container

docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf
Copy the code

4. Modify the configuration file

Redis is non-persistent by default. Appendonly Yes in the configuration file to persist aOF.

Vim/mydata/redis/conf/redis. Conf # # insert appendonly yes to the following contentCopy the code

5. Restart

docker restart redis
Copy the code

6. Enter the container Redis client and test it

[root@172 redis-6.2. 5]# docker exec -it redis redis-cli
127.0. 01.:6379> set key1 ljw
OK
127.0. 01.:6379> get key1
"ljw"
127.0. 01.:6379> 
Copy the code