简体   繁体   English

自动创建数据库 springboot 和 mysql

[英]Auto database creation springboot and mysql

I am trying create mysql database when application starts up.我正在尝试在应用程序启动时创建 mysql 数据库。 I have tried with bellow configuration but could not able to achieve, Please let me know if anybody have idea about this,我尝试了以下配置,但无法实现,如果有人对此有想法,请告诉我,

spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

If you are looking for to create database if not exists then you can use below in database configuration file.如果您正在寻找创建数据库(如果不存在),那么您可以在数据库配置文件中使用下面的内容。

jdbc:mysql://localhost:3306/dbname?createDatabaseIfNotExist=true

Otherwise make sure you have below properties in your application properties file.否则,请确保您的应用程序属性文件中有以下属性。

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=username
spring.datasource.password=password

You should be setting the property with a value that actually creates the schema, for instance:您应该使用实际创建架构的值设置属性,例如:

spring.jpa.hibernate.ddl-auto=create-drop

You may find a more detailed reference here .您可以在此处找到更详细的参考资料。

I have added the following option:我添加了以下选项:

"createDatabaseIfNotExist=true"
spring.datasource.url=jdbc:mysql://localhost:3306/"DB_NAME"?createDatabaseIfNotExist=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM