Redis can't connect, two solutions

Today, the redis of a certain project cannot be connected, so I will write a record of the two solutions I used (you can choose one of the two methods in actual use).


Our initial redis configuration on the application.yml file of the project is the server ip+port, but such a configuration project did not successfully connect to redis:

	redis:
    isRedisCache: 1   #是否使用redis缓存
    database: 0       # Redis数据库索引(默认为0
    host: 176.17.0.9   # Redis服务器地址
    port: 6379        # Redis服务器连接端口
    password: 123456  # Redis服务器连接密码(默认为空)
    timeout: 1000    # 连接超时时间(毫秒)

1. First check if there is any problem with the local redis connection

1. View redis on the server
[root@iZxxxxxxxxx20t8shdhZ ~]# docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                                      NAMES
137f72546f00   redis                         "docker-entrypoint.s…"   10 days ago      Up 10 days      0.0.0.0:6379->6379/tcp                     redis
2. Enter the container to test the connection

Enter the container: docker exec -it 137f72546f00 bash
to connect to redis: redis-cli -h ip address -p port
If prompted for a password, enter: auth password, such as auth 123456
If you can connect smoothly, use the ping command to check whether you can ping pong.
The following code example demonstrates:

[root@iZxxxxxxxxx20t8shdhZ ~]# docker exec -it 137f72546f00 bash
root@137f72546f00:/data# redis-cli -h 176.17.0.9 -p 6379
172.17.0.9:6379> ping
(error) NOAUTH Authentication required.
172.17.0.9:6379> auth 123456
OK
172.17.0.9:6379> ping
PONG
172.17.0.9:6379> 

It can be seen that the local connection is no problem, and redis can be pinged, which means that there is no problem with the local connection to redis

2. Two ways to connect to redis (you can choose one of the two)

Through the above test method, we know that there is no problem with redis itself, so we can use the following two methods to realize the redis connection of the project: one is to configure the Alibaba Cloud remote strategy, and the other is to replace the redis network address on the current server with the redis network address The connection host address.

1. The first method: configure the remote policy

If the project connection is unsuccessful, it means that the remote policy has not been activated. Then, according to the server address in the configuration file (as shown in the figure below),
insert image description here
log in to Alibaba Cloud/Tencent Cloud to find the security group of the cloud server and then configure the rules (specifically, how to configure can be done by Baidu):
insert image description here
after saving, the project can be connected to redis normally.

2. The second method: use the redis network address instead of the redis connection host address

First, find the network address of redis: docker inspect redis

[root@iZxxxxxxxxx20t8shdhZ ~]# docker inspect redis
	*前面的内容省略,只需要关注从下面的"IPAddress"*
 "Networks": {
    
    
                "master": {
    
    
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "redis",
                        "137f72546f59"
                    ],
                    "NetworkID": "507fcefa41123f2f2124467100e6ef772ef9dad7384e363a6c6c02433014ffa9",
                    "EndpointID": "734e9bf66314ce664382b1e6a5beada582789afc17dfdc1ee664462a59620e1a",
                    "Gateway": "174.12.0.1",
                    "IPAddress": "174.16.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:13:00:03",
                    "DriverOpts": null
                }
            }

Then put this 174.16.0.2 address into the application.yml file of the project as the redis connection host:

	redis:
    isRedisCache: 1   #是否使用redis缓存
    database: 0       # Redis数据库索引(默认为0
    host: 174.16.0.2   # Redis服务器的network地址
    port: 6379        # Redis服务器连接端口
    password: 123456  # Redis服务器连接密码(默认为空)
    timeout: 1000    # 连接超时时间(毫秒)

Repackage and deploy, and you can connect normally.

The above are two ways to connect to redis.

Note:
If you use the first method, that is, go to Alibaba Cloud to configure the policy, the redis host address in the yml file is still the address of the server itself, and you don’t need to change the configuration file; if you
use the second network address, you don’t need to configure any policies. But if you want to connect to the redis container in the server, you need to use the address + port of the network instead of the address of the server itself, otherwise you cannot connect.

Guess you like

Origin blog.csdn.net/Ivy_Xinxxx/article/details/125721865