Re-organise mysql scripts so the app runs without root access

It works better that way with test containers and in k8s.
This commit is contained in:
Dave Syer 2020-01-03 05:29:11 -05:00
parent f9424b548a
commit 5d57e0d5e2
5 changed files with 17 additions and 15 deletions

View file

@ -3,7 +3,10 @@ mysql:
ports: ports:
- "3306:3306" - "3306:3306"
environment: environment:
- MYSQL_ROOT_PASSWORD=petclinic - MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_USER=petclinic
- MYSQL_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic - MYSQL_DATABASE=petclinic
volumes: volumes:
- "./conf.d:/etc/mysql/conf.d:ro" - "./conf.d:/etc/mysql/conf.d:ro"

View file

@ -1,7 +1,7 @@
# database init, supports mysql too # database init, supports mysql too
database=mysql database=mysql
spring.datasource.url=jdbc:mysql://localhost/petclinic spring.datasource.url=jdbc:mysql://localhost/petclinic
spring.datasource.username=root spring.datasource.username=petclinic
spring.datasource.password=petclinic spring.datasource.password=petclinic
# SQL is written to be idempotent so this is safe # SQL is written to be idempotent so this is safe
spring.datasource.initialization-mode=always spring.datasource.initialization-mode=always

View file

@ -18,12 +18,14 @@
mysql_1_eedb4818d817 | MySQL init process done. Ready for start up. mysql_1_eedb4818d817 | MySQL init process done. Ready for start up.
... ...
2) Create the PetClinic database and user by executing the "db/mysql/{schema,data}.sql" 2) (Once only) create the PetClinic database and user by executing the "db/mysql/user.sql"
scripts (or set "spring.datasource.initialization-mode=always" the first time you run the app). scripts. You can connect to the database running in the docker container using
`mysql -u root -h localhost --protocol tcp`, but you don't need to run the script there
because the petclinic user is already set up if you use the provided `docker-compose.yaml`.
3) Run the app with `spring.profiles.active=mysql` (e.g. as a System property via the command 3) Run the app with `spring.profiles.active=mysql` (e.g. as a System property via the command
line, but any way that sets that property in a Spring Boot app should work). line, but any way that sets that property in a Spring Boot app should work).
N.B. the "petclinic" database has to exist for the app to work with the JDBC URL value N.B. the "petclinic" database has to exist for the app to work with the JDBC URL value
as it is configured by default. This condition is taken care of by the docker-compose as it is configured by default. This condition is taken care of by the docker-compose
configuration provided, or by the `schema.sql` if you can run that as root. configuration provided, or by the `schema.sql` if you can run that as root.

View file

@ -1,13 +1,3 @@
CREATE DATABASE IF NOT EXISTS petclinic;
ALTER DATABASE petclinic
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON petclinic.* TO pc@localhost IDENTIFIED BY 'pc';
USE petclinic;
CREATE TABLE IF NOT EXISTS vets ( CREATE TABLE IF NOT EXISTS vets (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30), first_name VARCHAR(30),

View file

@ -0,0 +1,7 @@
CREATE DATABASE IF NOT EXISTS petclinic;
ALTER DATABASE petclinic
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON petclinic.* TO 'petclinic@%' IDENTIFIED BY 'petclinic';