主页

windows - 重启电脑之后 wsl 命令行里面的 mysql 又无法连接到 mysql workbench

2026-03-12 09:34AM

我之前在电脑里面实现了 wsl 命令行里面的 mysql 可以连接到 mysql workbench,参考这篇:https://meiyi.site/articles/1199 但是重启完电脑之后发现又连接不上了

主要原因;

是由于端口不匹配,我在重启电脑之前 mysql 里面的端口设置的是 13306,但是重启完电脑之后 mysql 的端口变成了 33060

$ sudo service mysql status
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-03-12 09:17:04 CST; 2s ago
    Process: 1867 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 1875 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 38447)
     Memory: 368.1M (peak: 382.8M)
        CPU: 650ms
     CGroup: /system.slice/mysql.service
             └─1875 /usr/sbin/mysqld

Mar 12 09:17:02 DESKTOP-OFMC52Q systemd[1]: Starting mysql.service - MySQL Community Server...
Mar 12 09:17:03 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:03.089821Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.45-0ubuntu0.24.04.1) starting as process 1875
Mar 12 09:17:03 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:03.099001Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
Mar 12 09:17:04 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:04.614539Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
Mar 12 09:17:04 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:04.918703Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
Mar 12 09:17:04 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:04.918743Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now suppo>
Mar 12 09:17:04 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:04.941730Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/>
Mar 12 09:17:04 DESKTOP-OFMC52Q mysqld[1875]: 2026-03-12T01:17:04.941881Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.45-0ubuntu0.24.04.1'  sock>
Mar 12 09:17:04 DESKTOP-OFMC52Q systemd[1]: Started mysql.service - MySQL Community Server.

$ sudo netstat -tulpn | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      1875/mysqld
tcp6       0      0 :::33060                :::*                    LISTEN      1875/mysqld

解决方法:

1. 打开 /etc/mysql/my.cnf 文件,在文件末尾添加一行,引入重启电脑之前修改的目录 /etc/mysql/mysql.conf.d/

$ sudo cat /etc/mysql/my.cnf
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
!includedir /etc/mysql/mysql.conf.d/  # 增加这一行

2. 重启 mysql 

$ sudo systemctl restart mysql 

3. 验证端口是否生效

$ sudo netstat -tulpn | grep mysqld
tcp        0      0 0.0.0.0:33060           0.0.0.0:*               LISTEN      2039/mysqld
tcp        0      0 0.0.0.0:13306           0.0.0.0:*               LISTEN      2039/mysqld 

然后重新连接 mysql workbench,就发现已经可以了。 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论