相关疑难解决方法(0)

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

我正在努力让我的数据库与我的Java程序对话.

有人可以使用JDBC给我一个快速而又脏的示例程序吗?

我收到了一个相当惊人的错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at SqlTest.main(SqlTest.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    The …
Run Code Online (Sandbox Code Playgroud)

java mysql jdbc

200
推荐指数
11
解决办法
53万
查看次数

odd SQLException - 无法检索转换只读状态服务器

我有一个Quartz Job,它每5分钟在我的MySQL数据库中执行一次存储过程,由于某种原因,3次执行中有1次失败并给出了这个奇怪的异常.我搜索并搜索了这个异常的含义,但我找不到解决方案.这是完整的堆栈跟踪:

java.sql.SQLException: Could not retrieve transation read-only status server
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:951)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3939)
    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3910)
    at com.mysql.jdbc.PreparedStatement.checkReadOnlySafeStatement(PreparedStatement.java:1258)
    at com.mysql.jdbc.CallableStatement.checkReadOnlySafeStatement(CallableStatement.java:2656)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1278)
    at com.mysql.jdbc.CallableStatement.execute(CallableStatement.java:920)
    at com.mchange.v2.c3p0.impl.NewProxyCallableStatement.execute(NewProxyCallableStatement.java:3044)
    at org.deadmandungeons.website.tasks.RankUpdateTask.execute(RankUpdateTask.java:30)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 1,198,219 milliseconds ago.  The last packet sent successfully to the server was 950,420 milliseconds ago.
    at sun.reflect.GeneratedConstructorAccessor43.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) …
Run Code Online (Sandbox Code Playgroud)

java mysql sql jdbc

17
推荐指数
1
解决办法
1万
查看次数

Java + Tomcat,Dying数据库连接?

我有一个tomcat实例设置,但我配置的数据库连接context.xml在一段时间不活动后仍然会死.

当我检查日志时,我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是68051秒前.成功发送到服务器的最后一个数据包是68051秒前,这比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.

这是context.xml中的配置:

<Resource name="dataSourceName" 
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000" 
        username="username" 
        password="********"
        removeAbandoned = "true"
        logAbandoned = "true"
        driverClassName="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://127.0.0.1:3306/databasename?autoReconnect=true&amp;useEncoding=true&amp;characterEncoding=UTF-8"  />
Run Code Online (Sandbox Code Playgroud)

我正在使用autoReconnect=true错误说要做,但连接一直在死.我以前从未见过这种情况.

我还验证了所有数据库连接都已正确关闭.

java mysql database tomcat

14
推荐指数
1
解决办法
3万
查看次数

SQL错误:0,SQLState:08S01通信链接失败

我收到此错误(不是那么频繁):

2013-05-08 16:44:35,786  WARN (JDBCExceptionReporter.java:100) [org.hibernate.util.JDBCExceptionReporter, logExceptions] - SQL Error: 0, SQLState: 08S01
2013-05-08 16:44:35,786 ERROR (JDBCExceptionReporter.java:101) [org.hibernate.util.JDBCExceptionReporter, logExceptions] - Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2013-05-08 16:44:35,833 ERROR (AdminDaoImpl.java:297) [com.myapp.admin.db.AdminDaoImpl, createFilePackage] - data-upload: Exception while adding new file package
org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate jdbc

14
推荐指数
2
解决办法
11万
查看次数

如何修复“[08S01]通信链路故障最后一个数据包成功发送到服务器是在 0 毫秒前。”

我正在使用 PhpStorm + DataGrip + MySQL + WAMP 开发一个 PHP 项目,从那时起,我就可以使用这些配置正常使用远程数据库(参见图片):

在此输入图像描述

在此输入图像描述

但突然我收到这个错误:

[08S01] 通信链路故障 最后一次成功发送到服务器的数据包是在 0 毫秒前。驱动程序尚未收到来自服务器的任何数据包。java.net.ConnectException:连接被拒绝:连接

在此输入图像描述

有人知道我可以做什么来解决这个错误吗?

我已经尝试做我在这里找到的事情(用 JDBC 和 MySQL 解决“通信链接故障”),但还没有成功。

我希望能够再次正常连接到我的数据库。

mysql wamp phpstorm intellij-datagrip

11
推荐指数
1
解决办法
1万
查看次数

Spring Boot:使用Hibernate,JDBC和MySQL几小时不活动后,通信链路出现故障

如果我的Spring Boot应用程序在几个小时内处于非活动状态(例如在夜间),我会收到此错误:

2015-05-19 09:16:32.666  WARN 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
2015-05-19 09:16:32.668 ERROR 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure

The last packet successfully received from the server was 29.792.613 milliseconds ago.  The last packet sent successfully to the server was 6 milliseconds ago.
Run Code Online (Sandbox Code Playgroud)

试图解决这个问题我读到MySQL有一个名为wait_timeoutsetted 的参数,默认为8小时(28800秒),在此之后我的所有非活动连接都关闭了,所以我的Spring Boot应用程序停止工作......

我的问题是:

  • 我该如何避免这个问题?
  • 我应该增加这个参数的值吗?
  • 是否存在增加这种价值的任何缺点?(它总是可以是我的应用程序达到的值,如果它长时间处于非活动状态..:/)
  • 是不是有另一种使用Spring Boot的解决方案(如轮询或类似的东西)?

编辑

这里还有其他类似/有用的问题:

mysql spring hibernate jdbc spring-boot

10
推荐指数
1
解决办法
1万
查看次数

MySQLNonTransientConnectionException:无法创建与数据库服务器的连接.尝试重新连接3次.放弃

我无法连接到我的数据库.我认为有一个我不知道的地方,需要改变.我在这里有一些非常基本的代码:

public static void main(String[] args)
{
    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
        con.close();
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

显然有登录信息.它会导致此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Caused by: java.net.ConnectException: Connection refused
Run Code Online (Sandbox Code Playgroud)

我已经浏览了大约10个关于同一问题的不同帖子,并尝试了他们的解决方案,仍然无法让它工作.

java mysql database-connection

7
推荐指数
1
解决办法
6万
查看次数

无法将MySQL容器连接到docker中的Tomcat容器

计划

我希望我的tomcat服务器能够在单独的容器中连接到我的MySQL服务器.

问题

Tomcat无法连接到MySQL

我使用了wordpress教程中有关设置mysql容器链接的一些细节,并创建了MySQL的链接.

虽然tomcat和mysql旋转得很好我似乎无法让tomcat能够连接到MySQL,我的本地机器上的设置工作完全没问题.

我也尝试过使用--net: "host"虽然它不能用于Tomcat,因为它会引发严重的错误.

以前的答案

在这篇文章中注意到了错误的可能修复程序,尽管我不相信其中任何一个会转换为我的问题,因为我认为这是一个docker问题而不是主机问题.

泊坞窗,compose.yml

web:
  image: tomcat:7.0
  container_name: tomcat-container
  ports:
   - "80:8080"
  hostname: docker-tomcat
  volumes:
   - /home/webapps:/usr/local/tomcat/webapps
  links:
   - db
db:
  image: mysql
  container_name: mysql-container
  environment:
   MYSQL_ROOT_PASSWORD: Mysqlpassword1
   MYSQL_DATABASE: tracker
  volumes:
   - /home/mysqlDB:/var/lib/mysql
Run Code Online (Sandbox Code Playgroud)

这是我的 Context.xml from tomcat.

<Context>
    <Resource
        name="jdbc/tracker" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" 
        url="jdbc:mysql://localhost:3306/tracker?useSSL=false"
        driverClassName="com.mysql.jdbc.Driver"
        username="root" password="mysqladmin1"
    />
    <Resource
        name="jdbc/jenkins" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" 
        url="jdbc:mysql://localhost:3306/jenkins?useSSL=false"
        driverClassName="com.mysql.jdbc.Driver"
        username="root" password="mysqladmin1"
    />
</Context>
Run Code Online (Sandbox Code Playgroud)

错误代码.

The last packet sent successfully …
Run Code Online (Sandbox Code Playgroud)

mysql docker docker-compose

6
推荐指数
1
解决办法
6946
查看次数

无法从java连接到mysql服务器

"Communications link failure"在这行代码中得到错误:

mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)

我查了这篇文章中的所有内容:

  • 我在etc/mysql中增加了my.cnf中的max-allowed-packet:max_allowed_pa​​cket = 5073741824 ------ [mysqldump] max_allowed_pa​​cket = 1G
  • 绑定地址是:127.0.0.1
  • 所有超时值都等于一个数字
  • Tomcat尚未安装在服务器上(新服务器)
  • my.cnf中没有跳过网络
  • 我可以ping服务器
  • 我通过ssh连接到mysql数据库

当我将查询字符串更改为:

mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:22/127.0.0.1:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)

我收到了错误 Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

虽然我已经改变了my.cnf上的数据包大小,然后重新启动了mysql服务.

有什么建议?

注意:

我可以通过ssh与这段代码连接,但这种方式似乎不合理!我可以在main中连接一次然后我应该将连接传递给所有类.

public my-class-constructor() {

        try {
            go();
        } catch (Exception e) {
            e.printStackTrace();
        }

        mySqlCon = null; …
Run Code Online (Sandbox Code Playgroud)

java mysql database-connection

5
推荐指数
1
解决办法
885
查看次数

为什么我在使用Java(Netbeans)连接到MySQL时出现此错误'通信链接失败最后发送的数据包...'?

我正在尝试使用MySQL数据库(安装在我的电脑上的虚拟机(Debian)上并具有IP地址192.168.1.5)来使用NetBeans.

我已按如下方式配置连接:

Driver Name MySQL(Connector/J Driver)

Host 192.168.1.5

Database test

Username root

Password *

JDBC URL jdbc:mysql://192.168.1.5:3306/test
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:

cannot establish a connection to jdbc:mysql://192.168.1.5:3306/test using 
  com.mysql.jdbc.Driver (Communications link failure  The last packet sent successfully 
  to the server was 0 milliseconds ago. The driver has not received any packets 
  from the server.)
Run Code Online (Sandbox Code Playgroud)

我的mysql.user表看起来像这样(我知道% root这不是很安全,但这只是为了简化目前的事情):

+------------+------------------+
| host       | user             |
+------------+------------------+
| %          | root             |
| 127.0.0.1  | root             |
| ::1        | root …
Run Code Online (Sandbox Code Playgroud)

java mysql netbeans netbeans-7

3
推荐指数
1
解决办法
2万
查看次数

线程“main”中的异常java.net.ConnectException:连接被拒绝:连接套接字编程Java

我最近学习了客户端和服务器之间的套接字编程。所以我想到做一个连接客户端和服务器的练习。但是,当我尝试运行代码时遇到此错误消息: 线程“main”java.net.ConnectException 中的异常:连接被拒绝:连接

这是我的客户端类代码:

public class clientpart {
    public static void main(String[]args) throws UnknownHostException, IOException {
        Scanner input = new Scanner(System.in);
        int port = 8080;
        String host=null;
        String answer; String sendMessage; String receivedMessage;
        InetAddress address = InetAddress.getByName(host);
        Socket socket= new Socket(address,port);

        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);

        System.out.println("Please answered the following question: ");
        System.out.println("What is the subject code for Socket Programming?");
        answer = input.nextLine();

        sendMessage = answer;
        bw.write(sendMessage);
        bw.newLine();
        bw.flush();
        System.out.println("Message sent …
Run Code Online (Sandbox Code Playgroud)

java sockets client server

3
推荐指数
1
解决办法
5万
查看次数

如何使用MS Access等目录路径连接MySQL数据库文件(.sql)?

对不起也许这是我第二次问这个问题是因为没有得到任何答案.

这是我的代码

    try{
    File  f = new File("Database.sql");
    if(f.exists()){
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/"+f.getName(),"","");
    }else{
        f.createNewFile();
        System.out.println("file created");
        //also do the connection
    }
    }catch(Exception ex){
       System.out.println(ex.getMessage());
    }
Run Code Online (Sandbox Code Playgroud)

这是错误:

通信链路故障成功发送到服务器的最后一个数据包是0毫秒前.驱动程序未收到来自服务器的任何数据包.

这个问题的目的是:我正在创建一个适用于许多用户的应用程序,问题是他们对计算机一无所知,我必须尽可能简单.

那么有没有办法通过目录路径连接MYSQL,如MS ACCESS?

或者还有其他建议吗?

谢谢 .

java mysql connection

0
推荐指数
1
解决办法
6888
查看次数

Docker、Springboot 和 Mysql:com.mysql.cj.exceptions.CJCommunicationsException:通信链路故障

我尝试在 Docker 上运行带有 mysql 连接的 Springboot 应用程序。没有docker,它工作得很好。但是当我尝试在容器上部署时,我有com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure。\n我使用这个 docker-compose:

\n\n
version: \'3\'\nservices:\n  nginx:\n   container_name: nginx\n   image: nginx:latest\n   restart: always\n   ports:\n   - 80:80\n   - 443:443\n   volumes:\n   - ./nginx/conf.d:/etc/nginx/conf.d\n   depends_on:\n   - spring-boot-app\n\n\n  mysql:\n   container_name: mysql\n   image: mysql:latest\n   environment:\n    MYSQL_ROOT_PASSWORD: "rootpass"\n    MYSQL_DATABASE: "db"\n    MYSQL_USER: "user"\n    MYSQL_PASSWORD: "pass"\n   ports:\n   - "1306:3306"\n   volumes:\n   - ./data/:/var/lib/mysql\n   networks: \n   - mysql-db\n   restart: always\n\n  spring-boot-app:\n    container_name: spring-boot-app\n    image: spring-boot-app\n    build:\n      context: ./spring-boot-app\n      dockerfile: Dockerfile\n    ports:\n      - "8080:8080"\n    depends_on:\n      - mysql\n    networks: \n      - mysql-db\n …
Run Code Online (Sandbox Code Playgroud)

mysql spring jdbc docker spring-boot

0
推荐指数
1
解决办法
4187
查看次数