java连接Linux服务器问题

问题:

  使用java连接Linux服务器上的redis失败

  代码:

    

package com.redis.study.test;

import org.junit.Test;
import redis.clients.jedis.Jedis;

public class RedisTest {

    @Test
    public void test() {
        Jedis jedis = new Jedis("192.168.80.128", 6379);
        System.out.println(jedis.get("username"));
    }
}

  错误日志:

  

redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.80.128:6379

    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:117)
    at redis.clients.jedis.Jedis.get(Jedis.java:152)
    at com.redis.study.test.RedisTest.test(RedisTest.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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)
    ... 27 more

思考:

  1、ip地址错误

   (1)、 进入服务器查看Linux服务器地址

      

  唉,好像有两个ip地址 试试另一个

  

  吐血。还是不行。

  (2)、会不会是本机没法连接服务器,ping一下试试

    

    能ping的通啊

  (3)、试试telnet

    

    看样子是端口没开放

    进入服务器尝试开放该端口,但是发现我连Iptables都没有 只要一个iptabkeconfig

    

    所以应该不是防火墙的问题

  (3)、查到说是redis的还需要开放允许远程连接

    进入redis的安装目录修改redis.conf

    找到bind 将127.0,0.1注释

    保存退出

    

    重启redis试试

    

    连接的问题到此解决但是又出现了新问题

    

    日志:

    

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:130)
    at redis.clients.jedis.Protocol.process(Protocol.java:164)
    at redis.clients.jedis.Protocol.read(Protocol.java:218)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:341)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:260)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:249)
    at redis.clients.jedis.Jedis.get(Jedis.java:153)
    at com.redis.study.test.RedisTest.test(RedisTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code -1

  大概意思是说开启了protected mode 也就是保护模式??

  还是找到配置文件,打开,查看 发现真的有这么一个mode把它改成no试试

  

  重启redis再试试

  

  终于出结果了


 总结:

  1、判断是否是服务器地址不对

  2、判断端口是否能访问

  3、关闭redis的只允许本机访问的限制

  4、关闭保护模式

原文地址:https://www.cnblogs.com/zqzhen/p/12684793.html

时间: 2024-08-01 11:21:10

java连接Linux服务器问题的相关文章

Java连接Linux服务器

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 --><dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>build210</version></dependency> 一.

widows本地-xshell实现远程连接linux服务器图形界面

本地环境远程连接linux图形界面,常用的实现工具有,VNC.Puty.Xshell等,这里我们用的xshell manager: Xmanager简介:Xmanager是一个运行于 Windows平台上的高性能的X Server软件.它能把远端Unix/Linux的桌面无缝地带到你的Windows上,甚至电脑是在内网或防火墙后,也能通过SSH协议安全的运行远端的X应用程序. 特点包括: 可通过Xcongfig工具设置多个Xmanager设置: 支持多用户的Windows终端环境: 支持多个IP

远程连接Linux服务器

远程连接Linux服务器一般使用ssh方式. 1.Linux系统下连接远程Linux服务器可以直接使用ssh命令. 格式如下: ssh [email protected]192.168.1.1 root对应你使用的用户名,192……对应的服务器ip地址,一般服务器端口22,命令默认22,如果需要更改端口在ssh后面 -p 端口. 2.Windows系统下连接Linux服务器一般需借助PuTTY, Xshell, SSH Secure Shell Slient,SecureCRT.

java连接远程服务器

我用的是smb协议 共享方式连接远程 Windows服务器  也可以用ftp 但要保证服务器是ftp的 连接Linux服务器可以用ssh 协议 新建一个res.properites连接 IP=10.61.28.56 SMB_MINGCHENG=smb://jichuzhongxin:[email protected] //服务器登录名 密码 IP WENJIAN_NAME=/res.properties //代码 public class UploadFile{ // 读取服务器文件夹到本地 p

连接Linux服务器:Win免费SSH客户端工具

连接Linux服务器:Win免费SSH客户端工具 http://blog.csdn.net/jiangdou88/article/details/51585555

如何在Windows系统上利用Telnet协议连接Linux服务器

Telnet协议是Internet远程登录服务的标准协议,它为用户提供了在本地计算机上完成远程主机工作的能力.很多终端使用者都习惯在计算机上利用Telnet会话来远程控制服务器.这里小编就分两步为大家演示如何在Windows系统上利用Telnet协议连接Linux服务器. 步骤一:在Windows系统上打开Telnet功能:  步骤二:在Linux系统上启用Telnet服务. 步骤一:Windows终端开启Telnet功能 1/由于在Windows Vista之后的版本默认并没有提供Telnet

Windows10连接linux服务器

平时我们通过windows连接服务器时,大多数用的是ssh客户端软件,推荐使用ssh,安全系数比较高.下面介绍用telnet连接服务器. 客户端:C:\Users\dell>systeminfo主机名:           BOHAOOS 名称:          Microsoft Windows 10 家庭中文版OS 版本:          10.0.10240 OS 制造商:        Microsoft CorporationOS 配置:          独立工作站OS 构件类型

连接Linux服务器操作Oracle数据库

连接Linux服务器操作Oracle数据库 由于项目已经上线,现场的数据库服务器不允许直接用Oracle的客户端plsqldev.exe来连接,只能通过Linux服务器的命令来操作. 以下是用SecureCRT 5.1客户端连接Linux服务器来操作Oracle数据库: 1.连接到Linux服务器(输入服务器的ip地址) ssh 10.199.94.227 连接成功后会提示输入密码,输入成功会提示. 2.切换到操作Oracle数据库 su - oracle 切换成功会提示相关数据库的实例.例如:

xshell连接linux服务器切换至oracle的sqlplus控制台时,无法使用回车键的解决方案!

当使用xshell连接linux服务器后,切换至sqlplus控制台,当使用回车键时,出现^H的符号,貌似回车键不能用!解决方案: 1)$ stty erase ^H : 与退格键相关的设置是erase,它表示删除最后一个字符. 如果在当前窗口执行的话,只对当前的窗口有效,下次登陆的时候还需要重新设置,可以把这个命令写入shell 的配置文件,如~/.bashrc 中,这样每次都能生效了. 2)使用delete键代替退格键, 3)使用ctrl+退格键 4)执行命令 stty erase ^H 修