针对MySQL,Spring Boot有官方教程,地址是https://spring.io/guides/gs/accessing-data-mysql/

不过比较坑的是,有些Linux的发行版,用mariadb代替了MySQL。比如在Fedora里使用sudo dnf install mysql-server,实际上安装的是mariadb。
我使用的是macOS,因为讨厌Oracle,拥护开源,就使用了mariadb。
mariadb跟MySQL是冲突的,想要卸载mariadb替换成MySQL,不是容易的事儿。
所以我就干脆用mariadb替换MySQL。

如果更改的地方如下
MySQL的依赖是

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

mariadb的依赖是

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
</dependency>

application.properties也需要修改
MySQL的是

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

mariadb的是

spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

需要注意的是,在给数据库创建用户,给权限的时候,可能要修改%成localhost
官方教程的语句是grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database
修改成grant all on db_example.* to 'springuser'@'localhost'; -- Gives all privileges to the new user on the newly created database

DONE.

参考链接https://blog.csdn.net/Pagegle/article/details/104212854

 posted on 2020-09-21 12:57  gimp  阅读(761)  评论(0编辑  收藏  举报