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
- ssh連線Linux收到The remote system refused the connection報錯LinuxREM
- Redis筆記2:Jedis連線池Redis筆記
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolRedisclientException
- Jedis使用連線池操作redis叢集Redis
- redis 原始碼分析:Jedis 哨兵模式連線原理Redis原始碼模式
- Redis學習記錄(二)--使用Jedis連線Redis
- 解決使用jedis連線是報DENIED Redis is running in protected mode錯誤Redis
- Redis筆記3:Jedis連線自動釋放Redis筆記
- go get報錯connect: connection refusedGo
- mysql connection refusedMySql
- 連線華為雲的Redis服務報錯“Error: Connection reset by peer”RedisError
- Error:Can't connect to SOCKS proxy:Connection refused (Connection refused)Error
- 連線資料庫提示sqlstate[hy000] [2002] Connection refused資料庫SQL
- Oracle11G監聽報錯Connection refusedOracle
- jdbc連線sql server 出現 java.sql.SQLException: Network error IOException: Connection refused:JDBCSQLServerJavaExceptionError
- Web - Redis & JedisWebRedis
- Redis(15) jedisRedis
- jedis操作 redisRedis
- Connection (建立連線)
- IOException in sending Request :: Connection refusedException
- docker phpmyadmin 連線宿主mysql資料庫報錯:mysqli::real_connect(): (HY000/2002): Connection refusedDockerPHPMySql資料庫
- OGG 同步報錯 - TCP/IP error 111 (Connection refused)TCPError
- Xlib: connection to ":0.0" refused by server(openSUSE)Server
- Java 客戶端 Jedis和JedisPool 連線池Java客戶端
- 【Azure Cache for Redis】Python Djange-Redis連線Azure Redis服務遇上(104, 'Connection reset by peer')RedisPython
- FTP Connection refused error 解決辦法FTPError
- Xlib: connection to ":0.0" refused by server 問題Server
- redis(二)redis概述與jedis的使用Redis
- java操作redis。jedis使用apiJavaRedisAPI
- ssh: connect to host localhost port 22: Connection refusedlocalhost
- java.sql.SQLException: Io 異常: Connection refusedJavaSQLException
- Python連線Redis連線配置PythonRedis
- Java專案連線資料庫Mysql報錯create connection SQLExceptionJava資料庫MySqlException
- Jedis連線池究竟是何物|得物技術
- Jedis 與 MySQL的連線執行緒安全問題MySql執行緒
- Linux啟動tomcat後執行shutdown.sh關閉時出現異常:Connection refused (Connection refused)LinuxTomcat