- Install latest Docker Desktop. Follow the instructions for your operating system.
Pull the MySQL image
Pull the latest MySQL image from Docker Hub. In your terminal, run docker pull mysql
to pull the latest MySQL version from Docker Hub:
docker pull mysql
Alternatively, you can pull preferred version with a specific tag:
docker pull mysql:8.2
When MySQL image is downloaded, you can check it in Images
tab in Docker Desktop or by running docker images
:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 4e8a34aea708 2 months ago 609MB
Start a MySQL instance
To start a new MySQL container, run the following command:
docker run --name drizzle-mysql -e MYSQL_ROOT_PASSWORD=mypassword -d -p 3306:3306 mysql
- The
--name
option assigns the container the namedrizzle-mysql
. - The
-e MYSQL_ROOT_PASSWORD=
option sets theMYSQL_ROOT_PASSWORD
environment variable with the specified value. This is password for the root user. - The
-d
flag runs the container in detached mode (in the background). - The
-p
option maps port3306
on the container to port3306
on your host machine, allowing MySQL to be accessed from your host system through this port. - The
mysql
argument specifies the image to use for the container. You can also specify other versions likemysql:8.2
.
You can also specify other parameters like:
-e MYSQL_DATABASE=
to create a new database when the container is created. Default ismysql
.-e MYSQL_USER=
and-e MYSQL_PASSWORD=
to create a new user with a password when the container is created. But you still need to specifyMYSQL_ROOT_PASSWORD
forroot
user.
To check if the container is running, check Containers
tab in Docker Desktop or use the docker ps
command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19506a8dc12b mysql "docker-entrypoint.sā¦" 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:3306->3306/tcp drizzle-mysql
Configure database url
To connect to the MySQL database, you need to provide the database URL. The URL format is:
mysql://<user>:<password>@<host>:<port>/<database>
You should replace placeholders with your actual values. For example, for created container the url will be:
mysql://root:mypassword@localhost:3306/mysql
Now you can connect to the database using the URL in your application.