fix: mysql in docker (#216)
* fix(database): Implement dynamic database connector selection * refactor: Convert database detection messages to English * chore: add mysql2 dependency * Update docker-compose.yml --------- Co-authored-by: Ou <[email protected]>
This commit is contained in:
@@ -14,7 +14,7 @@ services:
|
||||
- INIT_TABLE=true
|
||||
- ENABLE_CACHE=true
|
||||
# MySQL Database Configuration (uncomment to use MySQL instead of SQLite)
|
||||
# - MYSQL_HOST=mysql
|
||||
# - MYSQL_HOST=newsnow_mysql
|
||||
# - MYSQL_PORT=3306
|
||||
# - MYSQL_USER=newsnow
|
||||
# - MYSQL_PASSWORD=your_password
|
||||
|
||||
+6
-8
@@ -1,5 +1,3 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
newsnow:
|
||||
image: ghcr.io/ourongxing/newsnow:latest
|
||||
@@ -18,12 +16,12 @@ services:
|
||||
- INIT_TABLE=true
|
||||
- ENABLE_CACHE=true
|
||||
# MySQL Database Configuration (uncomment to use MySQL instead of SQLite)
|
||||
# - MYSQL_HOST=mysql
|
||||
# - MYSQL_PORT=3306
|
||||
# - MYSQL_USER=newsnow
|
||||
# - MYSQL_PASSWORD=your_password
|
||||
# - MYSQL_DATABASE=newsnow
|
||||
# - MYSQL_SSL=false
|
||||
- MYSQL_HOST=newsnow_mysql
|
||||
- MYSQL_PORT=3306
|
||||
- MYSQL_USER=newsnow
|
||||
- MYSQL_PASSWORD=root_password
|
||||
- MYSQL_DATABASE=newsnow
|
||||
- MYSQL_SSL=false
|
||||
# Uncomment the following lines to use MySQL database
|
||||
# depends_on:
|
||||
# - mysql
|
||||
|
||||
+37
-10
@@ -4,6 +4,41 @@ import viteNitro from "vite-plugin-with-nitro"
|
||||
import { RollopGlob } from "./tools/rollup-glob"
|
||||
import { projectDir } from "./shared/dir"
|
||||
|
||||
// 检测是否配置了 MySQL
|
||||
function isMySQLConfigured(): boolean {
|
||||
return !!(process.env.MYSQL_HOST &&
|
||||
process.env.MYSQL_USER &&
|
||||
process.env.MYSQL_PASSWORD &&
|
||||
process.env.MYSQL_DATABASE)
|
||||
}
|
||||
|
||||
// 根据环境变量动态配置数据库
|
||||
function getDatabaseConfig() {
|
||||
if (isMySQLConfigured()) {
|
||||
console.log('🔗 Using MySQL connector')
|
||||
return {
|
||||
default: {
|
||||
connector: "mysql2",
|
||||
options: {
|
||||
host: process.env.MYSQL_HOST,
|
||||
port: parseInt(process.env.MYSQL_PORT || "3306"),
|
||||
user: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE,
|
||||
ssl: process.env.MYSQL_SSL === "true" ? {} : false,
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
console.log('🗃️ Using SQLite connector')
|
||||
return {
|
||||
default: {
|
||||
connector: "better-sqlite3",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const nitroOption: Parameters<typeof viteNitro>[0] = {
|
||||
experimental: {
|
||||
database: true,
|
||||
@@ -12,16 +47,8 @@ const nitroOption: Parameters<typeof viteNitro>[0] = {
|
||||
plugins: [RollopGlob()],
|
||||
},
|
||||
sourceMap: false,
|
||||
database: {
|
||||
default: {
|
||||
connector: "better-sqlite3",
|
||||
},
|
||||
},
|
||||
devDatabase: {
|
||||
default: {
|
||||
connector: "better-sqlite3",
|
||||
},
|
||||
},
|
||||
database: getDatabaseConfig(),
|
||||
devDatabase: getDatabaseConfig(),
|
||||
imports: {
|
||||
dirs: ["server/utils", "shared"],
|
||||
},
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
"jose": "^6.0.8",
|
||||
"jotai": "^2.12.1",
|
||||
"md5": "^2.3.0",
|
||||
"mysql2": "^3.11.4",
|
||||
"ofetch": "^1.4.1",
|
||||
"overlayscrollbars": "^2.11.1",
|
||||
"pnpm": "^10.5.2",
|
||||
|
||||
@@ -11,10 +11,10 @@ function isMySQLDatabase(): boolean {
|
||||
process.env.MYSQL_DATABASE
|
||||
|
||||
if (hasMySQLConfig) {
|
||||
console.log('🔍 检测到 MySQL 配置,使用 MySQL 数据库')
|
||||
console.log('🔗 Using MySQL database')
|
||||
return true
|
||||
} else {
|
||||
console.log('🔍 未检测到 MySQL 配置,使用 SQLite 数据库')
|
||||
console.log('🗃️ Using SQLite database')
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user