📙 MongoDB Setup Guide for BambiSleep.Chat
Table of Contents
- Introduction
- Prerequisites
- Installation
- Configuration
- Database Setup
- Connection Testing
- Backup and Restore
- Troubleshooting
- Advanced Topics
Introduction
BambiSleep.Chat uses MongoDB for storing user profiles, session data, chat histories, triggers, and other application data. This guide provides detailed instructions for setting up and configuring MongoDB for use with BambiSleep.Chat.
Prerequisites
Before installing MongoDB, ensure your system meets these requirements:
- 64-bit operating system (Windows 10+, Ubuntu 20.04+, macOS 12+)
- Minimum 4GB RAM (8GB+ recommended)
- At least 10GB free disk space
- Internet connection for downloading packages
- Administrative/root access to the system
Installation
Linux Installation
Ubuntu/Debian-based Systems
Import the MongoDB GPG key:
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
Add MongoDB repository:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Update and install MongoDB:
sudo apt-get update sudo apt-get install -y mongodb-org
Start and enable MongoDB service:
sudo systemctl start mongod sudo systemctl enable mongod
Verify installation:
mongod --version
RHEL/CentOS/Fedora
Create a repository file:
sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo<<EOF [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc EOF
Install MongoDB:
sudo yum install -y mongodb-org
Start and enable service:
sudo systemctl start mongod sudo systemctl enable mongod
Verify installation:
mongod --version
Windows Installation
Download the MongoDB Community Server installer from the official MongoDB website.
Run the installer:
- Select "Complete" installation
- Choose "Run service as Network Service user"
- Install MongoDB Compass (optional but recommended)
Verify installation:
- Open Command Prompt and run:
"C:\Program Files\MongoDB\Server\6.0\bin\mongo.exe" --version
MongoDB service should start automatically. If not:
net start MongoDB
macOS Installation
Install using Homebrew (recommended):
brew tap mongodb/brew brew install mongodb-community
Start MongoDB service:
brew services start mongodb-community
Verify installation:
mongod --version
Docker Installation
Pull the official MongoDB image:
docker pull mongo:6.0
Create a directory for MongoDB data:
mkdir -p ~/mongodb/data
Start MongoDB container:
docker run -d --name bambisleep-mongodb -p 27017:27017 \ -v ~/mongodb/data:/data/db \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=your_secure_password \ mongo:6.0
Verify container is running:
docker ps | grep bambisleep-mongodb
Configuration
Basic Configuration
The default MongoDB configuration works for development, but for production, edit the MongoDB configuration file:
- Linux:
/etc/mongod.conf
- Windows:
C:\Program Files\MongoDB\Server\6.0\bin\mongod.cfg
- macOS (Homebrew):
/usr/local/etc/mongod.conf
Basic configuration example:
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
net:
port: 27017
bindIp: 127.0.0.1
processManagement:
timeZoneInfo: /usr/share/zoneinfo
After changing the configuration, restart MongoDB:
sudo systemctl restart mongod
Security Configuration
For production environments, enable security features:
Create an admin user:
use admin db.createUser({ user: "admin", pwd: "secure_password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
Enable authentication in
mongod.conf
:security: authorization: enabled
Restart MongoDB:
sudo systemctl restart mongod
Connect with authentication:
mongosh --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin
Performance Tuning
For optimal performance with BambiSleep.Chat, consider these settings:
# In mongod.conf
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 2 # Adjust based on available RAM (50% of RAM minus 1GB)
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
net:
maxIncomingConnections: 500
Database Setup
Creating Databases
BambiSleep.Chat requires two databases:
Connect to MongoDB:
mongosh --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin
Create main database:
use bambisleep
Create profiles database:
use bambisleep-profiles
Creating Collections
Set up required collections:
// Switch to main database
use bambisleep
// Create collections with validators
db.createCollection("chat_messages", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["content", "username", "timestamp"],
properties: {
content: { bsonType: "string" },
username: { bsonType: "string" },
timestamp: { bsonType: "date" },
processed: { bsonType: "bool" }
}
}
}
})
db.createCollection("session_history", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["sessionId", "username", "messages"],
properties: {
sessionId: { bsonType: "string" },
username: { bsonType: "string" },
startTime: { bsonType: "date" },
endTime: { bsonType: "date" },
messages: { bsonType: "array" }
}
}
}
})
// Switch to profiles database
use bambisleep-profiles
// Create profiles collection
db.createCollection("profiles", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["username", "displayName", "createdAt"],
properties: {
username: { bsonType: "string" },
displayName: { bsonType: "string" },
avatarUrl: { bsonType: "string" },
isPublic: { bsonType: "bool" },
xp: { bsonType: "int" },
level: { bsonType: "int" },
createdAt: { bsonType: "date" },
triggers: { bsonType: "array" }
}
}
}
})
// Create indexes for better performance
db.profiles.createIndex({ "username": 1 }, { unique: true })
db.profiles.createIndex({ "isPublic": 1 })
use bambisleep
db.chat_messages.createIndex({ "timestamp": -1 })
db.session_history.createIndex({ "username": 1 })
db.session_history.createIndex({ "sessionId": 1 }, { unique: true })
User Management
Create application-specific users:
// Switch to admin database
use admin
// Create application user for main database
db.createUser({
user: "bambisleep_app",
pwd: "strong_password_1",
roles: [
{ role: "readWrite", db: "bambisleep" }
]
})
// Create application user for profiles database
db.createUser({
user: "bambisleep_profiles",
pwd: "strong_password_2",
roles: [
{ role: "readWrite", db: "bambisleep-profiles" }
]
})
Connection Testing
Test MongoDB connection:
From the BambiSleep.Chat application directory, run:
npm run test:mongodb
Or manually test with mongosh:
mongosh "mongodb://bambisleep_app:strong_password_1@localhost:27017/bambisleep?authSource=admin"
Verify connection to profiles database:
mongosh "mongodb://bambisleep_profiles:strong_password_2@localhost:27017/bambisleep-profiles?authSource=admin"
Backup and Restore
Creating Backups
Back up entire database:
mongodump --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin --out /backup/$(date +"%Y-%m-%d")
Back up specific database:
mongodump --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin --db bambisleep --out /backup/$(date +"%Y-%m-%d")
Restoring from Backup
Restore entire backup:
mongorestore --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin /backup/2025-05-20/
Restore specific database:
mongorestore --host localhost --port 27017 -u admin -p secure_password --authenticationDatabase admin --db bambisleep /backup/2025-05-20/bambisleep/
Troubleshooting
Common Issues
Connection Refused
Error: MongoNetworkError: connect ECONNREFUSED
- Ensure MongoDB service is running:
sudo systemctl status mongod
- Check MongoDB is listening on the right port:
netstat -tulpn | grep mongod
- Verify firewall settings:
sudo ufw status
Authentication Failed
Error: Authentication failed
- Verify username and password
- Check the authSource parameter (should be "admin" for admin users)
- Confirm the user has appropriate roles
Disk Space Issues
Error: WiredTiger Error: No space left on device
- Check available disk space:
df -h
- Clean up or expand disk
- Consider implementing log rotation
Performance Issues
- Check system resources:
htop
- Analyze slow queries: Enable profiling level 1
- Check indexes with
db.collection.getIndexes()
MongoDB Logs
Log locations:
- Linux:
/var/log/mongodb/mongod.log
- Windows:
C:\Program Files\MongoDB\Server\6.0\log\mongod.log
- macOS:
/usr/local/var/log/mongodb/mongo.log
- Docker:
docker logs bambisleep-mongodb
Advanced Topics
Replication
For high availability, set up a MongoDB replica set:
Modify configuration files on each server:
replication: replSetName: "bambisleep_rs"
Restart MongoDB on each server
Connect to primary and initialize:
rs.initiate({ _id: "bambisleep_rs", members: [ { _id: 0, host: "mongo1:27017" }, { _id: 1, host: "mongo2:27017" }, { _id: 2, host: "mongo3:27017" } ] })
Update connection strings:
mongodb://user:password@mongo1:27017,mongo2:27017,mongo3:27017/bambisleep?replicaSet=bambisleep_rs&authSource=admin
Sharding
For very large deployments:
- Set up config servers (replica set)
- Set up shard servers (replica sets)
- Set up mongos routers
- Configure sharding on databases and collections
Monitoring
Set up MongoDB monitoring:
- MongoDB Compass (GUI tool)
- MongoDB Cloud Manager
- Prometheus with MongoDB exporter
- Grafana dashboards
For additional help, contact the BambiSleep development team at dev@bambisleep.chat
Last updated: May 20, 2025