ソースを参照

refactor: import the sql file when the service starts

gin 5 日 前
コミット
cc20e89161
3 ファイル変更35 行追加4 行削除
  1. 26 3
      README.md
  2. 4 0
      backend/docker-compose.yml
  3. 5 1
      backend/sql/agileboot.sql

+ 26 - 3
README.md

@@ -34,8 +34,31 @@ cd backend
 docker compose up -d
 ```
 
-On a fresh Docker volume, Compose creates the MySQL database `agileboot_pure`.
-Import the SQL files under `backend/sql/` before starting the backend.
+The compose configuration mounts `backend/sql/agileboot.sql` into the MySQL
+container as `/docker-entrypoint-initdb.d/01-agileboot.sql`.
+The official MySQL image only runs files in `/docker-entrypoint-initdb.d` when
+the database directory is empty, during the first initialization of the
+`mysql_data` volume.
+
+If MySQL has already been started before, the `mysql_data` volume already
+contains data and `docker compose up -d` will not import the SQL again. To
+reinitialize the local database, remove the volumes first:
+
+```bash
+cd backend
+docker compose down -v
+docker compose up -d
+```
+
+Warning: `docker compose down -v` deletes the local MySQL and Redis volumes,
+including existing database data and Redis data.
+
+If you do not want to delete the volumes, import the SQL manually:
+
+```bash
+cd backend
+docker exec -i mysql-server mysql -uroot -proot123 agileboot_pure < sql/agileboot.sql
+```
 
 ### Install Frontend Dependencies
 
@@ -50,7 +73,7 @@ Start the backend:
 
 ```bash
 cd backend
-./mvnw -pl agileboot-admin -am spring-boot:run
+./mvnw -pl agileboot-admin spring-boot:run
 ```
 
 Start the web frontend:

+ 4 - 0
backend/docker-compose.yml

@@ -3,6 +3,9 @@ services:
     image: mysql:8.0
     container_name: mysql-server
     restart: always
+    command:
+      - --character-set-server=utf8mb4
+      - --collation-server=utf8mb4_unicode_ci
     environment:
       MYSQL_ROOT_PASSWORD: root123
       MYSQL_DATABASE: agileboot_pure
@@ -10,6 +13,7 @@ services:
       - "3306:3306"
     volumes:
       - mysql_data:/var/lib/mysql
+      - ./sql/agileboot.sql:/docker-entrypoint-initdb.d/01-agileboot.sql:ro
     networks:
       - db-network
 

+ 5 - 1
backend/sql/agileboot-20230814.sql → backend/sql/agileboot.sql

@@ -1,3 +1,8 @@
+SET NAMES utf8mb4;
+SET character_set_client = utf8mb4;
+SET character_set_connection = utf8mb4;
+SET character_set_results = utf8mb4;
+
 create table sys_config
 (
     config_id       int auto_increment comment '参数主键'
@@ -359,4 +364,3 @@ create table sys_user
 INSERT INTO  sys_user (user_id, post_id, role_id, dept_id, username, nickname, user_type, email, phone_number, sex, avatar, password, status, login_ip, login_date, is_admin, creator_id, create_time, updater_id, update_time, remark, deleted) VALUES (1, 1, 1, 4, 'admin', 'valarchie1', 0, 'agileboot@163.com', '15888888883', 0, '/profile/avatar/20230725164110_blob_6b7a989b1cdd4dd396665d2cfd2addc5.png', '$2a$10$o55UFZAtyWnDpRV6dvQe8.c/MjlFacC49ASj2usNXm9BY74SYI/uG', 1, '127.0.0.1', '2023-08-14 23:07:03', 1, null, '2022-05-21 08:30:54', 1, '2023-08-14 23:07:03', '管理员', 0);
 INSERT INTO  sys_user (user_id, post_id, role_id, dept_id, username, nickname, user_type, email, phone_number, sex, avatar, password, status, login_ip, login_date, is_admin, creator_id, create_time, updater_id, update_time, remark, deleted) VALUES (2, 2, 2, 5, 'ag1', 'valarchie2', 0, 'agileboot1@qq.com', '15666666666', 1, '/profile/avatar/20230725114818_avatar_b5bf400732bb43369b4df58802049b22.png', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', 1, '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员1', 0);
 INSERT INTO  sys_user (user_id, post_id, role_id, dept_id, username, nickname, user_type, email, phone_number, sex, avatar, password, status, login_ip, login_date, is_admin, creator_id, create_time, updater_id, update_time, remark, deleted) VALUES (3, 2, 0, 5, 'ag2', 'valarchie3', 0, 'agileboot2@qq.com', '15666666667', 1, '/profile/avatar/20230725114818_avatar_b5bf400732bb43369b4df58802049b22.png', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', 1, '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员2', 0);
-