如何配置Spring Boot连接一个Docker化的MySQL数据库?

huangapple go评论57阅读模式
英文:

How can I configure Spring Boot to connect a dockerized MySql database?

问题

The exception you are encountering when starting your project is related to an access denied error for the MySQL user 'root'@'localhost' with the provided password.

在启动项目时出现的异常与MySQL用户'root'@'localhost'的访问被拒绝以及提供的密码有关。

To resolve this issue, you should verify the following:

为解决此问题,您应该验证以下内容:

  1. Ensure that the MySQL server is up and running.

确保MySQL服务器正在运行。

  1. Double-check the password in your application.properties file for the spring.datasource.password property. Make sure it matches the actual password for the 'root' user in your MySQL server.

仔细检查application.properties文件中spring.datasource.password属性的密码。确保它与MySQL服务器中'root'用户的实际密码匹配。

  1. Verify that the MySQL server allows connections from 'root'@'localhost'. You might need to check MySQL's user privileges and configurations.

验证MySQL服务器允许来自'root'@'localhost'的连接。您可能需要检查MySQL用户权限和配置。

  1. Ensure that there are no typos or special characters in your docker-compose.yml file that could affect the MySQL container's configuration.

确保docker-compose.yml文件中没有可能影响MySQL容器配置的拼写错误或特殊字符。

  1. Make sure your Spring Boot application is running on the same machine where MySQL is hosted if you're connecting to 'localhost'.

如果您要连接到'localhost',请确保您的Spring Boot应用程序在托管MySQL的同一台计算机上运行。

  1. Check for any firewall or network issues that might be preventing the connection to the MySQL server.

检查是否有防火墙或网络问题可能阻止连接到MySQL服务器。

  1. Ensure that your MySQL server is configured to accept password-based authentication.

确保您的MySQL服务器配置为接受基于密码的身份验证。

By verifying these points, you should be able to identify and resolve the issue with your connection configuration. If the problem persists, please provide more specific details about your setup for further assistance.

英文:

Whatever I try the following exception occurs when I start the project:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=MyPassword

docker-compose.yml

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mysql:8.0
    # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (this is just an example, not intended to be a production configuration)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MyPassword

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

I successfully connect to that database loging into Adminer with the above mentioned credentials.

SELECT user, plugin FROM mysql.user;

shows:

| user    | plugin
+---------+-----------------------
| root    | mysql_native_password

The database and Adminer are started in a docker-compose file and I start separately the Spring Boot project without using Docker. Can you please tell me what's wrong with the connection's configuration?

org.springframework.boot -> 3.0
OS -> Ubuntu 22.04
Dockerized MySql -> 8.0

</details>


# 答案1
**得分**: 2

如果您想连接到作为Docker容器运行的服务,还需要暴露MySQL端口:

```yaml
version: '3.1'

services:

  db:
    image: mysql:8.0
    # 注意:不推荐使用“mysql_native_password”:https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (这只是一个示例,不打算用于生产配置)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MyPassword
    ports:
      - 3306:3306

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
英文:

If you want to connect to the service running as docker container you also have to expose the MySQL ports:

version: &#39;3.1&#39;

services:

  db:
    image: mysql:8.0
    # NOTE: use of &quot;mysql_native_password&quot; is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (this is just an example, not intended to be a production configuration)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MyPassword
    ports:
      - 3306:3306

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

huangapple
  • 本文由 发表于 2023年6月26日 22:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定