Spring Boot + MySQL Docker
Recursos:
https://spring.io/guides/gs/accessing-data-mysql/ https://spring.io/guides/tutorials/rest/
Dependências: Spring Web, Spring Data JPA, and MySQL Driver.
Docs: https://hub.docker.com/_/mysql
docker pull mysql
docker run --name mysql_container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=s3cret -d mysql
docker ps
docker exec -it mysql_container_id sh
mysql -p
Entrar a senha s3cret
. Agora temos que criar o banco de dados e dar privilégios a ele. Exemplo:
mysql> create database db_example; -- Creates the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database
Ir para src/main/resources/application.properties
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
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Não mandatório
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
#spring.jpa.show-sql: true
Quando fizer mudanças em um banco de dados:
-
Resete as permissões:
mysql> revoke all on db_example.* from 'springuser'@'%'; mysql> grant select, insert, delete, update on db_example.* to 'springuser'@'%';
-
Mude o
spring.jpa.hibernate.ddl-auto
paraudpate
-
Reinicie o app