@cmd233/mcp-database-server 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md.en.bak +328 -0
- package/dist/src/db/adapter.js +3 -3
- package/dist/src/db/index.js +23 -23
- package/dist/src/db/mysql-adapter.js +17 -17
- package/dist/src/db/postgresql-adapter.js +24 -24
- package/dist/src/db/sqlite-adapter.js +19 -19
- package/dist/src/db/sqlserver-adapter.js +187 -86
- package/dist/src/handlers/resourceHandlers.js +8 -8
- package/dist/src/handlers/toolHandlers.js +7 -7
- package/dist/src/index.js +25 -25
- package/dist/src/tools/insightTools.js +9 -9
- package/dist/src/tools/queryTools.js +10 -10
- package/dist/src/tools/schemaTools.js +15 -15
- package/dist/src/utils/formatUtils.js +14 -14
- package/package.json +3 -2
- package/readme.md +100 -100
package/README.md.en.bak
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
[](https://mseep.ai/app/executeautomation-mcp-database-server)
|
|
2
|
+
|
|
3
|
+
# MCP Database Server
|
|
4
|
+
|
|
5
|
+
This MCP (Model Context Protocol) server provides database access capabilities to Claude, supporting SQLite, SQL Server, PostgreSQL, and MySQL databases.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
1. Clone the repository:
|
|
10
|
+
```
|
|
11
|
+
git clone https://github.com/executeautomation/mcp-database-server.git
|
|
12
|
+
cd mcp-database-server
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
2. Install dependencies:
|
|
16
|
+
```
|
|
17
|
+
npm install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
3. Build the project:
|
|
21
|
+
```
|
|
22
|
+
npm run build
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage Options
|
|
26
|
+
|
|
27
|
+
There are two ways to use this MCP server with Claude:
|
|
28
|
+
|
|
29
|
+
1. **Direct usage**: Install the package globally and use it directly
|
|
30
|
+
2. **Local development**: Run from your local development environment
|
|
31
|
+
|
|
32
|
+
### Direct Usage with NPM Package
|
|
33
|
+
|
|
34
|
+
The easiest way to use this MCP server is by installing it globally:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install -g @executeautomation/database-server
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
This allows you to use the server directly without building it locally.
|
|
41
|
+
|
|
42
|
+
### Local Development Setup
|
|
43
|
+
|
|
44
|
+
If you want to modify the code or run from your local environment:
|
|
45
|
+
|
|
46
|
+
1. Clone and build the repository as shown in the Installation section
|
|
47
|
+
2. Run the server using the commands in the Usage section below
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
### SQLite Database
|
|
52
|
+
|
|
53
|
+
To use with an SQLite database:
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
node dist/src/index.js /path/to/your/database.db
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### SQL Server Database
|
|
60
|
+
|
|
61
|
+
To use with a SQL Server database:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
node dist/src/index.js --sqlserver --server <server-name> --database <database-name> [--user <username> --password <password>]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Required parameters:
|
|
68
|
+
- `--server`: SQL Server host name or IP address
|
|
69
|
+
- `--database`: Name of the database
|
|
70
|
+
|
|
71
|
+
Optional parameters:
|
|
72
|
+
- `--user`: Username for SQL Server authentication (if not provided, Windows Authentication will be used)
|
|
73
|
+
- `--password`: Password for SQL Server authentication
|
|
74
|
+
- `--port`: Port number (default: 1433)
|
|
75
|
+
|
|
76
|
+
### PostgreSQL Database
|
|
77
|
+
|
|
78
|
+
To use with a PostgreSQL database:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
node dist/src/index.js --postgresql --host <host-name> --database <database-name> [--user <username> --password <password>]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Required parameters:
|
|
85
|
+
- `--host`: PostgreSQL host name or IP address
|
|
86
|
+
- `--database`: Name of the database
|
|
87
|
+
|
|
88
|
+
Optional parameters:
|
|
89
|
+
- `--user`: Username for PostgreSQL authentication
|
|
90
|
+
- `--password`: Password for PostgreSQL authentication
|
|
91
|
+
- `--port`: Port number (default: 5432)
|
|
92
|
+
- `--ssl`: Enable SSL connection (true/false)
|
|
93
|
+
- `--connection-timeout`: Connection timeout in milliseconds (default: 30000)
|
|
94
|
+
|
|
95
|
+
### MySQL Database
|
|
96
|
+
|
|
97
|
+
#### Standard Authentication
|
|
98
|
+
|
|
99
|
+
To use with a MySQL database:
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
node dist/src/index.js --mysql --host <host-name> --database <database-name> --port <port> [--user <username> --password <password>]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Required parameters:
|
|
106
|
+
- `--host`: MySQL host name or IP address
|
|
107
|
+
- `--database`: Name of the database
|
|
108
|
+
- `--port`: Port number (default: 3306)
|
|
109
|
+
|
|
110
|
+
Optional parameters:
|
|
111
|
+
- `--user`: Username for MySQL authentication
|
|
112
|
+
- `--password`: Password for MySQL authentication
|
|
113
|
+
- `--ssl`: Enable SSL connection (true/false or object)
|
|
114
|
+
- `--connection-timeout`: Connection timeout in milliseconds (default: 30000)
|
|
115
|
+
|
|
116
|
+
#### AWS IAM Authentication
|
|
117
|
+
|
|
118
|
+
For Amazon RDS MySQL instances with IAM database authentication:
|
|
119
|
+
|
|
120
|
+
**Prerequisites:**
|
|
121
|
+
- AWS credentials must be configured (the RDS Signer uses the default credential provider chain)
|
|
122
|
+
- Configure using one of these methods:
|
|
123
|
+
- `aws configure` (uses default profile)
|
|
124
|
+
- `AWS_PROFILE=myprofile` environment variable
|
|
125
|
+
- `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables
|
|
126
|
+
- IAM roles (if running on EC2)
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
node dist/src/index.js --mysql --aws-iam-auth --host <rds-endpoint> --database <database-name> --user <aws-username> --aws-region <region>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Required parameters:
|
|
133
|
+
- `--host`: RDS endpoint hostname
|
|
134
|
+
- `--database`: Name of the database
|
|
135
|
+
- `--aws-iam-auth`: Enable AWS IAM authentication
|
|
136
|
+
- `--user`: AWS IAM username (also the database user)
|
|
137
|
+
- `--aws-region`: AWS region where RDS instance is located
|
|
138
|
+
|
|
139
|
+
Note: SSL is automatically enabled for AWS IAM authentication
|
|
140
|
+
|
|
141
|
+
## Configuring Claude Desktop
|
|
142
|
+
|
|
143
|
+
### Direct Usage Configuration
|
|
144
|
+
|
|
145
|
+
If you installed the package globally, configure Claude Desktop with:
|
|
146
|
+
|
|
147
|
+
```json
|
|
148
|
+
{
|
|
149
|
+
"mcpServers": {
|
|
150
|
+
"sqlite": {
|
|
151
|
+
"command": "npx",
|
|
152
|
+
"args": [
|
|
153
|
+
"-y",
|
|
154
|
+
"@executeautomation/database-server",
|
|
155
|
+
"/path/to/your/database.db"
|
|
156
|
+
]
|
|
157
|
+
},
|
|
158
|
+
"sqlserver": {
|
|
159
|
+
"command": "npx",
|
|
160
|
+
"args": [
|
|
161
|
+
"-y",
|
|
162
|
+
"@executeautomation/database-server",
|
|
163
|
+
"--sqlserver",
|
|
164
|
+
"--server", "your-server-name",
|
|
165
|
+
"--database", "your-database-name",
|
|
166
|
+
"--user", "your-username",
|
|
167
|
+
"--password", "your-password"
|
|
168
|
+
]
|
|
169
|
+
},
|
|
170
|
+
"postgresql": {
|
|
171
|
+
"command": "npx",
|
|
172
|
+
"args": [
|
|
173
|
+
"-y",
|
|
174
|
+
"@executeautomation/database-server",
|
|
175
|
+
"--postgresql",
|
|
176
|
+
"--host", "your-host-name",
|
|
177
|
+
"--database", "your-database-name",
|
|
178
|
+
"--user", "your-username",
|
|
179
|
+
"--password", "your-password"
|
|
180
|
+
]
|
|
181
|
+
},
|
|
182
|
+
"mysql": {
|
|
183
|
+
"command": "npx",
|
|
184
|
+
"args": [
|
|
185
|
+
"-y",
|
|
186
|
+
"@executeautomation/database-server",
|
|
187
|
+
"--mysql",
|
|
188
|
+
"--host", "your-host-name",
|
|
189
|
+
"--database", "your-database-name",
|
|
190
|
+
"--port", "3306",
|
|
191
|
+
"--user", "your-username",
|
|
192
|
+
"--password", "your-password"
|
|
193
|
+
]
|
|
194
|
+
},
|
|
195
|
+
"mysql-aws": {
|
|
196
|
+
"command": "npx",
|
|
197
|
+
"args": [
|
|
198
|
+
"-y",
|
|
199
|
+
"@executeautomation/database-server",
|
|
200
|
+
"--mysql",
|
|
201
|
+
"--aws-iam-auth",
|
|
202
|
+
"--host", "your-rds-endpoint.region.rds.amazonaws.com",
|
|
203
|
+
"--database", "your-database-name",
|
|
204
|
+
"--user", "your-aws-username",
|
|
205
|
+
"--aws-region", "us-east-1"
|
|
206
|
+
]
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Local Development Configuration
|
|
213
|
+
|
|
214
|
+
For local development, configure Claude Desktop to use your locally built version:
|
|
215
|
+
|
|
216
|
+
```json
|
|
217
|
+
{
|
|
218
|
+
"mcpServers": {
|
|
219
|
+
"sqlite": {
|
|
220
|
+
"command": "node",
|
|
221
|
+
"args": [
|
|
222
|
+
"/absolute/path/to/mcp-database-server/dist/src/index.js",
|
|
223
|
+
"/path/to/your/database.db"
|
|
224
|
+
]
|
|
225
|
+
},
|
|
226
|
+
"sqlserver": {
|
|
227
|
+
"command": "node",
|
|
228
|
+
"args": [
|
|
229
|
+
"/absolute/path/to/mcp-database-server/dist/src/index.js",
|
|
230
|
+
"--sqlserver",
|
|
231
|
+
"--server", "your-server-name",
|
|
232
|
+
"--database", "your-database-name",
|
|
233
|
+
"--user", "your-username",
|
|
234
|
+
"--password", "your-password"
|
|
235
|
+
]
|
|
236
|
+
},
|
|
237
|
+
"postgresql": {
|
|
238
|
+
"command": "node",
|
|
239
|
+
"args": [
|
|
240
|
+
"/absolute/path/to/mcp-database-server/dist/src/index.js",
|
|
241
|
+
"--postgresql",
|
|
242
|
+
"--host", "your-host-name",
|
|
243
|
+
"--database", "your-database-name",
|
|
244
|
+
"--user", "your-username",
|
|
245
|
+
"--password", "your-password"
|
|
246
|
+
]
|
|
247
|
+
},
|
|
248
|
+
"mysql": {
|
|
249
|
+
"command": "node",
|
|
250
|
+
"args": [
|
|
251
|
+
"/absolute/path/to/mcp-database-server/dist/src/index.js",
|
|
252
|
+
"--mysql",
|
|
253
|
+
"--host", "your-host-name",
|
|
254
|
+
"--database", "your-database-name",
|
|
255
|
+
"--port", "3306",
|
|
256
|
+
"--user", "your-username",
|
|
257
|
+
"--password", "your-password"
|
|
258
|
+
]
|
|
259
|
+
},
|
|
260
|
+
"mysql-aws": {
|
|
261
|
+
"command": "node",
|
|
262
|
+
"args": [
|
|
263
|
+
"/absolute/path/to/mcp-database-server/dist/src/index.js",
|
|
264
|
+
"--mysql",
|
|
265
|
+
"--aws-iam-auth",
|
|
266
|
+
"--host", "your-rds-endpoint.region.rds.amazonaws.com",
|
|
267
|
+
"--database", "your-database-name",
|
|
268
|
+
"--user", "your-aws-username",
|
|
269
|
+
"--aws-region", "us-east-1"
|
|
270
|
+
]
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
The Claude Desktop configuration file is typically located at:
|
|
277
|
+
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
278
|
+
- Windows: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
279
|
+
- Linux: `~/.config/Claude/claude_desktop_config.json`
|
|
280
|
+
|
|
281
|
+
## Available Database Tools
|
|
282
|
+
|
|
283
|
+
The MCP Database Server provides the following tools that Claude can use:
|
|
284
|
+
|
|
285
|
+
| Tool | Description | Required Parameters |
|
|
286
|
+
|------|-------------|---------------------|
|
|
287
|
+
| `read_query` | Execute SELECT queries to read data | `query`: SQL SELECT statement |
|
|
288
|
+
| `write_query` | Execute INSERT, UPDATE, or DELETE queries | `query`: SQL modification statement |
|
|
289
|
+
| `create_table` | Create new tables in the database | `query`: CREATE TABLE statement |
|
|
290
|
+
| `alter_table` | Modify existing table schema | `query`: ALTER TABLE statement |
|
|
291
|
+
| `drop_table` | Remove a table from the database | `table_name`: Name of table<br>`confirm`: Safety flag (must be true) |
|
|
292
|
+
| `list_tables` | Get a list of all tables | None |
|
|
293
|
+
| `describe_table` | View schema information for a table | `table_name`: Name of table |
|
|
294
|
+
| `export_query` | Export query results as CSV/JSON | `query`: SQL SELECT statement<br>`format`: "csv" or "json" |
|
|
295
|
+
| `append_insight` | Add a business insight to memo | `insight`: Text of insight |
|
|
296
|
+
| `list_insights` | List all business insights | None |
|
|
297
|
+
|
|
298
|
+
For practical examples of how to use these tools with Claude, see [Usage Examples](docs/usage-examples.md).
|
|
299
|
+
|
|
300
|
+
## Additional Documentation
|
|
301
|
+
|
|
302
|
+
- [SQL Server Setup Guide](docs/sql-server-setup.md): Details on connecting to SQL Server databases
|
|
303
|
+
- [PostgreSQL Setup Guide](docs/postgresql-setup.md): Details on connecting to PostgreSQL databases
|
|
304
|
+
- [Usage Examples](docs/usage-examples.md): Example queries and commands to use with Claude
|
|
305
|
+
|
|
306
|
+
## Development
|
|
307
|
+
|
|
308
|
+
To run the server in development mode:
|
|
309
|
+
|
|
310
|
+
```
|
|
311
|
+
npm run dev
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
To watch for changes during development:
|
|
315
|
+
|
|
316
|
+
```
|
|
317
|
+
npm run watch
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Requirements
|
|
321
|
+
|
|
322
|
+
- Node.js 18+
|
|
323
|
+
- For SQL Server connectivity: SQL Server 2012 or later
|
|
324
|
+
- For PostgreSQL connectivity: PostgreSQL 9.5 or later
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT
|
package/dist/src/db/adapter.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 使用动态导入适配器
|
|
2
2
|
import { SqliteAdapter } from './sqlite-adapter.js';
|
|
3
3
|
import { SqlServerAdapter } from './sqlserver-adapter.js';
|
|
4
4
|
import { PostgresqlAdapter } from './postgresql-adapter.js';
|
|
5
5
|
import { MysqlAdapter } from './mysql-adapter.js';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* 工厂函数,创建相应的数据库适配器
|
|
8
8
|
*/
|
|
9
9
|
export function createDbAdapter(type, connectionInfo) {
|
|
10
10
|
switch (type.toLowerCase()) {
|
|
11
11
|
case 'sqlite':
|
|
12
|
-
//
|
|
12
|
+
// 对于 SQLite,如果 connectionInfo 是字符串,则直接将其用作路径
|
|
13
13
|
if (typeof connectionInfo === 'string') {
|
|
14
14
|
return new SqliteAdapter(connectionInfo);
|
|
15
15
|
}
|
package/dist/src/db/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { createDbAdapter } from './adapter.js';
|
|
2
|
-
//
|
|
2
|
+
// 存储活动的数据库适配器
|
|
3
3
|
let dbAdapter = null;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param connectionInfo
|
|
7
|
-
* @param dbType
|
|
5
|
+
* 初始化数据库连接
|
|
6
|
+
* @param connectionInfo 连接信息对象或 SQLite 路径字符串
|
|
7
|
+
* @param dbType 数据库类型 ('sqlite' 或 'sqlserver')
|
|
8
8
|
*/
|
|
9
9
|
export async function initDatabase(connectionInfo, dbType = 'sqlite') {
|
|
10
10
|
try {
|
|
11
|
-
//
|
|
11
|
+
// 如果 connectionInfo 是字符串,则假定它是 SQLite 路径
|
|
12
12
|
if (typeof connectionInfo === 'string') {
|
|
13
13
|
connectionInfo = { path: connectionInfo };
|
|
14
14
|
}
|
|
15
|
-
//
|
|
15
|
+
// 根据数据库类型创建相应的适配器
|
|
16
16
|
dbAdapter = createDbAdapter(dbType, connectionInfo);
|
|
17
|
-
//
|
|
17
|
+
// 初始化连接
|
|
18
18
|
await dbAdapter.init();
|
|
19
19
|
}
|
|
20
20
|
catch (error) {
|
|
@@ -22,10 +22,10 @@ export async function initDatabase(connectionInfo, dbType = 'sqlite') {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @param query SQL
|
|
27
|
-
* @param params
|
|
28
|
-
* @returns Promise
|
|
25
|
+
* 执行 SQL 查询并获取所有结果
|
|
26
|
+
* @param query 要执行的 SQL 查询
|
|
27
|
+
* @param params 查询参数
|
|
28
|
+
* @returns 包含查询结果的 Promise
|
|
29
29
|
*/
|
|
30
30
|
export function dbAll(query, params = []) {
|
|
31
31
|
if (!dbAdapter) {
|
|
@@ -34,10 +34,10 @@ export function dbAll(query, params = []) {
|
|
|
34
34
|
return dbAdapter.all(query, params);
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param query SQL
|
|
39
|
-
* @param params
|
|
40
|
-
* @returns Promise
|
|
37
|
+
* 执行修改数据的 SQL 查询
|
|
38
|
+
* @param query 要执行的 SQL 查询
|
|
39
|
+
* @param params 查询参数
|
|
40
|
+
* @returns 包含结果信息的 Promise
|
|
41
41
|
*/
|
|
42
42
|
export function dbRun(query, params = []) {
|
|
43
43
|
if (!dbAdapter) {
|
|
@@ -46,9 +46,9 @@ export function dbRun(query, params = []) {
|
|
|
46
46
|
return dbAdapter.run(query, params);
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @param query SQL
|
|
51
|
-
* @returns Promise
|
|
49
|
+
* 执行多条 SQL 语句
|
|
50
|
+
* @param query 要执行的 SQL 语句
|
|
51
|
+
* @returns 执行完成后解析的 Promise
|
|
52
52
|
*/
|
|
53
53
|
export function dbExec(query) {
|
|
54
54
|
if (!dbAdapter) {
|
|
@@ -57,7 +57,7 @@ export function dbExec(query) {
|
|
|
57
57
|
return dbAdapter.exec(query);
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
60
|
+
* 关闭数据库连接
|
|
61
61
|
*/
|
|
62
62
|
export function closeDatabase() {
|
|
63
63
|
if (!dbAdapter) {
|
|
@@ -66,7 +66,7 @@ export function closeDatabase() {
|
|
|
66
66
|
return dbAdapter.close();
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
69
|
+
* 获取数据库元数据
|
|
70
70
|
*/
|
|
71
71
|
export function getDatabaseMetadata() {
|
|
72
72
|
if (!dbAdapter) {
|
|
@@ -75,7 +75,7 @@ export function getDatabaseMetadata() {
|
|
|
75
75
|
return dbAdapter.getMetadata();
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
78
|
+
* 获取列出表的数据库特定查询
|
|
79
79
|
*/
|
|
80
80
|
export function getListTablesQuery() {
|
|
81
81
|
if (!dbAdapter) {
|
|
@@ -84,8 +84,8 @@ export function getListTablesQuery() {
|
|
|
84
84
|
return dbAdapter.getListTablesQuery();
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
88
|
-
* @param tableName
|
|
87
|
+
* 获取描述表的数据库特定查询
|
|
88
|
+
* @param tableName 表名
|
|
89
89
|
*/
|
|
90
90
|
export function getDescribeTableQuery(tableName) {
|
|
91
91
|
if (!dbAdapter) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import mysql from "mysql2/promise";
|
|
2
2
|
import { Signer } from "@aws-sdk/rds-signer";
|
|
3
3
|
/**
|
|
4
|
-
* MySQL
|
|
4
|
+
* MySQL 数据库适配器实现
|
|
5
5
|
*/
|
|
6
6
|
export class MysqlAdapter {
|
|
7
7
|
constructor(connectionInfo) {
|
|
@@ -23,17 +23,17 @@ export class MysqlAdapter {
|
|
|
23
23
|
this.config.ssl = connectionInfo.ssl;
|
|
24
24
|
}
|
|
25
25
|
else if (connectionInfo.ssl === true) {
|
|
26
|
-
//
|
|
26
|
+
// 对于 AWS IAM 认证,为 RDS 适当配置 SSL
|
|
27
27
|
if (this.awsIamAuth) {
|
|
28
28
|
this.config.ssl = {
|
|
29
|
-
rejectUnauthorized: false // AWS RDS
|
|
29
|
+
rejectUnauthorized: false // AWS RDS 处理证书验证
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
else {
|
|
33
33
|
this.config.ssl = {};
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
//
|
|
36
|
+
// 验证端口号
|
|
37
37
|
if (connectionInfo.port && typeof connectionInfo.port !== 'number') {
|
|
38
38
|
const parsedPort = parseInt(connectionInfo.port, 10);
|
|
39
39
|
if (isNaN(parsedPort)) {
|
|
@@ -41,11 +41,11 @@ export class MysqlAdapter {
|
|
|
41
41
|
}
|
|
42
42
|
this.config.port = parsedPort;
|
|
43
43
|
}
|
|
44
|
-
//
|
|
44
|
+
// 记录端口用于调试
|
|
45
45
|
console.error(`[DEBUG] MySQL connection will use port: ${this.config.port}`);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* 生成 AWS RDS 认证令牌
|
|
49
49
|
*/
|
|
50
50
|
async generateAwsAuthToken() {
|
|
51
51
|
if (!this.awsRegion) {
|
|
@@ -72,17 +72,17 @@ export class MysqlAdapter {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
*
|
|
75
|
+
* 初始化 MySQL 连接
|
|
76
76
|
*/
|
|
77
77
|
async init() {
|
|
78
78
|
try {
|
|
79
79
|
console.info(`[INFO] Connecting to MySQL: ${this.host}, Database: ${this.database}`);
|
|
80
|
-
//
|
|
80
|
+
// 处理 AWS IAM 认证
|
|
81
81
|
if (this.awsIamAuth) {
|
|
82
82
|
console.info(`[INFO] Using AWS IAM authentication for user: ${this.config.user}`);
|
|
83
83
|
try {
|
|
84
84
|
const authToken = await this.generateAwsAuthToken();
|
|
85
|
-
//
|
|
85
|
+
// 使用生成的令牌作为密码创建新配置
|
|
86
86
|
const awsConfig = {
|
|
87
87
|
...this.config,
|
|
88
88
|
password: authToken
|
|
@@ -110,7 +110,7 @@ export class MysqlAdapter {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
113
|
+
* 执行 SQL 查询并获取所有结果
|
|
114
114
|
*/
|
|
115
115
|
async all(query, params = []) {
|
|
116
116
|
if (!this.connection) {
|
|
@@ -125,7 +125,7 @@ export class MysqlAdapter {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
128
|
-
*
|
|
128
|
+
* 执行修改数据的 SQL 查询
|
|
129
129
|
*/
|
|
130
130
|
async run(query, params = []) {
|
|
131
131
|
if (!this.connection) {
|
|
@@ -142,7 +142,7 @@ export class MysqlAdapter {
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
145
|
+
* 执行多条 SQL 语句
|
|
146
146
|
*/
|
|
147
147
|
async exec(query) {
|
|
148
148
|
if (!this.connection) {
|
|
@@ -156,7 +156,7 @@ export class MysqlAdapter {
|
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
/**
|
|
159
|
-
*
|
|
159
|
+
* 关闭数据库连接
|
|
160
160
|
*/
|
|
161
161
|
async close() {
|
|
162
162
|
if (this.connection) {
|
|
@@ -165,7 +165,7 @@ export class MysqlAdapter {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
/**
|
|
168
|
-
*
|
|
168
|
+
* 获取数据库元数据
|
|
169
169
|
*/
|
|
170
170
|
getMetadata() {
|
|
171
171
|
return {
|
|
@@ -176,16 +176,16 @@ export class MysqlAdapter {
|
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
178
|
/**
|
|
179
|
-
*
|
|
179
|
+
* 获取列出表的数据库特定查询
|
|
180
180
|
*/
|
|
181
181
|
getListTablesQuery() {
|
|
182
182
|
return `SELECT table_name AS name FROM information_schema.tables WHERE table_schema = '${this.database}'`;
|
|
183
183
|
}
|
|
184
184
|
/**
|
|
185
|
-
*
|
|
185
|
+
* 获取描述表的数据库特定查询
|
|
186
186
|
*/
|
|
187
187
|
getDescribeTableQuery(tableName) {
|
|
188
|
-
// MySQL DESCRIBE
|
|
188
|
+
// MySQL DESCRIBE 返回具有不同名称的列,因此我们使用与预期格式匹配的查询
|
|
189
189
|
return `
|
|
190
190
|
SELECT
|
|
191
191
|
COLUMN_NAME as name,
|