在AWS部署应用程序时出现错误:java.sql.SQLException: 未选择数据库。

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

Deploying app in aws throws error java.sql.SQLException: No database selected

问题

好的,以下是翻译好的部分:

开发者们,我正在处理这个Angular+MySQL+Spring-Boot应用程序,我现在正处于将我的数据库导出到AWS的阶段,但由于某种原因,在测试了与MySQL Workbench的数据库连接之后,当我在application.properties中设置参数时,会出现错误:

java.sql.SQLException: 未选择数据库

AWS抛出的终端节点和端口分别是:

终端节点:bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com
端口:3306

因此,在我的app.properties中使用这些元素,我只初始化了:

spring.datasource.url=jdbc:mysql://bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com:3306/restaurant_booking_ok?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=ssssssssssss
spring.datasource.password=sssssssssssssss
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
logging.level.org.hibernate.SQL=debug

我在这里暴露了Workbench数据库及其终端节点的图像。

但是,每当我尝试访问应用程序中的任何端点时,都会出现“未选择数据库”的500错误。您能帮助我看看我漏掉了哪个步骤吗?

我知道这是很多代码,但我也附上了我的pom文件,以防您发现有什么问题:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>cubancoder</groupId>
    <artifactId>restaurant-booking</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>restaurant-booking</name>
    <description>Demo project for Spring Boot made on maven front end angular</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail 
            dependencia para el envio de emails -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.4.6</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.stripe/stripe-java dependencia para el wireling de pagos y demas-->
        <dependency>
            <groupId>com.stripe</groupId>
            <artifactId>stripe-java</artifactId>
            <version>19.45.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

提前感谢您的帮助。
英文:

Good day developers , im working in this Angular+MySQL+Spring-Boot app, and i'm in that phase of exporting my database to AWS, but for any reason after having tested the database connection to my mySQL Workbench , when i set the parameters in the application.properties the error
java.sql.SQLException: No database selected
appears.

The endpoint AWS throws as well as the port are :

endpoint: bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com

port: 3306

thus using this elements on my app.properties i just initialize



spring.datasource.url=jdbc:mysql://bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com
:3306/restaurant_booking_ok?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC

spring.datasource.username=ssssssssssss
spring.datasource.password=sssssssssssssss

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

logging.level.org.hibernate.SQL=debug

I'm exposing here the workbench database and its endpoint

在AWS部署应用程序时出现错误:java.sql.SQLException: 未选择数据库。

在AWS部署应用程序时出现错误:java.sql.SQLException: 未选择数据库。

But any time i try to reach whichever the endpoints in my app the error 500 of no database selected shows up. Could you help me to see what step im ommiting

I know is a lot of code but also i attach my pom in case you detect something is wrong there


&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.2.RELEASE&lt;/version&gt;
		&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;cubancoder&lt;/groupId&gt;
	&lt;artifactId&gt;restaurant-booking&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;restaurant-booking&lt;/name&gt;
	&lt;description&gt;Demo project for Spring Boot made on maven  front end angular&lt;/description&gt;

	&lt;properties&gt;
		&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
			&lt;version&gt;8.0.18&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.modelmapper&lt;/groupId&gt;
			&lt;artifactId&gt;modelmapper&lt;/artifactId&gt;
			&lt;version&gt;2.3.8&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;


		&lt;!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
			&lt;version&gt;2.9.2&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail 
			dependencia para el envio de emails --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
			&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/junit/junit --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;junit&lt;/groupId&gt;
			&lt;artifactId&gt;junit&lt;/artifactId&gt;
			&lt;version&gt;4.13&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/org.mockito/mockito-all --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.mockito&lt;/groupId&gt;
			&lt;artifactId&gt;mockito-all&lt;/artifactId&gt;
			&lt;version&gt;1.10.19&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/org.mockito/mockito-core --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.mockito&lt;/groupId&gt;
			&lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
			&lt;version&gt;3.4.6&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

		&lt;!-- https://mvnrepository.com/artifact/com.stripe/stripe-java dependencia para el wireling de pagos
		y demas--&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.stripe&lt;/groupId&gt;
			&lt;artifactId&gt;stripe-java&lt;/artifactId&gt;
			&lt;version&gt;19.45.0&lt;/version&gt;
		&lt;/dependency&gt;



	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

Thanks in advance for your help

答案1

得分: 5

错误 java.sql.SQLException: No database selected 似乎是指数据库名称。

数据库名称在属性 spring.datasource.url 中设置正确,然而对于亚马逊 RDS 来说,只需将该属性指向端点即可(无需包含数据库名称)。请尝试以下操作:

spring.datasource.url=jdbc:mysql://bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com
:3306

编辑:

我成功地重现了这个错误。问题在于亚马逊 RDS 默认情况下创建的是新的数据库实例,而不是数据库。要创建数据库,有以下选项需要设置:

在AWS部署应用程序时出现错误:java.sql.SQLException: 未选择数据库。

另外,错误不会在连接到数据库后立即出现,而是在第一次查询之后(例如创建新表)出现。

将“Initial database name”设置为 mydb,并且数据库实例 ID 为 database-instance-1,那么 URL 将会是:

spring.datasource.url=jdbc:mysql://database-instance-1.blablabla.rds.amazonaws.com:3306/mydb

我知道你的 spring.datasource.url 看起来是正确的,并且你可以在 Workbench 中看到你的数据库,但我建议你使用亚马逊 RDS 仪表盘创建一个新的数据库实例,并根据上述说明设置数据库名称。

这并不是最终的答案,但我希望这个尝试能帮助你找出问题的原因。

英文:

The error java.sql.SQLException: No database selected seems to refer to the database name.

The database name is set correctly in the property spring.datasource.url, however for Amazon RDS it's enough to point that property to the endpoint (without the database name). Try the following:

spring.datasource.url=jdbc:mysql://bookingrestaurantok.clnhc7kmwszz.us-west-2.rds.amazonaws.com
:3306

Edit:

I was able to reproduce the error. The issue is that Amazon RDS by default creates a new database instance but not a database. To create a database there is this option to set:

在AWS部署应用程序时出现错误:java.sql.SQLException: 未选择数据库。

Also, the error does not appear after connecting to the database, but only after the first query (for example creating a new table).

Setting "Initial database name" to mydb and having a database instance id database-instance-1, then the URL would be:

spring.datasource.url=jdbc:mysql://database-instance-1.blablabla.rds.amazonaws.com:3306/mydb

I know that your spring.datasource.url looks correct and you can see your database in Workbench but I suggest you create a new database instance using the Amazon RDS dashboard and setting the database name as explained above.

It's not a definitive answer but I hope this tentative will let you figure out the cause of the issue.

答案2

得分: -1

  1. 请检查部署应用程序的节点与包含数据库的实例之间的连接情况。
  2. 您是否为两个节点提供了足够的通信权限?
  3. 从应用程序机器到数据库机器进行ping测试。
  4. 在应用程序机器上安装mysql客户端,尝试连接到数据库。
  5. 数据库在该数据库服务器中可用吗?
英文:
  1. Please check the connectivity between the node in which application is deployed and the instance in which DB is there.
  2. Have you provided enough permissions for both nodes for communications ?.
  3. Do ping from application machine to db machine.
  4. Install mysql client in application machine and try to connect to the db.
  5. Is the database available in that database server ?

huangapple
  • 本文由 发表于 2020年9月9日 16:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63807934.html
匿名

发表评论

匿名网友

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

确定