cosmoguard-bd/utils/README.md

153 lines
3.3 KiB
Markdown

# PIF Compiler - MongoDB Docker Setup
## Quick Start
Start MongoDB and Mongo Express web interface:
```bash
cd utils
docker-compose up -d
```
Stop the services:
```bash
docker-compose down
```
Stop and remove all data:
```bash
docker-compose down -v
```
## Services
### MongoDB
- **Port**: 27017
- **Database**: toxinfo
- **Username**: admin
- **Password**: admin123
- **Connection String**: `mongodb://admin:admin123@localhost:27017/toxinfo?authSource=admin`
### Mongo Express (Web UI)
- **URL**: http://localhost:8082
- **Username**: admin
- **Password**: admin123
## Usage in Python
Update your MongoDB connection in `src/pif_compiler/functions/mongo_functions.py`:
```python
# For local development with Docker
db = connect(user="admin", password="admin123", database="toxinfo")
```
Or use the connection URI directly:
```python
from pymongo import MongoClient
client = MongoClient("mongodb://admin:admin123@localhost:27017/toxinfo?authSource=admin")
db = client['toxinfo']
```
## Data Persistence
Data is persisted in Docker volumes:
- `mongodb_data` - Database files
- `mongodb_config` - Configuration files
These volumes persist even when containers are stopped.
## Creating Application User
It's recommended to create a dedicated user for your application instead of using the admin account.
### Option 1: Using mongosh (MongoDB Shell)
```bash
# Access MongoDB shell
docker exec -it pif_mongodb mongosh -u admin -p admin123 --authenticationDatabase admin
# In the MongoDB shell, run:
use toxinfo
db.createUser({
user: "pif_app",
pwd: "pif_app_password",
roles: [
{ role: "readWrite", db: "toxinfo" }
]
})
# Exit the shell
exit
```
### Option 2: Using Mongo Express Web UI
1. Go to http://localhost:8082
2. Login with admin/admin123
3. Select `toxinfo` database
4. Click on "Users" tab
5. Add new user with `readWrite` role
### Option 3: Using Python Script
Create a file `utils/create_user.py`:
```python
from pymongo import MongoClient
# Connect as admin
client = MongoClient("mongodb://admin:admin123@localhost:27017/?authSource=admin")
db = client['toxinfo']
# Create application user
db.command("createUser", "pif_app",
pwd="pif_app_password",
roles=[{"role": "readWrite", "db": "toxinfo"}])
print("User 'pif_app' created successfully!")
client.close()
```
Run it:
```bash
cd utils
uv run python create_user.py
```
### Update Your Application
After creating the user, update your connection in `src/pif_compiler/functions/mongo_functions.py`:
```python
# Use application user instead of admin
db = connect(user="pif_app", password="pif_app_password", database="toxinfo")
```
Or with connection URI:
```python
client = MongoClient("mongodb://pif_app:pif_app_password@localhost:27017/toxinfo?authSource=toxinfo")
```
### Available Roles
- `read`: Read-only access to the database
- `readWrite`: Read and write access (recommended for your app)
- `dbAdmin`: Database administration (create indexes, etc.)
- `userAdmin`: Manage users and roles
## Security Notes
⚠️ **WARNING**: The default credentials are for local development only.
For production:
1. Change all passwords in `docker-compose.yml`
2. Use environment variables or secrets management
3. Create dedicated users with minimal required permissions
4. Configure firewall rules
5. Enable SSL/TLS connections