Tomcat/Hibernate 与 MySql 的连接失败,出现“通信链接失败”和“权限被拒绝”
Posted
技术标签:
【中文标题】Tomcat/Hibernate 与 MySql 的连接失败,出现“通信链接失败”和“权限被拒绝”【英文标题】:Tomcat/Hibernate connection to MySql fails with "Communications link failure" & "Permission denied" 【发布时间】:2018-03-23 04:56:51 【问题描述】:我正在尝试使用 Hibernate 从 tomcat webapp 连接到本地主机上的 mysql (=MariaDB)(我们称之为 A),但不断收到通信链接故障(下面附有完整的异常跟踪)。
我在另一台机器上有另一个数据库副本(我们称之为 B),并且还使用第三台计算机进行开发(我们称之为 C)。
1) 从 C 连接到任一数据库(A 和 B)都没有问题。 2)在“A”上,我在本地连接到数据库没有问题(运行'mysql -u'命令)。我在端口 3306 上对 A 或 B 进行 telnet 也没有问题。所以我假设两个 DB 都已启动并正在运行并且可用于连接。
唯一的问题是当我尝试从生产 tomcat(在 A 上运行)连接到 A 或 B db 时。我得到了错误:
com.mysql.cj.jdbc.exceptions.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.
从这个错误的类似案例中,我发现大多数错误是由于无效的用户/密码凭据或非法 IP 造成的。我还尝试将 ip 更改为 localhost 、 127.0.0.1 和 10.90.41.155 (这是机器的实际 ip)。没有任何帮助,同时请记住,连接只会因 A 上的特定 Tomcat 安装而失败。
我还尝试了以下方法: 1) catalina 启动添加“-Djava.net.preferIPv4Stack=true” 2) 尝试连接到非法地址/端口 - 这将最终错误(如预期)从“权限被拒绝”更改为“没有到主机的路由”。 3)在我的连接字符串中添加了以下内容:“autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8” - 这导致了相同的错误(“尝试重新连接 3 次。放弃。”+“权限被拒绝(连接失败)” 4)尝试了两个数据库网址。对于尝试使用 localhost、127.0.0.1 和实际 IP 的本地数据库。尝试在连接字符串中使用和不使用“:3306”
附件是hibernate.cfg文件+异常的完整服务器跟踪:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://10.90.41.151/boonder
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
XXXXXXXXXX
</property>
<!-- List of XML mapping files -->
<mapping resource="User.hbm.xml"/>
<mapping resource="Issue.hbm.xml"/>
<mapping resource="TraderIssue.hbm.xml"/>
</session-factory>
</hibernate-configuration>
例外是:
type Exception report
message Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ExceptionInInitializerError
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:54)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
...
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
com.mysql.cj.jdbc.exceptions.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.
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:166)
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:58)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
...
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
com.mysql.cj.core.exceptions.CJCommunicationsException: 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.
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.net.SocketException: Permission denied (connect failed)
java.net.PlainSocketImpl.socketConnect(Native Method)
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
java.net.Socket.connect(Socket.java:589)
com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:202)
com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
...
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.76 logs.
【问题讨论】:
在 centos 7.4.1708 和 tmocat 7.0.76 上也有同样的问题 【参考方案1】:这个命令修复了我同样的权限问题:
setsebool -P tomcat_can_network_connect_db 1
【讨论】:
【参考方案2】:我刚刚也追踪到了这个问题。我的症状和你的完全一样,我什至试过你试过的东西。总而言之,我的问题只是因为我在带有 SELinux 的 centos 上使用 tomcat 而出现。
有些人帮助我使用以下命令进行诊断,以查看安全事件以及为什么不允许某些操作:
sudo ausearch -m avc
sudo grep "tcp_socket" /var/log/audit/audit.log | audit2why // translates log into some human readable reasons why the audit record was generated
我看到了这样的条目:
type=AVC msg=audit(1507861264.561:64750): avc: denied name_connect for pid=1326 comm="java" dest=3306 scontext=system_u:system_r:tomcat_t:s0 tcontext=system_u:object_r:mysqld_port_t:s0 tclass=tcp_socket
原因是:缺少类型强制 (TE) 允许规则。
您可以使用 audit2allow 生成可加载模块以允许此访问。
然后发现这篇关于tomcat和SELinux的文章https://noobient.com/post/165972214381/selinux-woes-with-tomcat-on-centos-74
可以运行以下命令查看tomcat是否在tomcat_t安全上下文中运行。
$ps auxZ | grep tomcat
为了真正确定我的错误是否是 SELinux,我禁用了 SELinux
$ sestatus // shows: SELinux status: enabled
$ sudo vim /etc/selinux/config // set "SELINUX=disabled"
$ sudo shutdown -r now
$ sestatus // shows: SELinux status: disabled
重新启动我的机器,发现 tomcat 现在正在建立出站连接。
太好了,现在我至少知道为什么会这样了。当然,如果您需要 SELinux,禁用它不是解决方案。另一个可能不是解决方案的解决方法是在不通过 yum 的情况下安装 tomcat。 Yum 将使用安全上下文安装 tomcat。
我认为现在的行动方案是修改安全上下文或管理 tomcat 的规则集,以允许它建立传出连接。
【讨论】:
感谢 jbu !这对我来说很完美。在被困了这么久之后,这真是一种解脱……我按照你的建议禁用了 SELinux,现在 SQL 可以正常工作了。现在,我将开始深入研究 SELinux 配置,看看如何在不完全禁用它的情况下获得连接。如果您有更多提示,或找到相关内容 - 请分享所需的额外步骤。 Bug 现在已完全解决,无需禁用 SELinux。为了将 tomcat 添加到策略中,请使用以下 2 个命令:` ausearch -c 'java' --raw | audit2allow -M my-java semodule -i my-java.pp `@jbu 这并没有为我解决,但我必须做的是将 jdbc 驱动程序 jar 放在 tomcat 的 /lib 目录中并重新启动它 感谢@jbu 的提示。通过'setsebool -P tomcat_can_network_connect_db 1'修复了问题 @gYanI 这是我,来自他朋友电脑的 Amir Forsati;我的朋友说,如果我帮他解决这个问题,他会付钱给我。谢谢你,你救了他的命和我的钱!以上是关于Tomcat/Hibernate 与 MySql 的连接失败,出现“通信链接失败”和“权限被拒绝”的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat/Hibernate 问题“严重:错误 listenerStart”