Using Docker to Run MySQL on WSL2
3 min read
Your development environment is a Linux distribution made available on Windows through Windows Subsystem For Linux 2 (WSL2). You need to stand up a MySQL (or other) database that is persistent, available to your WSL2-enabled development environment, and accessible to database management systems (DBMS) natively installed on your Windows host.
We can containerize a MySQL (or other) database. I accomplished this using docker-compose, but you can probably easily modify this to run from a standalone Dockerfile if you wish.
version: "3.9" services: mysql-dev: image: mysql command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: my-secret-pw ports: - "3306:3306" volumes: - ./data:/var/lib/mysql
This should be saved in the directory that you intend to execute docker-compose from. Then, from the terminal, run:
$ docker-compose up --build -d
- We create a container called
mysql(latest tag implied) image hosted on the official mysql Docker hub registry.
- We run the command specified in the Mysql official Docker image documentation:
--default-authentication-plugin=mysql_native_password. According to the MySQL 8.0 documentation,
mysql_native_passwordis a plugin that implements authentication based on the password hashing method in use from before the introduction of plugable authentication.
- We set restart to always -- this is user preference, but generally I would like my database to restart automatically if it crashes.
- We set
MYSQL_ROOT_PASSWORDin the environmental variables of the container. Since this is for development, the password doesn't really matter. Please do not use this password in production!
- We then map port 3306 (the standard port address of MySQL) of the host system to port 3306 of the container. This allows us to connect to the service running on the container using the host's IP address.
- We mount our local data directory,
./data, as a volume to the container's default MySQL data directory,
/var/lib/mysql. This allows us to persist the data created in the database even if the container is destroyed.
docker-compose up --build -dwill run the container in "detached" mode--that is, it will be run as a background process and you will still have access to your teminal. You can validate that the container is running by using
Connecting to MySQL
- From the WSL2 terminal, we can connect to the container using
localhost:3306as our address.
- From the Windows host, we can connect to the container using the WSL2 distros private IP address. To find this, from the WSL2 terminal, run
ip a show eth0(your interface may vary--if you receive no results, simply do
ip ato show all interfaces).
I found it challenging to connect to a Windows MySQL instance from WSL2, which is why I researched the methodology shown above. I personally like the container approach because it can allow me to easily migrate the database in the future and it is portable in the event that I decide to develop from a different workstation.
Note: I used MySQL for this exercise, but perhaps you could achieve similar results with another database. Docker Hub supports images for most databases--find the system you prefer, read the documentation on the image, and apply the steps shown above to (hopefully) create a portable, persistent, development database.