diff --git a/docker-compose.yml b/docker-compose.yml index 0f4a7fc3..5166fe90 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,10 @@ mysql: ports: - "3306:3306" environment: - - MYSQL_ROOT_PASSWORD=petclinic + - MYSQL_ROOT_PASSWORD= + - MYSQL_ALLOW_EMPTY_PASSWORD=true + - MYSQL_USER=petclinic + - MYSQL_PASSWORD=petclinic - MYSQL_DATABASE=petclinic volumes: - "./conf.d:/etc/mysql/conf.d:ro" diff --git a/readme.md b/readme.md index 37b44292..715eb65b 100644 --- a/readme.md +++ b/readme.md @@ -45,9 +45,11 @@ Note that whenever the database type is changed, the data-access.properties file You could start a MySql database with docker: ``` -docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7 +docker run -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8 ``` +Further documentation is provided [here](https://github.com/spring-projects/spring-petclinic/blob/master/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt). + ## Looking for something in particular? diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index 823b32b6..6d54ddad 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -1,7 +1,7 @@ # database init, supports mysql too database=mysql spring.datasource.url=jdbc:mysql://localhost/petclinic -spring.datasource.username=root +spring.datasource.username=petclinic spring.datasource.password=petclinic -# Uncomment this the first time the app runs -# spring.datasource.initialization-mode=always +# SQL is written to be idempotent so this is safe +spring.datasource.initialization-mode=always diff --git a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt b/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt index 6733a933..270274b2 100644 --- a/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt +++ b/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt @@ -9,9 +9,24 @@ -------------------------------------------------------------------------------- 1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x), - which can be found here: http://dev.mysql.com/downloads/. Or run the + which can be found here: https://dev.mysql.com/downloads/. Or run the "docker-compose.yml" from the root of the project (if you have docker installed - locally). + locally): -2) Create the PetClinic database and user by executing the "db/mysql/{schema,data}.sql" - scripts (or set "spring.datasource.initialize=true" the first time you run the app). + $ docker-compose up + ... + mysql_1_eedb4818d817 | MySQL init process done. Ready for start up. + ... + +2) (Once only) create the PetClinic database and user by executing the "db/mysql/user.sql" + 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 + 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 +as it is configured by default. This condition is taken care of automatically by the +docker-compose configuration provided, or by the `user.sql` script if you run that as +root. diff --git a/src/main/resources/db/mysql/schema.sql b/src/main/resources/db/mysql/schema.sql index 6a982598..eb5d7d5d 100644 --- a/src/main/resources/db/mysql/schema.sql +++ b/src/main/resources/db/mysql/schema.sql @@ -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 ( id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(30), diff --git a/src/main/resources/db/mysql/user.sql b/src/main/resources/db/mysql/user.sql new file mode 100644 index 00000000..60abcee7 --- /dev/null +++ b/src/main/resources/db/mysql/user.sql @@ -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';