java app无法连接到远程MySQL数据库,但可以连接到本地MySQL数据库

时间:2015-10-09 23:33:50

标签: java mysql hibernate

给定两个MySQL服务器,一个本地,一个远程。 两者都有一个包含表bohica的数据库foobar。 本地服务器已定义用户'myadmin'@'%','myadmin'@'localhost'。 远程服务器已定义用户'myadmin'@'%','myadmin'@'localhost'和'myadmin'@'my.domain.com'。

已授予所有这些用户的权限和刷新的权限。

两台服务器都已启动。

从命令提示符窗口我可以连接到两个服务器,即

mysql --user=myadmin --password=mylocalpw
mysql --user=myadmin --password=myremotepw --host=my.domain.com

两者都成功,这证明我可以访问并登录到远程服务器。

我的java / maven / hibernate应用程序有一个上下文文件

...
<bean id="databasePropertiesServerB"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--
  <property name="location"          value="classpath:databaseServerBlocal.properties" />
-->
  <property name="location"          value="classpath:databaseServerBliveadmin.properties" />
  <property name="placeholderPrefix" value="$dbServerB{" />
  <property name="placeholderSuffix" value="}" />
</bean>

<bean id="dataSourceServerB"  class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  <property name="uniqueResourceName"    value="XADBMS_B" />
  <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
  <property name="xaProperties">
      <props>
          <prop key="databaseName">foobar</prop>
          <prop key="user">$dbServerB{hibernate.connection.username}</prop>
          <prop key="password">$dbServerB{hibernate.connection.password}</prop>
      </props>
  </property>
  <property name="poolSize"><value>20</value></property>
  <property name="testQuery" value="SELECT 1" />
</bean> 

<bean id="emfB" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="packagesToScan"         value="com.mybiz.forms" />
  <property name="dataSource"             ref="dataSourceServerB" />
  <property name="jpaDialect"             ref="jpaHibernateDialect" />
  <property name="jpaVendorAdapter"       ref="jpaHibernateVendorAdapter" />
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.dialect">$dbServerB{hibernate.dialect}</prop>
      <prop key="hibernate.connection.characterEncoding">$dbServerB{hibernate.connection.characterEncoding}</prop>
      <prop key="hibernate.connection.driver_class">$dbServerB{hibernate.connection.driver_class}</prop>
      <prop key="hibernate.connection.url">$dbServerB{hibernate.connection.url}</prop>
      <prop key="hibernate.connection.release_mode">$dbServerB{hibernate.connection.release_mode}</prop>
      <prop key="hibernate.cache.provider_class">$dbServerB{hibernate.cache.provider_class}</prop>
      <prop key="hibernate.c3p0.min_size">$dbServerB{hibernate.c3p0.min_size}</prop>
      <prop key="hibernate.c3p0.max_size">$dbServerB{hibernate.c3p0.max_size}</prop>
      <prop key="hibernate.c3p0.timeout">$dbServerB{hibernate.c3p0.timeout}</prop>
      <prop key="hibernate.c3p0.max_statements">$dbServerB{hibernate.c3p0.max_statements}</prop>
      <prop key="hibernate.show_sql">$dbServerB{hibernate.show_sql}</prop>
      <prop key="hibernate.format_sql">$dbServerB{hibernate.format_sql}</prop>
      <prop key="hibernate.hbm2ddl.auto">$dbServerB{hibernate.hbm2ddl.auto}</prop>
      <prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory</prop>
      <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
    </props>
  </property>
</bean> 
...

和专业文件 databaseServerBliveadmin.properties

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://my.domain.com:3306/foobar
hibernate.connection.username=myadmin
hibernate.connection.password=myremotepw
hibernate.connection.release_mode=after_transaction
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.c3p0.init_size=10
hibernate.c3p0.min_size=10
hibernate.c3p0.max_size=50
hibernate.c3p0.timeout=600
hibernate.c3p0.max_statements=50
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create

和databaseServerBlocaladmin.properties

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/foobar
hibernate.connection.username=myadmin
hibernate.connection.password=mylocalpw
hibernate.connection.release_mode=after_transaction
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.c3p0.init_size=10
hibernate.c3p0.min_size=10
hibernate.c3p0.max_size=50
hibernate.c3p0.timeout=600
hibernate.c3p0.max_statements=50
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update

现在变得奇怪了。 当我调整“databasePropertiesServerB”bean中的“location”属性值以使用databaseServerBlocal.properties时, 该应用程序可以连接到本地服务器并按预期执行操作。

但是(你知道还有......但是......)

当我调整“databasePropertiesServerB”bean中的“location”属性值以使用databaseServerBliveadmin.properties时,我得到了可怕的

java.sql.SQLException: Access denied for user 'myadmin'@'localhost' (using password: YES)

ERRMSG。我可以手动登录到远程服务器,证明用户名和密码是正确的。 我一直非常小心地在两个.properties文件中正确拼写用户名和密码值 - 没有尾随空格等等。所以我很难过。有什么想法吗?

TIA,

Still-learning Stev

1 个答案:

答案 0 :(得分:0)

在随后的问题中出现完整答案

cannot achieve connectivity with MySQL db on remote machine

基本上,关于hibernate属性文件以及MySQL实例上的帐户和密码的所有内容都是正确的 - 原因是没有在实体管理器工厂bean使用的Atomikos数据源bean中显式设置xaProperties.server属性 - 如果没有明确设置,则默认设置为“localhost”,这当然不适用于远程计算机,因为它尝试以xxxxx身份登录。%

鬼鬼祟祟鬼鬼祟祟。

CASE CLOSED

仍在学习史蒂夫