Containers are all the rage, and Docker is the container engine of choice these days. So, I wanted to provide this tutorial for setting up a MySQL server instance in a Docker container.
Pre-requisites:
- Docker Desktop is installed and running. If you’re on a version of Windows that doesn’t support Docker Desktop, then you can use Docker Toolbox.
- Ability to access Docker commands from a terminal or command prompt.
- MySQL Workbench is downloaded and installed on your computer
Getting Image and Creating a Container:
- Pull the MySQL image from DockerHub. For the purpose of this specific tutorial, we’ll assume the image is mysql and not mysql-server.
docker pull mysql
- Create a MySQL Container with container port 3306 mapped host port 3306. This will create a container named cse3330db. You should replace <some_password> with a password of your choice. Don’t use a passwords that you might use in other places as it will be floating around in free text.
docker run --name cse3330db -e MYSQL_ROOT_PASSWORD=<some_password> -p 3306:3306 -d mysql:latest
Connect to The Container
Use MySQL Client inside the container
Remember that MySQL is a client server architecture. This image contains both the database server as well as the command line client tools to access the server. To execute mysql (which is the client) inside the container, use the following command.
docker run -it --link cse3330db:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
Starting a Bash Shell in the Container
If you want to start a shell inside the container itself, use the following command:
docker exec -it cse3330db bash
There’s very limited functionality, but you will be able to use basic shell commands.
Connecting from MySQL Workbench
- From the Database menu, choose ‘Manage DB Connections …’
- Choose ‘New’ in the bottom left corner of the dialog.
- Give your connection a new name
- Choose the Connection Tab
- For Hostname, use 0.0.0.0
- For Port, use 3306.
- For username, use root.
- Click Test Connection. A dialog should pop up asking for the root user’s password. This is the one you used when creating the image above.
- If the connection test was successful, click close.
- You should see your new server connection on the Home screen of MySQL Workbench now. Click on it to open the connection.
If there are no errors, then congrats!
Speak Your Mind