- 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:
Alternatively, you can pull preferred version with a specific tag:
When MySQL image is downloaded, you can check it in Images
tab in Docker Desktop or by running docker images
:
Start a MySQL instance
To start a new MySQL container, run the following command:
- 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:
Configure database url
To connect to the MySQL database, you need to provide the database URL. The URL format is:
You should replace placeholders with your actual values. For example, for created container the url will be:
Now you can connect to the database using the URL in your application.