MySQL Database Overview
MySQL is a widely-used open-source relational database management system, favored for its performance, reliability, and ease of use, particularly in small to medium-sized web applications.
Official Docker Image
The official MySQL Docker image is available at: https://hub.docker.com/_/mysql.
Deploying a MySQL Container
Starting the Container
Use the following command to run a MySQL container instance:
docker run --name mysql-db `
-e MYSQL_ROOT_PASSWORD=securepassword `
-p 3306:3306 `
-d mysql
| Parameter | Description |
|---|---|
-e MYSQL_ROOT_PASSWORD |
Required. Sets the password for the MySQL root user. |
-p 3306:3306 |
Maps the container's port 3306 to the host's port 3306. |
-d |
Runs the container in detached mode (background). |
--name |
Assigns a custom name to the container. |
mysql |
The official Docker image tag. |
To persist data, mount a volume or host directory using the -v flag:
- Docker Volume:
-v mysql-data:/var/lib/mysql - Host Directory:
-v /host/data/path:/var/lib/mysql
Updating Root Authentication Method
Some MySQL client tools may fail to connnect due to the default caching_sha2_password authentication plugin. To switch to the mysql_native_password method:
- Access the container's shell: ```
docker exec -it mysql-db /bin/bash
- Connect to the MySQL server: ```
mysql -h localhost -u root -p
- Execute the following SQL command to alter the root user's authentication: ```
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'newpassword';
Managing the MySQL Instance
Using the MySQL Command-Line Client
Inside the container, use the mysql client for administrative tasks such as listing databases, creating new schemas, and running queries.
Visual Studio Code Extensions
VS Code offers extensions for database management:
- MySQL Extension: Provides a graphical interface to connect and manage MySQL servers directly within the editor.
- SQLTools: Supports multiple databases (MySQL, PostgreSQL, SQLite, etc.) with features like query completion, bookmarks, and data export to CSV or JSON.
Deploying phpMyAdmin via Docker
phpMyAdmin is a web-based MySQL administration tool. Run it in a container linked to your MySQL instance:
docker run --name phpmyadmin `
--link mysql-db:db `
-e MYSQL_ROOT_PASSWORD=securepassword `
-p 8080:80 `
-d phpmyadmin/phpmyadmin
After starting, access the interface at http://localhost:8080 and log in with the root credentials.