gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:34:02 28 4
gpt4 key购买 nike

给定两台 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,

仍在学习的史蒂夫

最佳答案

完整答案出现在后续问题中

cannot achieve connectivity with MySQL db on remote machine

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

鬼鬼祟祟鬼鬼祟祟的。

结案

仍在学习史蒂夫

关于java 应用程序无法连接到远程 MySQL 数据库,但可以连接到本地 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048585/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com