当前位置: 首页 > 知识库问答 >
问题:

hibernate被拒绝连接非默认mysql端口

柴飞星
2023-03-14

我连接到MySQL(5.1.6)数据库从Linux默认的3306端口和一切都很好:

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/wpdb" />
<property name="username" value="root" />
<property name="password" value="" />

现在,我进入/etc/mysql/my。cnf并将默认端口更改为3307(包括[client]和[mysqld])重新启动MySQL,调整hibernate连接url:

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/wpdb" />
<property name="username" value="root" />
<property name="password" value="" />

然后重启jboss。这一次我再也无法连接:

08:22:05,276 WARN  [org.hibernate.util.JDBCExceptionReporter] (MSC service thread 1-13) SQL Error: 0, SQLState: 08S01
08:22:05,276 ERROR [org.hibernate.util.JDBCExceptionReporter] (MSC service thread 1-13) 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.
08:22:05,277 WARN  [org.hibernate.cfg.SettingsFactory] (MSC service thread 1-13) Could not obtain connection to query metadata: 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) [rt.jar:1.6.0_26]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_26]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_26]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_26]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2209) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) [mysql-connector-java-5.1.9.jar:]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_26]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_26]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_26]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_26]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284) [mysql-connector-java-5.1.9.jar:]
    at java.sql.DriverManager.getConnection(DriverManager.java:582) [rt.jar:1.6.0_26]
    at java.sql.DriverManager.getConnection(DriverManager.java:154) [rt.jar:1.6.0_26]
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173) [spring-jdbc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164) [spring-jdbc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149) [spring-jdbc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119) [spring-jdbc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81) [spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:114) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2159) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1383) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) [hibernate-annotations-3.5.1-Final.jar:3.5.1-Final]
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860) [spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779) [spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) [spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:400) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:275) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139) [spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79) [spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70) [spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:103) [spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1475) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1443) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:728) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449) [spring-context-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_26]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_26]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_26]
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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_26]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_26]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_26]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_26]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343) [mysql-connector-java-5.1.9.jar:]
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2132) [mysql-connector-java-5.1.9.jar:]
    ... 59 more
Caused by: java.net.ConnectException: Connection refused

知道mysql拒绝连接的原因吗?没有更改其他配置。。但MySQL是默认端口。也没有防火墙启动和运行。

谢谢你的建议。

共有2个答案

邢冷勋
2023-03-14

我也面临着同样的问题。最好更改另一个版本的jdbc mysql驱动程序。因为当我使用mysql 5.1.9驱动程序时,它会引发连接被拒绝(最后一个成功发送到服务器的数据包是0毫秒前的。驱动程序没有从服务器收到任何数据包。)问题然后我换了另一个版本,比如5.1.18,它可以工作。

姚丰羽
2023-03-14

首先,您可以检查您可以运行的服务器中正在使用哪些选项:

   mysqld --verbose --help

停止服务

  sudo service mysql stop

应该更改的选项是[mysqld]部分中的port,正如您之前所说的,这是在 /etc/mysql/my.cnf上,但请确保文件"~/.my.cnf"(如果存在)没有覆盖端口。无论如何,这个想法是设置端口如下所示:

   port            = 3307

最后再次启动MySQL服务,当然还有JBoss。

   sudo service mysql start

使用mysqld--verbose--help检查端口,看看是否一切都如您所期望的那样。

另一方面,您可以使用一些客户端应用程序和相同的连接器jar测试连接。即:SQuirrel,并处理相应lib目录中的库。当我有这样的问题时,这真的帮助了我。

我希望这能有所帮助。

 类似资料:
  • 我试图连接到mysql数据库使用hibernate从cfg文件作为 虽然我的连接字符串、用户名、密码是正确的,并且在其他具有基本jdbc连接的项目中运行良好,但当我使用hibernate尝试它时,我在控制台上遇到了这个错误 是的,数据库服务器已启动并运行,但来自此Hibernate应用程序的连接被拒绝

  • 问题内容: 我创建了一个小工具,过去它像一个魅力一样可以部署小型WordPress实例。看起来像这样: 但是当我现在启动它时(也许是因为docker更新到Docker 1.9.1版本,build ),它失败了 当我的猫的有项针对MySQL: 而且我能够ping通MariaDB服务器。 当我安装WordPress并多次重启后,MariaDB容器打印出来: 是哪个指示它正在运行,不是吗? 我如何获得W

  • 所以今天我决定如果我把数据库转移到另一个服务器上,我的网站会运行得更快,所以我做了,你可以看到我是如何连接下面的: 当我连接到/时,这种方法很好,现在我得到了这个错误: 修复1:修复了连接被拒绝的错误-步骤1:编辑文件步骤2:删除行

  • 问题内容: 我是 HBase 和 Hadoop的 新手。我已经完全设置了HBase并完美启动。现在,当我尝试使用Java客户端从 p1 连接到HBase(HBase安装在 p2上 )时,它抛出了一个奇怪的异常。 问题答案: 我找到了解决方案。 通过仅从我的 主机中 删除 localhost 条目。现在我的本地主机条目就像 192.169.19.50 [这是我的hbase IP] =本地主机,而不是

  • 问题内容: 我正在尝试实现TCP连接,从服务器端一切正常,但是当我运行客户端程序(从客户端计算机)时,出现以下错误: 我尝试更改套接字号以防万一,但无济于事,有谁知道导致此错误的原因和解决方法。 服务器代码: 客户代码: 问题答案: 此异常意味着你尝试连接的IP /端口上没有侦听服务: 你试图连接到错误的IP /主机或端口。 你尚未启动服务器。 你的服务器没有监听连接。 在Windows服务器上,

  • 我无法连接到RabbitMQ。RabbitMQ不在本地计算机上。 我的应用程序.属性看起来像 我可以使用https://urltologinscreen:15671访问Rabbitmq gui 我得到以下错误 如何解决问题?