3.5 KiB
3.5 KiB
Database Quick Reference
Database Connection Test
To test the database connection at any time:
npm run test:db
This will:
- Verify MySQL server is running
- Check database credentials
- Confirm database exists
- Show MySQL version
- List existing tables
Sequelize CLI Commands
Database Creation
Create the database manually:
mysql -u root -p -e "CREATE DATABASE interview_quiz_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
Migrations
Generate a new migration:
npx sequelize-cli migration:generate --name migration-name
Run all pending migrations:
npm run migrate
Undo last migration:
npm run migrate:undo
Check migration status:
npm run migrate:status
Seeders
Generate a new seeder:
npx sequelize-cli seed:generate --name seeder-name
Run all seeders:
npm run seed
Undo all seeders:
npm run seed:undo
Undo specific seeder:
npx sequelize-cli db:seed:undo --seed seeder-filename.js
Configuration Files
.sequelizerc
Configures Sequelize CLI paths for:
- config
- models-path
- seeders-path
- migrations-path
config/database.js
Contains environment-specific database configurations:
development- Local developmenttest- Testing environmentproduction- Production settings
config/db.js
Database utility functions:
testConnection()- Test database connectionsyncModels()- Sync models with databasecloseConnection()- Close database connectiongetDatabaseStats()- Get database statistics
models/index.js
- Initializes Sequelize
- Loads all model files
- Sets up model associations
- Exports db object with all models
Connection Pool Configuration
Current settings (from .env):
DB_POOL_MAX=10- Maximum connectionsDB_POOL_MIN=0- Minimum connectionsDB_POOL_ACQUIRE=30000- Max time to get connection (ms)DB_POOL_IDLE=10000- Max idle time before release (ms)
Server Integration
The server (server.js) now:
- Tests database connection on startup
- Provides database stats in
/healthendpoint - Warns if database connection fails
Test the health endpoint:
curl http://localhost:3000/health
Response includes:
{
"status": "OK",
"message": "Interview Quiz API is running",
"timestamp": "2025-11-09T...",
"environment": "development",
"database": {
"connected": true,
"version": "8.0.42",
"tables": 0,
"database": "interview_quiz_db"
}
}
Troubleshooting
Connection Failed
If database connection fails, check:
- MySQL server is running
- Database credentials in
.envare correct - Database exists
- User has proper permissions
Access Denied
# Grant permissions to user
mysql -u root -p -e "GRANT ALL PRIVILEGES ON interview_quiz_db.* TO 'root'@'localhost';"
mysql -u root -p -e "FLUSH PRIVILEGES;"
Database Not Found
# Create database
mysql -u root -p -e "CREATE DATABASE interview_quiz_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
Check MySQL Service
Windows:
net start MySQL80
Linux/Mac:
sudo systemctl start mysql
# or
brew services start mysql
Next Steps
After Task 2 completion, you can:
- ✅ Test database connection
- 🔄 Start creating migrations (Task 4+)
- 🔄 Build Sequelize models
- 🔄 Run migrations to create tables
- 🔄 Seed database with initial data
Status: Database setup complete and verified! ✅