Run MySQL in Docker on Windows
Tags: mysql, docker, windows, aws
Published: 2024-04-12 18:47:33

##Summary
Resolving MySQL/MariaDB database and table name case sensitivity on Windows.

##Details
I develop locally on my Windows 10 laptop. When I started my first project that used MySQL as the backend, I installed MySQL on my Windows but noticed that the database and table names were created in lower case. However, the same SQL scripts were creating them with intended casing in AWS RDS.

Here is a sample SQL script.
``` SQL
CREATE DATABASE IF NOT EXISTS `Sandbox`;
USE `Sandbox`;

CREATE TABLE IF NOT EXISTS `MyTable` (
  `Id` int(11) DEFAULT NULL,
  `Name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

```
Running this script in MySQL on Windows creates the following as shown in this screenshot from MySQL Workbench. Database and table names in lower case.

![MySQL Workbench showing database and table names in lower case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajl8vnco42kpi550ncd8.png)

To resolve this, I chose to go the Docker route and install/run MySQL from within a Linux Docker container on Windows.

Here are the steps I took to get this working.
1. Visit [Docker Desktop on Windows](https://docs.docker.com/desktop/install/windows-install/) for detailed steps. Check your machine configuration and ensure you meet the requirements.
2. Download and install Docker Desktop on Windows.
3. Once installed, it should look something like the screenshot shown below. (My Docker version is Version 4.27.2 (137060) at the time of this writing)
![Screenshot of Docker Desktop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlv9brnb1bdo7biph97o.png)
4. With Docker Desktop installed, you can run Docker commands from the Windows Command line.
5. In this example, I'm going to pull down the Docker MySQL 8.0.36 image by running the following command from the Windows Command Line.
    ``` docker pull mysql:8.0.36 ```
6. Once complete, you can see the image in Docker Desktop when you click on the Images icon on the left as shown here.
![View Images in Docker Desktop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1h53u0081tar4lfhhgy.png)
7. Next, we will create a container using this image with the command below. The **--name** is the name of the container, **--env** used to set the environment variables for the container, **--publish** is to publish a container's port(s) to the host and finally **--detach** is to run container in background and print container ID. Please see [Docker CLI](https://docs.docker.com/reference/cli/docker/container/run/) for more information.
    ``` docker run --name mysql8036-container --env MYSQL_ROOT_HOST=% --env MYSQL_ROOT_PASSWORD=passwordgoeshere --publish 15002:3306 --detach docker.io/library/mysql:8.0.36 ```
8. You should now see the container named "mysql8036-container" running in Docker Desktop when you click the "Containers" icon on the left.
![Docker Desktop showing the container named "mysql8036-container"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9j7ff1kbahlojzure6a.png)
9. Next we will connect to this new MySQL instance using MySQL Workbench.
10. Open MySQL Workbench, go to menu option Database - Manage Connections. Click "New". (My version is MySQL Workbench 8.0 Version 8.0.33 build 2947366 CE)
11. Enter the following information.
  a. Connection Name = MySQL8036-Docker
  b. Connection Method = Standard (TCP/IP)
  c. Hostname = localhost
  d. Port = 15002 (The published port from the Docker run command)
  e. Username = root
  f. Password = passwordgoeshere (The value passed to MYSQL_ROOT_PASSWORD in the run Docker run command)
  g. Then click the "Test Connection" to verify connectivity.    
12. In MySQL Workbench, use the menu option Database - Connect to Database and the connection we created above to connect to the new database instance.
13. Run the SQL script from above and voila we now have the database and table names with casing as intended.
![MySQL Workbench showing database and table names with intended casing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynf2zqnurzsz1yns8u8t.png)

You can now write web/console applications that connect to this instance and not have to worry about case-sensitivity with database and table names. 

I hope this helps you out. Feedback is always welcome.

###Reference links
https://docs.docker.com/desktop/install/windows-install/
https://dev.mysql.com/doc/refman/8.0/en/linux-installation-docker.html
https://docs.docker.com/reference/cli/docker/container/run/