- 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 mysqlAlternatively, you can pull preferred version with a specific tag:
docker pull mysql:8.2When MySQL image is downloaded, you can check it in Images tab in Docker Desktop or by running docker images:
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 4e8a34aea708 2 months ago 609MBStart 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
--nameoption assigns the container the namedrizzle-mysql. - The
-e MYSQL_ROOT_PASSWORD=option sets theMYSQL_ROOT_PASSWORDenvironment variable with the specified value. This is password for the root user. - The
-dflag runs the container in detached mode (in the background). - The
-poption maps port3306on the container to port3306on your host machine, allowing MySQL to be accessed from your host system through this port. - The
mysqlargument 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_PASSWORDforrootuser.
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-mysqlConfigure 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/mysqlNow you can connect to the database using the URL in your application.