简体   繁体   English

Glassfish无法连接到远程MySQL,但localhost可以

[英]Glassfish cannot connect to remote MySQL but localhost can

please give me a chance to explain my mysterious situation. 请给我一个机会解释我的神秘情况。

My Glassfish server cannot connect to the remote MySQL database. 我的Glassfish服务器无法连接到远程MySQL数据库。

I checked following instructions 我检查了以下指示

  1. ping to remote host - OK ping到远程主机-确定
  2. connect via mysql command (mysql -h remote -u user -p) - OK 通过mysql命令连接(mysql -h remote -u user -p)-确定
  3. ssh port forwarding (ssh -L13306:localhost:3306 remote ) and configure the glassfish to use it (jdbc://127.0.0.0:13306/mydb) - OK ssh端口转发(ssh -L13306:localhost:3306 remote )并配置glassfish使用它(jdbc://127.0.0.0:13306 / mydb)-确定

I think my configurations (server name, database name, user name and password) are correct. 我认为我的配置(服务器名称,数据库名称,用户名和密码)是正确的。 But still cannot connect to the database. 但是仍然无法连接到数据库。

My environment: 我的环境:

local side 当地方面

  • Ubuntu 17.04 Ubuntu 17.04
  • Oracle Java 1.8.0 u131 Oracle Java 1.8.0 U131
  • Glassfish 4.1 (build 13) Glassfish 4.1(内部版本13)
  • MySQL Connector/J 5.1.41 MySQL连接器/ J 5.1.41

remote side 偏远的一面

  • FreeBSD 11 FreeBSD 11
  • MySQL 5.5.46 MySQL 5.5.46

I have googled last night, but cannot found the answer. 我昨晚用Google搜寻,但找不到答案。

28 Apr. Edited: 4月28日编辑:

My server.log says: 我的server.log说:

[2017-04-26T09:03:07.932+0900] [glassfish 4.1] [WARNING] [test.connection.pool.failed] [javax.enterprise.resource.resourceadapter.com.sun.enterprise.connectors.service] [tid: _ThreadID=98 _ThreadName=admin-listen
er(6)] [timeMillis: 1493164987932] [levelValue: 900] [[
  RAR8054: Exception while creating an unpooled [test] connection for pool [ MysqlMacms ], Connection could not be allocated because: 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.]]

[2017-04-26T09:03:07.940+0900] [glassfish 4.1] [SEVERE] [] [org.glassfish.admingui] [tid: _ThreadID=49 _ThreadName=admin-listener(5)] [timeMillis: 1493164987940] [levelValue: 1000] [[
  RestResponse.getResponse() gives FAILURE.  endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=admtkMysqlMacms}']]

The first reason for the connection failure might is your jdbc url. 连接失败的第一个原因可能是您的jdbc网址。 You show 你展示

jdbc://127.0.0.0:13306/mydb

Although 172.0.0.0 is a valid loopback address (all 127.0.0.0/8 are) localhost normally is configured to be 127.0.0. 尽管172.0.0.0是有效的回送地址(所有127.0.0.0/8都是),但localhost通常配置为127.0.0。 1 . 1 You are missing as well the mysql specification: 您还缺少mysql规范:

So change that to 所以将其更改为

jdbc:mysql://127.0.0.1:13306/mydb

The second possible reason for the failure: How is access to mysql configured? 失败的第二个可能原因:如何配置对mysql的访问? Has the user access when connecting from 127.0.0.1? 从127.0.0.1连接时用户可以访问吗? When glassfish connects to the local ssh tunnel endpoint, for the mysql server it looks as somebody from it's mysql-server-localhost is connecting. 当glassfish连接到本地ssh隧道端点时,对于mysql服务器,它看起来像是mysql-server-localhost连接的人。

Can you login into the server that glassfish is running on and do a 您能否登录到运行glassfish的服务器并执行

mysql -h 127.0.0.1 --port 13306 -u user -p

from there. 从那里。 If this is not working you check the access configuration for users accessing from localhost and 127.0.0.1 如果这不起作用,则检查从本地主机和127.0.0.1访问的用户的访问配置。

the third reason might be a missing installation of the mysql connector library in Glassfish. 第三个原因可能是在Glassfish中缺少mysql连接器库的安装。 This is described in the mysql doc , basically: 基本上在mysql doc中对此进行了描述

Once GlassFish is installed, make sure it can access MySQL Connector/J. 安装GlassFish后,请确保它可以访问MySQL Connector / J。 To do this, copy the MySQL Connector/J jar file to the domain-dir/lib directory. 为此,请将MySQL Connector / J jar文件复制到domain-dir / lib目录。 For example, copy mysql-connector-java-5.1.30-bin.jar to C:\\glassfish-install-path\\domains\\domain-name\\lib. 例如,将mysql-connector-java-5.1.30-bin.jar复制到C:\\ glassfish-install-path \\ domains \\ domain \\ name \\ lib。 Restart the GlassFish Application Server. 重新启动GlassFish Application Server。 For more information, see “Integrating the JDBC Driver” in GlassFish Server Open Source Edition Administration Guide, available at GlassFish Server Documentation. 有关更多信息,请参见GlassFish Server文档中的《 GlassFish Server开源版管理指南》中的“集成JDBC驱动程序”。

Update 更新资料

added the :mysql: part in the connection url and reference to connector installation. 在连接URL中添加了:mysql:部分,并引用了连接器安装。

I tried it again, the problem no longer reproduced. 我再次尝试,问题不再出现。

I am not sure if my configuration somewhere wrong. 我不确定我的配置是否错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM