【发布时间】:2015-10-09 23:33:49
【问题描述】:
给定两台 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)
错误消息。我可以手动登录到远程服务器,证明用户名和密码正确。 我一直非常小心地在两个 .properties 文件中正确拼写用户名和密码值 - 没有尾随空格等。所以我在这一点上被难住了。有什么想法吗?
TIA,
还在学习的史蒂夫
【问题讨论】:
-
在创建数据库用户帐户时,我会避免使用 %(通配符)。这允许任何服务器在知道用户名/密码的情况下进行连接。此外,我喜欢为每个应用程序使用唯一的用户名来增强安全性。最后,永远不要将数据库主机放在您的公共 DNS 中。如果您使用的是 linux,则应将其保存在内部和 /etc/hosts 文件中。
-
错误消息似乎正在使用其他属性文件。我会创建一个不同的用户帐户和密码并尝试重新连接。
-
不,我可以通过暂时将用户名更改为 helloThere 来确认选择了 databaseServerBliveadmin.properties,并且确实显示在错误消息中。
-
您正在使用 hibernate.hbm2ddl.auto=create 用于 databaseServerBliveadmin.properties 文件和 hibernate.hbm2ddl.auto=update 用于 databaseServerBlocaladmin.properties 文件。我认为这是问题所在。请检查。
-
好的。告诉我你是在远程服务器的情况下从头开始创建数据库吗?并在你的属性文件中尝试这个 jdbc:mysql://my.domain.com:3306/foobar?user=myadmin&password=myremotepw。