How to install redis in docker and set password and connect

How to install redis in docker and set password and connect

Redis is a distributed cache service. Caching is also an indispensable means for large-scale system development and performance optimization. At this time, Redis was born. Since Redis caches data in the memory stick, its reading and writing speeds are very fast. Traditional relational databases are all on disk, so if you are filtering and querying a particularly large amount of data, it will be very slow, which will undoubtedly affect the users of our system.

The installation of redis is also very simple. We still use docker to install redis

1. Download redis from the docker repository

#Search for redis in the docker repository
docker search redis
#Download redis to the local warehouse without adding version number. The default is the latest version. docker pull redis
#View the downloaded container docker images

2. Use Docker to create and run the redis image and set the redis password

#Use docker run to create and start the container#--requirepass Set the password for connecting to redis docker run -p 6379:6379 --name redis -d redis:latest --requirepass "123456"
#Check if the container has been started docker ps

3. Connect to redis locally

#Local connection directly uses bash command to set the password and access with -a plus password [root@apg-server ~]# docker exec -it redis redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
#set a key test 127.0.0.1:6379> set name xiaomianyang
OK
#Query the key
127.0.0.1:6379> get name
"xiaomianyang"

4. Check the redis container IP address

[root@apg-server ~]# docker inspect redis | grep IPAddress
      "SecondaryIPAddresses": null,
      "IPAddress": "172.17.0.4",
          "IPAddress": "172.17.0.4",

5. Connect to redis remotely

#If it is on the local machine, use localhost, if it is somewhere else, use the host machine ip
[root@apg-server ~]# docker exec -it redis redis-cli -h localhost -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
localhost:6379> get name
"xiaomianyang"

At this point, redis has been successfully installed in docker. Later, when we use springBoot development, we can take advantage of the cache to improve the processing power of the system.

Supplementary knowledge: The complete process of installing Redis in Docker and configuring remote connection & precautions for pitfalls

1. Install Redis

Download the redis image through docker search redis and docker pull redis

2. Create a new mount configuration folder

Because of the default configuration of redis, you will find that you can only connect locally and cannot access remotely. Using Redis Desktop Manager to connect will result in an error, so you need to manually mount the redis configuration file

Create two new folders, data and conf, in any location.

eg:

mkdir -p /root/docker/redis/data

mkdir -p /root/docker/redis/conf

3. Add the configuration file redis.conf

Create a new file redis.conf in the newly created redis/conf with the following content:

#bind 127.0.0.1 //Allow remote connection protected-mode no
appendonly yes //persistence requirepass 123456 //password

4. Create and start the redis container

The execution command is as follows:

docker run --name my_redis -p 6379:6379 -v /root/docker/redis/data:/data -

v /root/docker/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf

The interpretation is as follows:

–name: Give the container a name

-p: port mapping host: container

-v: Mount custom configuration custom configuration: container internal configuration

-d: Run in the background

redis-server --appendonly yes: Execute the redis-server startup command in the container and turn on the redis persistence configuration

5. Startup successful, check the status

Check the startup status through docker ps to see if it is successful

6. Test the connection inside the container

Execute the docker exec -it my_redis redis-cli command to enter the terminal.

Log in using auth password.

The completed command is as follows:

[root@*** conf]# docker exec -it my_redis redis-cli
127.0.0.1:6379> set name wangcai
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set name wangcai
OK
127.0.0.1:6379> get name
"wangcai"

Note: If this error occurs:

(error) NOAUTH Authentication required.

Explanation: No password was entered for verification. Please enter: auth your password

7. Connect using the Redis Desktop Manager client

The interface is simple and easy to understand at a glance. Here are the pictures.

8. Summary

When an error occurs when starting the container port, you can use netstat -lntp | grep 6379 to check which program is occupying the port.

You can kill the program occupying the port through sudo kill 6379

If you use Alibaba Cloud, please be sure to open the corresponding port

The above operation of installing redis in docker, setting password and connecting is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the process of using redis to prevent API brushing and current limiting in Springboot
  • How to use Redis cache to store and read historical search keywords
  • Detailed explanation of go-redis implementation of redis distributed lock
  • Simple application implementation of RedisMQ in Asp.net core
  • A detailed tutorial on building SpringBoot2.X from scratch to integrate Redis framework
  • How to install and configure the Redis environment under Windows
  • Detailed explanation of how to use the six major data types in Redis
  • Redis Java Lettuce driver framework principle analysis
  • Redis API atomic operations and principle analysis

<<:  js to create a carousel effect

>>:  Sqoop export map100% reduce0% stuck in various reasons and solutions

Recommend

Notes on matching MySql 8.0 and corresponding driver packages

MySql 8.0 corresponding driver package matching A...

When modifying a record in MySQL, the update operation field = field + string

In some scenarios, we need to modify our varchar ...

Analysis of the problem of deploying vue project and configuring proxy in Nginx

1. Install and start nginx # Install nginx sudo a...

Detailed explanation of the two modes of Router routing in Vue: hash and history

hash mode (default) Working principle: Monitor th...

7 cool dynamic website designs for inspiration

In the field of design, there are different desig...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

js to realize web music player

This article shares simple HTML and music player ...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

About the correct way to convert time in js when importing excel

Table of contents 1. Basics 2. Problem Descriptio...

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

Some problems that may be caused by inconsistent MySQL encoding

Stored procedures and coding In MySQL stored proc...

Website background music implementation method

For individual webmasters, how to make their websi...

CSS3 uses the transition property to achieve transition effects

Detailed description of properties The purpose of...

Five solutions to cross-browser problems (summary)

Brief review: Browser compatibility issues are of...

About dynamically adding routes based on user permissions in Vue

Display different menu pages according to the use...