Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate gets refused connection for non default mysql port

I am connecting to a MySQL(5.1.6) database from Linux on default 3306 port and everything is fine:

<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="" />

Now, I go in /etc/mysql/my.cnf and change default port to 3307 (both [client] and [mysqld]) restart MySQL, adapt hibernate connection 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="" />

and restart jboss. this time I am no longer able to connect:

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

Any idea what could be the reason mysql refuses connection? No other configuration were changed..but MySQL default port. There are no firewall up and running either.

Thanks for any suggestion.

like image 830
ionut.diaconu Avatar asked Nov 05 '22 01:11

ionut.diaconu


2 Answers

first you can check what options are being used in the server you can run:

   mysqld --verbose --help

Stops the service doing

  sudo service mysql stop

The option that should be changed is port in [mysqld] section, as you said before this is on the /etc/mysql/my.cnf but be sure that the file "~/.my.cnf" (if exists) is not overriding the port. Anyway the idea is to set port as depicted below:

   port            = 3307

And finally start the MySQL service again, and of course JBoss.

   sudo service mysql start

Check the port with mysqld --verbose --help, and see if all is as you expected.

In the other hand, you can test the connection using some client application and the same connector jar. I.e: SQuirrel, and coping the library in the corresponding lib directory. This really helps me when I had issues like that.

I hope this helps.

like image 116
sgroh Avatar answered Nov 07 '22 21:11

sgroh


I also faced same problem like this.Better to change the jdbc mysql driver of another version.because when i am using mysql 5.1.9 driver it raises connection refused (The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.) problem then I switched another version like 5.1.18 it works.

like image 23
Srinivas Avatar answered Nov 07 '22 22:11

Srinivas