java 我的数据源设置有什么问题让 Spring Boot 连接到 MySQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28233003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:13:39  来源:igfitidea点击:

What is wrong with my datasource settings to get Spring Boot to connect to MySQL?

javamysqlspringhibernatespring-boot

提问by user781486

I followed the tutorial below to learn how to write Spring Boot app to access MySQL.

我按照下面的教程学习了如何编写 Spring Boot 应用程序来访问 MySQL。

http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/

http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/

There is an error message with connection to the MySQL database. http://pastebin.com/ykYJacjZ

连接到 MySQL 数据库时出现错误消息。http://pastebin.com/ykYJacjZ

I believe something is wrong with the DataSource Settings. Below are my settings;

我相信数据源设置有问题。以下是我的设置;

spring.datasource.url = jdbc:mysql://localhost:3360/netgloo_blog
spring.datasource.username = root
spring.datasource.password = ''
spring.datasource.driverClassName = com.mysql.jdbc.Driver

I am running a MySQL database using Xampp on port 3360. A database 'netgloo_blog' has been created. I have also created a table userswith the fields id, email, name.

我在端口 3360 上使用 Xampp 运行 MySQL 数据库。已创建数据库“netgloo_blog”。我还创建了一个users包含字段idemail、的表name

回答by aksamit

Have you tried connecting to the MySQL server using the CLI?

您是否尝试过使用 CLI 连接到 MySQL 服务器?

mysql --host=localhost --port=3360 --user=root --password

I believe you either got the port wrong or the credentials. Default MySQL port is 3306, can it be a typo in the port-number you have specified?

我相信您要么是端口错误,要么是凭据错误。MySQL 默认端口是 3306,这可能是您指定的端口号中的错字吗?

回答by Pankaj

Not connecting to DB means there is failure with the DB connection. Either the credentials are not working or DB service is not reachable to your system. As your URL states that it is in your local system only. So, try connecting with the mysql CLI as already stated above by aksamit.

未连接到 DB 意味着 DB 连接失败。您的系统无法访问凭据或数据库服务。正如您的 URL 所述,它仅在您的本地系统中。因此,请尝试使用 aksamit 上面已经说明的 mysql CLI 进行连接。

After providing the correct credentials even the below properties will work fine.

提供正确的凭据后,即使以下属性也能正常工作。

spring.datasource.url = jdbc:mysql://localhost:3360/netgloo_blog
spring.datasource.username = root
spring.datasource.password =

There is no need to specify the driverClassName util you want to use some non-generic driver class. Spring boot will select the driver based on the URL provided and the driver dependancy present in the pom.xml Let us know if you still face issue while connecting to the DB.

无需指定要使用某些非通用驱动程序类的 driverClassName 实用程序。Spring boot 将根据提供的 URL 和 pom.xml 中存在的驱动程序依赖性选择驱动程序,如果您在连接到数据库时仍然遇到问题,请告诉我们。

回答by MarcS

Maybe you need to grant access to root from localhost.

也许您需要从 localhost 授予对 root 的访问权限。

If this persists I recommend you to create a new user into your sql and then try to enter with him.

如果这种情况持续存在,我建议您在 sql 中创建一个新用户,然后尝试与他一起输入。