如何从 Spring Boot 应用程序连接到远程 MySQL 服务器

问题描述 投票:0回答:1

**问题:**

我想将我的

spring boot application
连接到位于
remote MySql server
中的数据库,但它显示“连接被拒绝”,您可以在以下部分中找到错误的详细信息。

错误:

Caused by: com.mysql.cj.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.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850)
    ... 50 more
Caused by: java.net.ConnectException: Connection refused: no further information
    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 52 more

注意: 我可以使用相同的凭据通过

mysql workbench
从我的计算机/系统访问相同的数据库。

采取的步骤:

  1. 创建了一个用于远程访问的数据库用户
    remoteUser@%
  2. 授予新创建的用户对所有数据库的完全权限。
  3. 刷新特权。
  4. 允许端口 3308 上的传入流量
    sudo ufw allow 3308
  5. 更新了 mysqld.cnf 文件。

#commented both of the following lines
# bind-address      = 127.0.0.1
# mysqlx-bind-address   = 127.0.0.1

  1. 重新启动mysql服务器
    sudo service mysql restart
  2. 更新了 Spring Boot 应用程序属性文件中的连接字符串。
 spring.datasource.url=jdbc:mysql://server_ip:3308/db_name
 spring.datasource.username=remoteUser
 spring.datasource.password=remotePassword
  1. 重新启动 Spring Boot 应用程序。

问题尚未解决,需要您的帮助。

mysql spring-boot
1个回答
0
投票

我在 Mysqld.cnf 文件中仅添加一行就遇到了问题。

bind-address      = 0.0.0.0

这就是所需要的。

© www.soinside.com 2019 - 2024. All rights reserved.