|
| 1 | +--- |
| 2 | +title: How to Set Up MySQL Using Docker |
| 3 | +author: Ayra |
| 4 | +updated_at: 2025/03/05 12:00:00 |
| 5 | +feature_image: /content/blog/how-to-set-up-mysql-using-docker/banner.webp |
| 6 | +tags: How-To |
| 7 | +featured: true |
| 8 | +description: 'Set up a local MySQL database with Docker.' |
| 9 | +--- |
| 10 | + |
| 11 | +<HintBlock type="info"> |
| 12 | + |
| 13 | +If you are using Mac, you can use following tools which include MySQL docker images across many versions: |
| 14 | +- [DBngin](https://dbngin.com/) - A free all-in-one database version manager |
| 15 | +- [Homebrew](https://brew.sh/) - Package manager for macOS |
| 16 | + |
| 17 | +</HintBlock> |
| 18 | + |
| 19 | +## Step-by-step Guide |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +Before you start, make sure you have [Docker](https://www.docker.com/products/docker-desktop) installed on your system. |
| 24 | + |
| 25 | +### Step 1: Pull the MySQL Docker Image |
| 26 | + |
| 27 | +Open your terminal and run: |
| 28 | + |
| 29 | +```bash |
| 30 | +docker pull mysql:8.0 |
| 31 | +``` |
| 32 | + |
| 33 | +This command downloads the official MySQL 8.0 image. You can replace `8.0` with your preferred version. |
| 34 | + |
| 35 | +### Step 2: Create and Run a MySQL Container |
| 36 | + |
| 37 | +```bash |
| 38 | +docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=your_password -p 3306:3306 -d mysql:8.0 |
| 39 | +``` |
| 40 | + |
| 41 | +This command: |
| 42 | +- Creates a container named `mysql-container` |
| 43 | +- Sets the root password to `your_password` (change this!) |
| 44 | +- Maps port 3306 from the container to your host |
| 45 | +- Runs MySQL in detached mode |
| 46 | + |
| 47 | +### Step 3: Verify the Container is Running |
| 48 | + |
| 49 | +```bash |
| 50 | +docker ps |
| 51 | +``` |
| 52 | + |
| 53 | +You should see your MySQL container in the list of running containers. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +### Step 4: Connect to MySQL |
| 58 | + |
| 59 | +Connect directly through the container using Docker CLI: |
| 60 | + |
| 61 | +```bash |
| 62 | +docker exec -it mysql-container mysql -uroot -p |
| 63 | +``` |
| 64 | + |
| 65 | +This command opens a MySQL shell inside the running container, giving you direct access to your database server. |
| 66 | + |
| 67 | +Alternatively, if you have the MySQL client installed on your host machine: |
| 68 | + |
| 69 | +```bash |
| 70 | +mysql -h 127.0.0.1 -P 3306 -uroot -p |
| 71 | +``` |
| 72 | + |
| 73 | +When prompted, enter the password you specified. |
| 74 | + |
| 75 | +### Step 5: Create a Database and User |
| 76 | + |
| 77 | +Once connected to MySQL, you can create a database and user: |
| 78 | + |
| 79 | +```sql |
| 80 | +CREATE DATABASE my_database; |
| 81 | +CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password'; |
| 82 | +GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'%'; |
| 83 | +FLUSH PRIVILEGES; |
| 84 | +``` |
| 85 | + |
| 86 | +You can exit MySQL server by `EXIT;`. |
| 87 | + |
| 88 | +## More Optional Operations |
| 89 | + |
| 90 | +### Persisting Data |
| 91 | + |
| 92 | +To persist your MySQL data beyond the container lifecycle, use a volume: |
| 93 | + |
| 94 | +```bash |
| 95 | +docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=your_password -p 3306:3306 -v mysql-data:/var/lib/mysql -d mysql:8.0 |
| 96 | +``` |
| 97 | + |
| 98 | +This creates a Docker volume named `mysql-data` that persists your database files. |
| 99 | + |
| 100 | +### Common Docker MySQL Commands |
| 101 | + |
| 102 | +Stop the container: |
| 103 | +```bash |
| 104 | +docker stop mysql-container |
| 105 | +``` |
| 106 | + |
| 107 | +Start an existing container: |
| 108 | +```bash |
| 109 | +docker start mysql-container |
| 110 | +``` |
| 111 | + |
| 112 | +Remove the container: |
| 113 | +```bash |
| 114 | +docker rm mysql-container |
| 115 | +``` |
| 116 | + |
| 117 | +### Using Docker Compose (Recommended for Development) |
| 118 | + |
| 119 | +For a more manageable setup, create a `docker-compose.yml` file: |
| 120 | + |
| 121 | +```yaml |
| 122 | +version: '3' |
| 123 | +services: |
| 124 | + mysql: |
| 125 | + image: mysql:8.0 |
| 126 | + container_name: mysql-container |
| 127 | + environment: |
| 128 | + MYSQL_ROOT_PASSWORD: your_password |
| 129 | + MYSQL_DATABASE: my_database |
| 130 | + MYSQL_USER: my_user |
| 131 | + MYSQL_PASSWORD: my_password |
| 132 | + ports: |
| 133 | + - "3306:3306" |
| 134 | + volumes: |
| 135 | + - mysql-data:/var/lib/mysql |
| 136 | + |
| 137 | +volumes: |
| 138 | + mysql-data: |
| 139 | +``` |
| 140 | +
|
| 141 | +Run with: |
| 142 | +```bash |
| 143 | +docker-compose up -d |
| 144 | +``` |
| 145 | + |
| 146 | +## Conclusion |
| 147 | + |
| 148 | +You now have a MySQL database running in Docker! This setup is ideal for development environments as it's isolated, reproducible, and easy to manage. |
| 149 | + |
| 150 | +For production deployments, consider additional security configurations and backup strategies. |
0 commit comments