Resolution - Cannot Start MySQL on WSL

Raymond Raymond event 2021-01-05 visibility 3,749
more_vert

Issue context

After installed MySQL server on Windows Subsystem for Linux (WSL, distro Ubuntu-18.04), the following command failed:

sudo service mysql start

Looking into the log file '/var/log/mysql/error.log', it shows the following error:

2021-01-05T10:50:42.322149Z 0 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
2021-01-05T10:50:42.322313Z 0 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
2021-01-05T10:50:42.322380Z 0 [Note] Server socket created on IP: '127.0.0.1'.
2021-01-05T10:50:42.322439Z 0 [ERROR] Can't start server: Bind on TCP/IP port: Permission denied
2021-01-05T10:50:42.322470Z 0 [ERROR] Do you already have another mysqld server running on port: 3306 ?
2021-01-05T10:50:42.322509Z 0 [ERROR] Aborting

Basically port 3306 number cannot be bound. 

Find out the root cause 

First run the following command in windows Command Prompt:

netstat -an | grep :3306

The result is empty in my scenario, i.e. no current process is bound to port 3306.

Then run the following command to find out Hyper-V reserved ports if Hyper-V is enabled on your computer:

netsh interface ipv4 show excludedportrange protocol=tcp

In my system, it outputs the following:

Protocol tcp Port Exclusion Ranges

Start Port    End Port
----------    --------
        80          80
      1542        1641
      1642        1741
      1742        1841
      1842        1941
      1942        2041
      2042        2141
      2282        2381
      2410        2509
      2611        2710
      2711        2810
      2811        2910
      2911        3010
      3011        3110
      3111        3210
      3211        3310
      3311        3410
      3605        3704
      3705        3804
      5357        5357
      5700        5700
      8081        8081
      8082        8082
      8884        8884
     50000       50059     *

Port 3306 was reserved.

Solution 1 - Change MySQL port number to a different one

In WSL, perform the following actions:

  1. Change directory to MySQL folder:
    cd /etc/mysql/
  2. Edit file my.cnf (sudo vi my.cnf) to change default port number to 10101 (or any other available port number in your system):
    [mysqld]
    general_log_file        = /home/tangr/mysql-logs/mysql.log
    general_log             = 1
    port = 10101

    The default settings are:

    [mysqld_safe]
    socket          = /var/run/mysqld/mysqld.sock
    nice            = 0
    
    [mysqld]
    #
    # * Basic Settings
    #
    user            = mysql
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    port            = 3306
    basedir         = /usr
    datadir         = /var/lib/mysql
    tmpdir          = /tmp
    lc-messages-dir = /usr/share/mysql
    skip-external-locking
    #
    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    bind-address            = 127.0.0.1
  3. Restart the service:
    sudo service mysql restart
    
  4. You should be able to see the following messages when it is successful: 
    $ sudo service mysql restart
     * Stopping MySQL database server mysqld                                                                         [ OK ]
     * Starting MySQL database server mysqld                                                                         [ OK ]

Solution 2 - Free port 3306

Follow these steps to free port 3306:

  1. Disable Hyper-V via Control Panel Windows Features GUI or the following command:
    dism.exe /Online /Disable-Feature:Microsoft-Hyper-V
    
  2. After reboot, reserve port 3306:
    netsh int ipv4 add excludedportrange protocol=tcp startport=3306 numberofports=1
  3. Enable Hyper-V again via Control Panel or the following command:
    dism.exe /Online /Enable-Feature:Microsoft-Hyper-V /All

Now MySQL service should be able to use this default port 3306.

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts