如图,开放了端口3306
但是程序连接3306端口的数据库连接不上
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.22.jar:8.0.22]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:115) [HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar:na]
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.3.3.jar:5.3.3]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.3.3.jar:5.3.3]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.3.3.jar:5.3.3]
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80) [mybatis-spring-2.0.4.jar:2.0.4]
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67) [mybatis-spring-2.0.4.jar:2.0.4]
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336) [mybatis-3.5.4.jar:3.5.4]
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.prepareStatement(MybatisSimpleExecutor.java:91) [mybatis-plus-core-3.3.2.jar:3.3.2]
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:66) [mybatis-plus-core-3.3.2.jar:3.3.2]
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) [mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) [mybatis-3.5.4.jar:3.5.4]
at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:163) [mybatis-plus-core-3.3.2.jar:3.3.2]
at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:90) [mybatis-plus-core-3.3.2.jar:3.3.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) [mybatis-3.5.4.jar:3.5.4]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) [mybatis-3.5.4.jar:3.5.4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) [mybatis-spring-2.0.4.jar:2.0.4]
at com.sun.proxy.$Proxy62.selectList(Unknown Source) [na:na]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223) [mybatis-spring-2.0.4.jar:2.0.4]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:134) [mybatis-plus-core-3.3.2.jar:3.3.2]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:96) [mybatis-plus-core-3.3.2.jar:3.3.2]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96) [mybatis-plus-core-3.3.2.jar:3.3.2]
at com.sun.proxy.$Proxy63.selectPage(Unknown Source) [na:na]
at com.baomidou.mybatisplus.extension.service.IService.page(IService.java:294) [mybatis-plus-extension-3.3.2.jar:3.3.2]
at com.hw.haowumall.coupon.service.impl.CouponServiceImpl.queryPage(CouponServiceImpl.java:21) [classes/:na]
at com.hw.haowumall.coupon.service.impl.CouponServiceImpl$$FastClassBySpringCGLIB$$266d816a.invoke() [classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) [spring-core-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) [spring-aop-5.3.3.jar:5.3.3]
at com.hw.haowumall.coupon.service.impl.CouponServiceImpl$$EnhancerBySpringCGLIB$$84f6cf86.queryPage() [classes/:na]
at com.hw.haowumall.coupon.controller.CouponController.list(CouponController.java:39) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) [spring-web-5.3.3.jar:5.3.3]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) [spring-web-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.3.3.jar:5.3.3]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.3.3.jar:5.3.3]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) [tomcat-embed-core-9.0.41.jar:4.0.FR]
at
注意:1.数据库已经永久的开放了此端口对于所有用户的登录
那么就尝试了一下将linux的防火墙关闭了,然后就可以了,果然是防火墙的问题
后台成功返回
那么就到解决问题的时候了
1.服务器防火墙开启是必要的安全,不能关闭!!!!
所以关闭防火墙是错误的做法,
那么为啥开放了端口还是被拦截了呢?
我们的防火墙默认规则如下:
如果防火墙放行了端口,但是仍然访问不到的话,可能是因为添加规则的时候,用的是iptables -A 选项,这样,增加的规则会排列在 第6条 规则后面,虽然service iptables status显示放行了端口,但是实际上,由于第六条规则的原因,新增加的这条并没有起作用。
改为使用iptables -I 插入规则即可,将规则添加的 第6条 之前,就可以生效了。
详情的iptables的过滤链还有很多学问,需要慢慢学习,
***************************************************************************下面是百度的内容
Iptables 是采取数据包过滤机制工作的,所以它会对请求的数据包的包头数据进行分析、并根据我们预先设定的规则进行匹配来决定是否可以进入、流出、流经主机。下面我们以 iptables 进入主机进行过滤的流程图为例子
提示:
防火墙规则的执行顺序认为从前到后依次执行、遇到匹配的规则就不在继续向下检查、
如果遇到不匹配的规则则会继续向下进行。
重点: 匹配上了拒接规则也是匹配。因此不在继续向下进行、这一点大家要注意、
例如:同时执行了以下规则
[root@Nagios2 ~]# iptables -A INPUT -p tcp --dport 3306 -j DROP
[root@Nagios2 ~]# iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
此时通过命令 telnet 192.168.132.194 3306 检查3306是不通的。原因就是telnet请求以匹配上了拒接规则、因此不会在找下面的允许规则、如果需要telnet 192.168.132.194 3306 连通、可以把ACCEPT规则中的-A 改为-I 即为 iptables –I INPUT –p tcp –dport 3306 –j ACCEPT 把允许规则防御INPUT链的第一行生效