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

时间:2012-04-15 05:43:27

标签: java mysql spring hibernate

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

<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 个答案:

答案 0 :(得分:1)

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

   mysqld --verbose --help

停止服务

  sudo service mysql stop

应该更改的选项是[mysqld]部分中的port,正如你之前在/etc/mysql/my.cnf上所说的那样,但要确保文件&#34;〜/ .my.cnf&# 34; (如果存在)不会覆盖端口。无论如何,想法是设置端口,如下所示:

   port            = 3307

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

   sudo service mysql start

使用mysqld --verbose --help检查端口,看看是否所有内容都符合您的预期。

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

我希望这会有所帮助。

答案 1 :(得分:1)

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