|
| 1 | +# Database Usage Guide with Docker |
| 2 | + |
| 3 | +This guide helps you run code snippets with connections to databases in Docker environments. |
| 4 | + |
| 5 | +## Supported Databases |
| 6 | + |
| 7 | +- **MySQL** (8.0) |
| 8 | +- **PostgreSQL** (15) |
| 9 | +- **MongoDB** (6) |
| 10 | +- **Redis** (7) |
| 11 | +- **SQLite** (3.x) |
| 12 | + |
| 13 | +## How to Use |
| 14 | + |
| 15 | +### 1. Start a Specific Database |
| 16 | + |
| 17 | +```bash |
| 18 | +# Using Docker Compose directly |
| 19 | +docker-compose -f docker/environments/databases/docker-compose.yml up -d mysql |
| 20 | + |
| 21 | +# Or using the Makefile |
| 22 | +make db-start DB=mysql |
| 23 | +``` |
| 24 | + |
| 25 | +Valid values for DB are: `mysql`, `postgres`, `mongodb`, `redis`, `sqlite` |
| 26 | + |
| 27 | +### 2. Run a Code Snippet with Database Connection |
| 28 | + |
| 29 | +```bash |
| 30 | +# Using the script directly |
| 31 | +./docker/run-db-snippet.sh mysql snippets/databases/mysql_example.py |
| 32 | + |
| 33 | +# Or using the Makefile |
| 34 | +make db-run-snippet DB=mysql FILE=snippets/databases/mysql_example.py |
| 35 | +``` |
| 36 | + |
| 37 | +### 3. Stop a Database When Not in Use |
| 38 | + |
| 39 | +```bash |
| 40 | +# Using Docker Compose directly |
| 41 | +docker-compose -f docker/environments/databases/docker-compose.yml stop mysql |
| 42 | + |
| 43 | +# Or using the Makefile |
| 44 | +make db-stop DB=mysql |
| 45 | +``` |
| 46 | + |
| 47 | +## Database Connection Information |
| 48 | + |
| 49 | +When running a code snippet with a database, the following environment variables will be automatically passed to the container: |
| 50 | + |
| 51 | +- `DB_HOST`: Database hostname |
| 52 | +- `DB_PORT`: Database port |
| 53 | +- `DB_USER`: Username for connection |
| 54 | +- `DB_PASS`: Password for connection |
| 55 | +- `DB_NAME`: Database name |
| 56 | +- `DB_CONN_STR`: Full connection string |
| 57 | + |
| 58 | +### Default Connection Strings |
| 59 | + |
| 60 | +- **MySQL**: `mysql://user:password@tech-notes-mysql:3306/tech_notes` |
| 61 | +- **PostgreSQL**: `postgresql://user:password@tech-notes-postgres:5432/tech_notes` |
| 62 | +- **MongoDB**: `mongodb://user:password@tech-notes-mongodb:27017/tech_notes` |
| 63 | +- **Redis**: `redis://tech-notes-redis:6379` |
| 64 | +- **SQLite**: `sqlite:///data/tech_notes.db` |
| 65 | + |
| 66 | +### Environment Variables Configuration |
| 67 | + |
| 68 | +To change the default connection information, you can create a `.env` file in the `docker/environments/databases/` directory: |
| 69 | + |
| 70 | +1. Copy the `.env.example` file to `.env`: |
| 71 | + ```bash |
| 72 | + cp docker/environments/databases/.env.example docker/environments/databases/.env |
| 73 | + ``` |
| 74 | + |
| 75 | +2. Edit the `.env` file to change the connection information (usernames, passwords, database names, etc.) |
| 76 | + |
| 77 | +3. When running the `run-db-snippet.sh` command or `make db-run-snippet`, the environment variables from the `.env` file will be automatically used. |
| 78 | + |
| 79 | +Note: The `.env` file has been added to `.gitignore` to prevent it from being tracked by Git, ensuring sensitive information is not pushed to the repository. |
| 80 | + |
| 81 | +## Example Connection Code |
| 82 | + |
| 83 | +### Python with MySQL |
| 84 | + |
| 85 | +```python |
| 86 | +import os |
| 87 | +import mysql.connector |
| 88 | + |
| 89 | +# Get connection info from environment variables |
| 90 | +db_host = os.environ.get('DB_HOST', 'localhost') |
| 91 | +db_port = os.environ.get('DB_PORT', '3306') |
| 92 | +db_user = os.environ.get('DB_USER', 'user') |
| 93 | +db_pass = os.environ.get('DB_PASS', 'password') |
| 94 | +db_name = os.environ.get('DB_NAME', 'tech_notes') |
| 95 | + |
| 96 | +# Connect to the database |
| 97 | +conn = mysql.connector.connect( |
| 98 | + host=db_host, |
| 99 | + port=db_port, |
| 100 | + user=db_user, |
| 101 | + password=db_pass, |
| 102 | + database=db_name |
| 103 | +) |
| 104 | + |
| 105 | +cursor = conn.cursor() |
| 106 | +cursor.execute("SELECT * FROM users") |
| 107 | +users = cursor.fetchall() |
| 108 | + |
| 109 | +for user in users: |
| 110 | + print(user) |
| 111 | + |
| 112 | +conn.close() |
| 113 | +``` |
| 114 | + |
| 115 | +### JavaScript with MongoDB |
| 116 | + |
| 117 | +```javascript |
| 118 | +const { MongoClient } = require('mongodb'); |
| 119 | + |
| 120 | +// Get connection string from environment variable |
| 121 | +const uri = process.env.DB_CONN_STR || 'mongodb://user:password@localhost:27017/tech_notes'; |
| 122 | + |
| 123 | +async function main() { |
| 124 | + const client = new MongoClient(uri); |
| 125 | + |
| 126 | + try { |
| 127 | + await client.connect(); |
| 128 | + const database = client.db('tech_notes'); |
| 129 | + const users = database.collection('users'); |
| 130 | + |
| 131 | + const query = {}; |
| 132 | + const cursor = users.find(query); |
| 133 | + |
| 134 | + if ((await cursor.count()) === 0) { |
| 135 | + console.log("No documents found!"); |
| 136 | + } |
| 137 | + |
| 138 | + await cursor.forEach(user => { |
| 139 | + console.log(user); |
| 140 | + }); |
| 141 | + |
| 142 | + } finally { |
| 143 | + await client.close(); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +main().catch(console.error); |
| 148 | +``` |
| 149 | + |
| 150 | +## Sample Data |
| 151 | + |
| 152 | +Each database has been configured with the following sample data: |
| 153 | + |
| 154 | +- `users` table/collection with 3 users |
| 155 | +- `posts` table/collection with 4 posts linked to users |
| 156 | + |
| 157 | +You can see the details of the sample data in the init files in the respective directory of each database. |
| 158 | + |
0 commit comments