Database Query Skill
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
DB_HOST |
Yes | — | MySQL server hostname or IP |
DB_USER |
Yes | — | Database username |
DB_PASS |
Yes | — | Database password |
DB_NAME |
No | — | Default database name |
DB_PORT |
No | 3306 | MySQL server port |
Important
All required environment variables (DB_HOST, DB_USER, DB_PASS, etc.) are pre-configured. Do NOT ask the user for database credentials — just run the query directly.
Workflow
Verify connectivity before running queries:
mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" -e "SELECT 1" 2>&1Run queries using the mysql CLI with these flags for clean output:
mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --batch --skip-column-namesFor tabular output (human-readable), omit
--skip-column-names:mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --tableExport to CSV:
mysql -h "$DB_HOST" -P "${DB_PORT:-3306}" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "YOUR SQL" --batch | tr '\t' ',' > output.csv
Safety Rules
- NEVER run
DROP,TRUNCATE,DELETE, orUPDATEwithout explicit user confirmation - NEVER modify database schema (
ALTER TABLE,CREATE TABLE,DROP TABLE) without approval - Always use
LIMITfor exploratory queries to avoid fetching millions of rows - Use
--batchmode for programmatic output,--tablefor human display - Mask passwords in any output shown to the user — never echo
$DB_PASS
Query Patterns
- Explore schema:
SHOW TABLES,DESCRIBE table_name,SHOW CREATE TABLE table_name - Row counts:
SELECT COUNT(*) FROM table_name - Sample data:
SELECT * FROM table_name LIMIT 10 - Aggregations: Use
GROUP BYwithCOUNT,SUM,AVGfor summaries - Date filtering:
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)