Jedis 連線 Redis報JedisConnectionException: java.net.ConnectException: Connection refused
總結一下Jedis 連線 Redis 常見錯誤
1、SocketTimeoutException 連線超時
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:154)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)
at redis.clients.jedis.Connection.sendCommand(Connection.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:88)
at redis.clients.jedis.Jedis.get(Jedis.java:97)
這種情況先排除是否開啟了redis,或者ip,埠是否正確。如果都沒問題,那八成就是防火牆了。
可能是redis的6379埠無法訪問,請先在cmd中輸入命令
telnet 127.0.0.1 6379
如果不能訪問,需要在遠端機器關掉防火牆或者新增允許通過
CentOS,root使用者登陸,輸入如下命令開啟埠:
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
重新整理自動啟動儲存
/etc/rc.d/init.d/iptables save
2、Connection refused 拒絕訪問
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
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 practice.RedisJava.main(RedisJava.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
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.conf配置檔案,使用vim命令開啟,找到bind那行修改後,wq儲存退出,重啟redis-server。
編輯後如上圖,用#號註釋。
3、DENIED Redis is running in protected mode (保護模式)
redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
at redis.clients.jedis.Protocol.processError(Protocol.java:117)
at redis.clients.jedis.Protocol.process(Protocol.java:142)
at redis.clients.jedis.Protocol.read(Protocol.java:196)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:207)
at redis.clients.jedis.Connection.getBulkReply(Connection.java:196)
at redis.clients.jedis.Jedis.get(Jedis.java:98)
at com.zyao89.jedis.JedisTest.test1(JedisTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
報錯資訊很長,但是主要是說redis開啟了protected mode,這也是Redis3.2加入的新特性,開啟保護模式的redis只允許本機登入,同樣設定在配置檔案redis.conf中,如圖
這裡原來是yes代表開啟了保護模式,後面可以填密碼也可以填no代表關閉,我們這裡選擇關閉保護模式,wq儲存退出後再重啟redis-server。
作者:Zyao89;轉載請保留此行,謝謝;
個人部落格:https://zyao89.cn
相關文章
- java.net.ConnectException: Connection refused (Connection refused)JavaException
- java.net.ConnectException: Connection refused 異常JavaException
- nacos Connection refused (Connection refused)
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolRedisclientException
- Jedis使用連線池操作redis叢集Redis
- 連線資料庫提示sqlstate[hy000] [2002] Connection refused資料庫SQL
- go get報錯connect: connection refusedGo
- Error:Can't connect to SOCKS proxy:Connection refused (Connection refused)Error
- redis 原始碼分析:Jedis 哨兵模式連線原理Redis原始碼模式
- nameko shell 報錯 : [Errno 111] Connection refused
- 解決使用jedis連線是報DENIED Redis is running in protected mode錯誤Redis
- (20200916 Solved)Docker||redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refusedDockerRedis127.0.0.1
- docker phpmyadmin 連線宿主mysql資料庫報錯:mysqli::real_connect(): (HY000/2002): Connection refusedDockerPHPMySql資料庫
- 連線華為雲的Redis服務報錯“Error: Connection reset by peer”RedisError
- SSH出現Connection refused錯誤
- 【Azure Cache for Redis】Python Djange-Redis連線Azure Redis服務遇上(104, 'Connection reset by peer')RedisPython
- Web - Redis & JedisWebRedis
- jedis操作 redisRedis
- Redis(15) jedisRedis
- ssh: connect to host localhost port 22: Connection refusedlocalhost
- raw.githubusercontent.com port 443: Connection refusedGithub
- Error!: SQLSTATE[HY000] [2002] Connection refusedErrorSQL
- Linux啟動tomcat後執行shutdown.sh關閉時出現異常:Connection refused (Connection refused)LinuxTomcat
- idea報錯資料庫connection refused【杭州多測師_樹哥】Idea資料庫
- telnet localhost 44444 telnet: connect to address ::1: Connection refusedlocalhost
- kubernetes應用flannel失敗 Connection refused.
- 【Azure Redis 快取】 Python連線Azure Redis, 使用redis.ConnectionPool 出現 "ConnectionResetError: [Errno 104] Connection reset by peer"Redis快取PythonError
- Java 客戶端 Jedis和JedisPool 連線池Java客戶端
- OGG-01224 error 111:Connection refused - retries exceeded.Error
- 解決 Android studio Connect to 127.0.0.1:[/127.0.0.1] failed: Connection refusedAndroid127.0.0.1AI
- Listener refused the connection with the following error:ORA-12514, TNS:listenerError
- Java專案連線資料庫Mysql報錯create connection SQLExceptionJava資料庫MySqlException
- Solaris SSH 服務處於maintenance模式,ssh connection refusedAINaN模式
- 坑:ssh: connect to host github.com port 22: Connection refusedGithub
- The connection to the server 10.10.0.2:6443 was refused - did you specify the right host or port?Server
- Jedis連線池究竟是何物|得物技術
- 聊聊jedis連線池對commons-pool的封裝封裝
- 異常解決——GitLab : ssh: connect to host port 22: Connection refusedGitlab