Skip to content

Instantly share code, notes, and snippets.

@LaurenceZanotti
Last active July 18, 2022 15:16
Show Gist options
  • Save LaurenceZanotti/70977d9a07cb906a06dc3ec469933b7c to your computer and use it in GitHub Desktop.
Save LaurenceZanotti/70977d9a07cb906a06dc3ec469933b7c to your computer and use it in GitHub Desktop.
Backend Dev Enviroment

Backend Dev Enviroment

Spring Boot + MySQL Docker

Recursos:

https://spring.io/guides/gs/accessing-data-mysql/ https://spring.io/guides/tutorials/rest/

Iniciar projeto

https://start.spring.io

Dependências: Spring Web, Spring Data JPA, and MySQL Driver.

MySQL Docker

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

Conexão do Spring Boot com MySQL

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

Segurança

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 para udpate

  • Reinicie o app

MySQL na máquina local

https://dev.mysql.com/downloads/windows/installer/8.0.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment