From 3d5cee6e6888db860903a61eaeedd5081d990ce6 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 29 Sep 2025 19:41:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20mysql=20=E4=B9=B1=E7=A0=81=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/db/init/00_init_db_and_user.sql | 4 --- .../src/main/resources/db/init/01_schema.sql | 5 +--- .../main/resources/db/init/02_seed_data.sql | 22 ++++------------ docker/docker-compose.yaml | 25 +++++++------------ docker/mysql/conf.d/charset.cnf | 10 ++++++++ 5 files changed, 25 insertions(+), 41 deletions(-) create mode 100644 docker/mysql/conf.d/charset.cnf diff --git a/backend/src/main/resources/db/init/00_init_db_and_user.sql b/backend/src/main/resources/db/init/00_init_db_and_user.sql index bbdceed25..d8cf198ef 100644 --- a/backend/src/main/resources/db/init/00_init_db_and_user.sql +++ b/backend/src/main/resources/db/init/00_init_db_and_user.sql @@ -1,17 +1,13 @@ --- 统一字符集 SET NAMES utf8mb4; SET CHARACTER SET utf8mb4; SET collation_connection = utf8mb4_0900_ai_ci; --- 创建数据库(如果不存在) CREATE DATABASE IF NOT EXISTS `openisle` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; --- 创建业务账号(容器会基于环境变量自动创建;此处再兜底 + 幂等) CREATE USER IF NOT EXISTS 'openisle'@'%' IDENTIFIED BY 'openisle'; GRANT ALL PRIVILEGES ON `openisle`.* TO 'openisle'@'%'; FLUSH PRIVILEGES; --- 切换到目标库 USE `openisle`; diff --git a/backend/src/main/resources/db/init/01_schema.sql b/backend/src/main/resources/db/init/01_schema.sql index 0844e5799..87e1e22da 100644 --- a/backend/src/main/resources/db/init/01_schema.sql +++ b/backend/src/main/resources/db/init/01_schema.sql @@ -1,9 +1,8 @@ USE `openisle`; - SET NAMES utf8mb4; + SET FOREIGN_KEY_CHECKS = 0; --- users 表 CREATE TABLE IF NOT EXISTS `users` ( `id` bigint NOT NULL AUTO_INCREMENT, `approved` bit(1) DEFAULT NULL, @@ -26,7 +25,6 @@ CREATE TABLE IF NOT EXISTS `users` ( UNIQUE KEY `UK_users_username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; --- categories 表 CREATE TABLE IF NOT EXISTS `categories` ( `id` bigint NOT NULL AUTO_INCREMENT, `description` text, @@ -37,7 +35,6 @@ CREATE TABLE IF NOT EXISTS `categories` ( UNIQUE KEY `UK_categories_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; --- tags 表(依赖 users.id 外键) CREATE TABLE IF NOT EXISTS `tags` ( `id` bigint NOT NULL AUTO_INCREMENT, `approved` bit(1) DEFAULT NULL, diff --git a/backend/src/main/resources/db/init/02_seed_data.sql b/backend/src/main/resources/db/init/02_seed_data.sql index d217be9a7..091635304 100644 --- a/backend/src/main/resources/db/init/02_seed_data.sql +++ b/backend/src/main/resources/db/init/02_seed_data.sql @@ -1,33 +1,21 @@ USE `openisle`; +SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; --- 清空并灌入演示数据(幂等处理:先删再插) DELETE FROM `tags`; DELETE FROM `categories`; DELETE FROM `users`; --- users(密码已是 bcrypt;账号:admin/user1/user2,明文 123321) -INSERT INTO `users` -(`id`,`approved`,`avatar`,`created_at`,`display_medal`,`email`,`experience`,`introduction`, - `password`,`password_reset_code`,`point`,`register_reason`,`role`,`username`,`verification_code`,`verified`) -VALUES -(1, b'1', '', '2025-09-01 16:08:17.426430', 'PIONEER', 'adminmail@openisle.com', 70, NULL, - '$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe', NULL, 110, - '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试', 'ADMIN', 'admin', NULL, b'1'), -(2, b'1', '', '2025-09-03 16:08:17.426430', 'PIONEER', 'usermail2@openisle.com', 70, NULL, - '$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe', NULL, 110, - '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试', 'USER', 'user1', NULL, b'1'), -(3, b'1', '', '2025-09-02 17:21:21.617666', 'PIONEER', 'usermail1@openisle.com', 40, NULL, - '$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe', NULL, 40, - '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试', 'USER', 'user2', NULL, b'1'); +INSERT INTO `users` (`id`,`approved`,`avatar`,`created_at`,`display_medal`,`email`,`experience`,`introduction`,`password`,`password_reset_code`,`point`,`register_reason`,`role`,`username`,`verification_code`,`verified`) VALUES +(1,b'1','', '2025-09-01 16:08:17.426430','PIONEER','adminmail@openisle.com',70,NULL,'$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe',NULL,110,'测试测试测试……','ADMIN','admin',NULL,b'1'), +(2,b'1','', '2025-09-03 16:08:17.426430','PIONEER','usermail2@openisle.com',70,NULL,'$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe',NULL,110,'测试测试测试……','USER','user1',NULL,b'1'), +(3,b'1','', '2025-09-02 17:21:21.617666','PIONEER','usermail1@openisle.com',40,NULL,'$2a$10$dux.NXwW09cCsdZ05BgcnOtxVqqjcmnbj3.8xcxGl/iiIlv06y7Oe',NULL,40,'测试测试测试……','USER','user2',NULL,b'1'); --- categories INSERT INTO `categories` (`id`,`description`,`icon`,`name`,`small_icon`) VALUES (1,'测试用分类1','1','测试用分类1',NULL), (2,'测试用分类2','2','测试用分类2',NULL), (3,'测试用分类3','3','测试用分类3',NULL); --- tags(此处不绑定 creator_id,避免外键顺序依赖;如需可填 1/2/3) INSERT INTO `tags` (`id`,`approved`,`created_at`,`description`,`icon`,`name`,`small_icon`,`creator_id`) VALUES (1,b'1','2025-09-02 10:51:56.000000','测试用标签1',NULL,'测试用标签1',NULL,NULL), (2,b'1','2025-09-02 10:51:56.000000','测试用标签2',NULL,'测试用标签2',NULL,NULL), diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 36f2392b3..d762df085 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -6,34 +6,27 @@ services: restart: always env_file: - ../.env - ports: - - "${MYSQL_PORT:-3306}:3306" - volumes: - - mysql-data:/var/lib/mysql - - ../backend/src/main/resources/db/init:/docker-entrypoint-initdb.d command: > --character-set-server=utf8mb4 --collation-server=utf8mb4_0900_ai_ci --default-time-zone=+08:00 + --skip-character-set-client-handshake + ports: + - "${MYSQL_PORT:-3306}:3306" + volumes: + - mysql-data:/var/lib/mysql + - ../backend/src/main/resources/db/init:/docker-entrypoint-initdb.d:ro + - ./mysql/conf.d:/etc/mysql/conf.d:ro networks: - openisle-network healthcheck: - test: - [ - "CMD", - "mysqladmin", - "ping", - "-h", - "127.0.0.1", - "-u", - "root", - "-p$MYSQL_ROOT_PASSWORD", - ] + test: ["CMD","mysqladmin","ping","-h","127.0.0.1","-u","root","-p$MYSQL_ROOT_PASSWORD"] interval: 5s timeout: 3s retries: 30 start_period: 20s + # OpenSearch Service opensearch: build: diff --git a/docker/mysql/conf.d/charset.cnf b/docker/mysql/conf.d/charset.cnf new file mode 100644 index 000000000..2464cce02 --- /dev/null +++ b/docker/mysql/conf.d/charset.cnf @@ -0,0 +1,10 @@ +[mysqld] +character-set-server = utf8mb4 +collation-server = utf8mb4_0900_ai_ci +skip-character-set-client-handshake + +[client] +default-character-set = utf8mb4 + +[mysql] +default-character-set = utf8mb4