问题描述
我是在阿里云服务器上通过docker安装的redis.在做jedis测试时出现了以下问题
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
at com.yp.TestPing.main(TestPing.java:20)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 6 more
问题排查过程
修改redis的redis.conf配置文件
通过查阅资料得知需要先修改redis.conf文件,但是我的docker中创建redis容器时并没有挂载文件所以需要删除原来的容器然后重新创建容器并挂载redis的配置文件
本机中redis的redis.conf文件的位置
我本机中的redis是通过宝塔下载的,所以需要到宝塔先找到本机中的redis的配置文件位置
在本机上确认一下是否有该文件
重新创建redis容器,将配置文件进行挂载
删除容器
[root@iZf8zeb4pvftjcmv58dmy8Z redis]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ee93b679ab1 redis "docker-entrypoint.s…" 25 hours ago Up 20 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp test-redis
[root@iZf8zeb4pvftjcmv58dmy8Z redis]
1ee93b679ab1
[root@iZf8zeb4pvftjcmv58dmy8Z redis]
1ee93b679ab1
[root@iZf8zeb4pvftjcmv58dmy8Z redis]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
创建新的容器并进行挂载
[root@iZf8zeb4pvftjcmv58dmy8Z redis]
1:C 16 Mar 2022 08:31:55.259
1:C 16 Mar 2022 08:31:55.259
1:C 16 Mar 2022 08:31:55.259
1:M 16 Mar 2022 08:31:55.260 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 16 Mar 2022 08:31:55.260
1:M 16 Mar 2022 08:31:55.260
1:M 16 Mar 2022 08:31:55.260 * Ready to accept connections
修改本地的配置文件
1.设置访问redis的密码:requirepass 要设置密码 2.注释bind 127.0.0.1 (重启redis-server服务,进入redis后要先验证密码,用这个命令:auth 密码 ,然后ping一下看有没有配置成功)
开启防火墙
firewall-cmd --state
firewall-cmd --permanent --zone=public --add-port=6379/tcp
firewall-cmd --reload
config set protected-mode "no"
完成
|