Installing MySQL 8.0 Under WSL 2 and Ubuntu

Submitted on May 02, 2020, 11:38 p.m.

Here's a PSA for those wanting to install MySQL Server 8.0 under the new Windows Subsystem for Linux - WSL 2, with an Ubuntu distribution.

Systemd is not available as a default in WSL 2, and init.d scripts are not run at startup.

What's more, if you install MySQL 8 from the deb package here https://dev.mysql.com/downloads/repo/apt/ - the default mysql.server helper script that is used to start or stop MySQL won't be installed.

One solution is to download the mysql.server.sh script from here - https://github.com/mysql/mysql-server/tree/8.0/support-files - and then copy and rename the script to /etc/init.d/mysql (make sure that it's also executable - chmod +x mysql)

You'll then need to set the default values for basdir, datadir and pid file locations.

Here's an excerpt with the top portion of the file and the settings that worked for me...

1# If you change base dir, you must also change datadir. These may get
2# overwritten by settings in the MySQL configuration files.
3
4basedir=/usr
5datadir=/var/lib/mysql
6
7# Default value, in seconds, afterwhich the script should timeout waiting
8# for server start.
9# Value here is overriden by value in my.cnf.
10# 0 means don't wait at all
11# Negative numbers mean to wait indefinitely
12service_startup_timeout=900
13
14# Lock directory for RedHat / SuSE.
15lockdir='/var/lock/subsys'
16lock_file_path="$lockdir/mysql"
17
18# The following variables are only set for letting mysql.server find things.
19
20# Set some defaults
21mysqld_pid_file_path=/var/run/mysqld/mysqld.pid
22if test -z "$basedir"

After this you should be able to start and stop MySQL as follows:

1sudo service mysql start
2sudo service mysql stop

Updated: In this post https://www.58bits.com/blog/2020/05/30/wsl-2-setup-development  I created a helper script that I use to start and stop the services I need under WSL 2...

1#!/bin/bash
2sudo mkdir -p /var/run/mysqld
3sudo chown mysql:mysql /var/run/mysqld
4sudo service mysql "$@"
5sudo service php7.3-fpm "$@"
6sudo service nginx "$@"
7SOLR_ULIMIT_CHECKS=false /opt/solr/bin/solr "$@"

For whatever reason, I need to check that the /var/run/mysqld directory exists and permissions are set before starting MySQL.

I then alias the following commands in my .localrc or .local.fish files:

1#wsl
2alias wsl-up='~/Scripts/windows/wsl-exec-services.sh start'
3alias wsl-down='~/Scripts/windows/wsl-exec-services.sh stop'

If I've rebooted, or I want to switch to another distribution running similar services, I run wsl-up, or wsl-down as needed.