在Spring Boot中嵌入式H2数据库的默认名称是什么?

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

What is the default name of embedded H2 database in Spring Boot?

问题

根据我阅读的内容,嵌入式 H2 数据库在 Spring Boot 中的默认名称应该是 testdb,但是如果我尝试使用 H2 控制台连接,会出现以下错误:

数据库 "mem:testdb" 未找到,请预先创建它或允许远程数据库创建(在安全环境中不推荐)

只有在我在 application.properties 文件中使用以下参数显式设置名称时,它才能正常工作:

spring.datasource.url=jdbc:h2:mem:testdb

由于应用程序可以在没有这个配置的情况下连接到嵌入式数据库,所以肯定存在不同的默认名称。但自动配置的数据库的默认名称是什么?

英文:

As I've read the default name of the embedded H2 database in Spring Boot should be testdb, but if I try to connect to with the H2 Console, I get the following error:

> Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments)

It works only if I set the name explicitly in the application.properties with the following parameter:

spring.datasource.url=jdbc:h2:mem:testdb

Since the application can connect to the embedded database without this configuration, there must be a different default name. But what is the default name of the automatically configured database?

答案1

得分: 5

application.properties中按照 jurez 所说启用 h2 控制台:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true

然后当你在控制台中运行应用程序时,你会看到类似以下的信息:

H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc'.

然后你可以使用这个 JDBC URL 来从 h2 控制台连接到数据库:

http://localhost:8080/h2-console

jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc

你可以通过在 application.properties 文件中添加以下属性来指定自己的数据库名称:

spring.datasource.url=jdbc:h2:mem:testdb

请记住,由于这是一个内存数据库,每次运行应用程序时都会被删除和重新创建。

h2 也有一个持久模式,但不建议使用,你可以通过添加以下配置来实现,这些配置取自以下教程 Spring Boot and H2 in-memory database - Why, What and How?

spring.datasource.name=yourdbname
spring.datasource.driverClassName=org.h2.Driver
     
spring.datasource.initialize=false
spring.datasource.url=jdbc:h2:file:~/yourdbname;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
     
spring.jpa.hibernate.ddl-auto=update
英文:

Enable h2 console in application.properties as jurez said

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true

and then when you run the application in the console you will see something like this:

H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc'

then you can use this JDBC URL to connect to the database from the h2-console

http://localhost:8080/h2-console

jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc

you can specify your own name by adding the following property in the application.property file

spring.datasource.url=jdbc:h2:mem:testdb

remember since this is an in-memory database it will be dropped and recreated every time you run your application

h2 also has a persistent mode but it is not recommended, you can do that by adding the following configurations taken from the following tutorial Spring Boot and H2 in memory database - Why, What and How?

spring.datasource.name=yourdbname
spring.datasource.driverClassName=org.h2.Driver
 
spring.datasource.initialize=false
spring.datasource.url=jdbc:h2:file:~/yourdbname;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
 
spring.jpa.hibernate.ddl-auto = update

答案2

得分: 0

默认情况下,内存数据库只能被运行它们的进程访问。

如果你想从H2控制台访问它,你需要在application.properties中启用它:

spring.h2.console.enabled=true
英文:

By default, memory databases are only accessible to the process in which they are running.

If you want to access it from H2 Console, you need to enable it in application.properties:

spring.h2.console.enabled=true

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

发表评论

匿名网友

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

确定