Environment:666ghj BettaFish MySQL or PostgreSQL
| Knowledge Sources | |
|---|---|
| Domains | Database, Infrastructure |
| Last Updated | 2026-10-08 21:30 GMT |
Overview
Relational database backend (MySQL or PostgreSQL) for storing crawled social media data, sentiment analysis results, and MindSpider crawl state.
Description
This environment provides a relational database backend for BettaFish. The system supports both MySQL and PostgreSQL via SQLAlchemy 2.x async engines. The database stores crawled posts, comments, topics, and keyword distributions. The Docker Compose deployment includes a PostgreSQL 15 container by default. The database dialect is configurable via the `DB_DIALECT` setting (`mysql` or `postgresql`).
Usage
Use this environment when running InsightEngine database mining, MindSpider crawling, or any task that queries the crawled social media corpus. It is required by MediaCrawlerDB search, NewsCollector, KeywordManager, DatabaseManager, MindSpider configuration validation, and PlatformCrawler.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Any | Docker recommended for PostgreSQL |
| Hardware | 2GB+ RAM | Depends on data volume |
| Disk | 10GB+ SSD | Scales with crawled data volume |
| Network | Local or remote DB | Remote DB requires stable connection |
Dependencies
System Packages
- MySQL 5.7+ or PostgreSQL 15+ (if running without Docker)
Python Packages
- `sqlalchemy` == 2.0.35
- `pymysql` == 1.1.0 (MySQL sync driver)
- `aiomysql` == 0.2.0 (MySQL async driver)
- `asyncmy` == 0.2.10 (MySQL async driver, used by MindSpider)
- `asyncpg` == 0.29.0 (PostgreSQL async driver)
- `psycopg[binary]` >= 3.1.0 (PostgreSQL driver)
- `aiosqlite` == 0.21.0 (SQLite fallback)
- `cryptography` == 42.0.7
Credentials
The following environment variables must be set in `.env`:
- `DB_DIALECT`: Database type (`mysql` or `postgresql`; default: `postgresql` in global config, `mysql` in MindSpider)
- `DB_HOST`: Database hostname or IP
- `DB_PORT`: Database port (default: 5432 for PostgreSQL, 3306 for MySQL)
- `DB_USER`: Database username
- `DB_PASSWORD`: Database password
- `DB_NAME`: Database name
- `DB_CHARSET`: Character set (default: `utf8mb4`)
- `DATABASE_URL`: Optional full connection URL (overrides individual fields; special chars in password must be percent-encoded)
Quick Install
# Python packages
pip install sqlalchemy==2.0.35 pymysql==1.1.0 aiomysql==0.2.0 asyncpg==0.29.0 "psycopg[binary]>=3.1.0"
# Or use Docker for PostgreSQL
docker-compose up -d db
Code Evidence
Database URL construction with dialect selection from `InsightEngine/utils/db.py:48-90`:
def _build_database_url() -> str:
dialect: str = (settings.DB_DIALECT or "mysql").lower()
database_url_env = os.getenv("DATABASE_URL")
if database_url_env:
url_obj = _parse_database_url(database_url_env)
return url_obj.render_as_string(hide_password=False)
if dialect in ("postgresql", "postgres"):
url_obj = URL.create(
drivername="postgresql+asyncpg",
username=user, password=password,
host=host, port=port_int, database=db_name,
)
return url_obj.render_as_string(hide_password=False)
# Default MySQL
url_obj = URL.create(
drivername="mysql+aiomysql",
username=user, password=password,
host=host, port=port_int, database=db_name,
)
return url_obj.render_as_string(hide_password=False)
MindSpider config validation from `MindSpider/main.py:49-69`:
def check_config(self) -> bool:
required_configs = [
'DB_HOST', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DB_CHARSET',
'MINDSPIDER_API_KEY', 'MINDSPIDER_BASE_URL', 'MINDSPIDER_MODEL_NAME'
]
missing_configs = []
for config_name in required_configs:
if not hasattr(settings, config_name) or not getattr(settings, config_name):
missing_configs.append(config_name)
if missing_configs:
logger.error(f"配置缺失: {', '.join(missing_configs)}")
return False
return True
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `配置缺失: DB_HOST, DB_PASSWORD` | Missing `.env` entries | Add all `DB_*` variables to `.env` |
| `DATABASE_URL 不是合法的数据库连接 URL` | Unescaped `@` in password | Percent-encode special chars or use individual `DB_*` fields |
| `asyncpg.exceptions.ConnectionError` | PostgreSQL not reachable | Verify host, port, and firewall rules |
| `aiomysql.MySQLConnectionError` | MySQL not reachable | Verify MySQL is running and credentials are correct |
Compatibility Notes
- PostgreSQL: Uses `asyncpg` async driver. Default in Docker Compose with port mapping `${POSTGRES_PORT:-5444}:5432`.
- MySQL: Uses `aiomysql` (InsightEngine) or `asyncmy` (MindSpider) async driver. Default port 3306.
- DATABASE_URL override: If set, takes priority over individual `DB_*` fields. Special characters in userinfo must be percent-encoded.
- Docker: The `docker-compose.yml` maps `DB_USER`, `DB_PASSWORD`, `DB_NAME` to `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_DB` for container initialization.
Related Pages
- Implementation:666ghj_BettaFish_MediaCrawlerDB_search
- Implementation:666ghj_BettaFish_MindSpider_check_config
- Implementation:666ghj_BettaFish_DatabaseManager_show_statistics
- Implementation:666ghj_BettaFish_KeywordManager_get_all_keywords
- Implementation:666ghj_BettaFish_NewsCollector_collect_and_save
- Implementation:666ghj_BettaFish_PlatformCrawler_run_multi