踩坑 WSL2 安装 MySQL 失败

TL;DR

  • 环境:WSL2 (Ubuntu 24.04)
  • 坑点:安装MySQL 总是安装失败
  • 原因:Windows 系统下也安装了 MySQL。在 WSL2 中,localhost (127.0.0.1) 是直接映射到 Windows 主机的网络接口的,因此 Windows 上的 MySQL 服务会与 WSL 中的 MySQL 服务竞争相同的端口。

复现过程

安装 mysql-server

bash 复制代码
sudo apt install mysql-server

到这里总是安装不上,信息如下:

vbnet 复制代码
...
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up libhtml-parser-perl:amd64 (3.76-1build2) ...
Setting up libhttp-message-perl (6.36-1) ...
Setting up mysql-server-8.0 (8.0.42-0ubuntu0.22.04.2) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 22382
dpkg: error processing package mysql-server-8.0 (--configure):
 installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
Setting up libcgi-pm-perl (4.54-1) ...
Setting up libhtml-template-perl (2.97-1.1) ...
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-8.0; however:
  Package mysql-server-8.0 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          Setting up libcgi-fast-perl (1:2.15-1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.10) ...
Errors were encountered while processing:
 mysql-server-8.0
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

我借助 AI 尝试了各种方法,都不能解决。我尝试的方法包括:

  • AI 提示的各种骚操作
  • 卸载 MySQL,重新安装
  • 卸载 WSL 内的 Ubuntu distro,重新安装了一个

我重装 WSL Ubuntu distro 之后,又再次安装 mysql-server,再次失败:

vbscript 复制代码
Setting up mysql-server-8.0 (8.0.42-0ubuntu0.24.04.2) ...
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 457
dpkg: error processing package mysql-server-8.0 (--configure):
 installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-8.0; however:
  Package mysql-server-8.0 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 mysql-server-8.0
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

解决过程

从上面的失败信息中其实很难找到原因,但似乎好像是因为 mysqld 启动失败 在 AI 的提示下,我去检查了/var/log/mysql/error.log

bash 复制代码
sudo cat /var/log/mysql/error.log

我省略了无关紧要的信息,看到两条关键的 log 如下:

vbscript 复制代码
2025-07-24T16:07:55.915365Z 0 [ERROR] [MY-011300] [Server] Plugin mysqlx reported: 'Setup of bind-address: '127.0.0.1' port: 33060 failed, `bind()` failed with error: Address already in use (98). Do you already have another mysqld server running with Mysqlx ?'

...

2025-07-24T16:07:55.966055Z 0 [ERROR] [MY-010257] [Server] Do you already have another mysqld server running on port: 3306 ?

3306 和 33060 端口被占用。MySQL 服务启动默认会占用这两个端口。我忽然想到我的 Windows 系统也曾安装过 MySQL,这时我知道大概率就是因为 Windows 系统下的启动 MySQL 服务占用了这两个端口,导致我 WSL Ubuntu 环境无法启动 MySQL。

其实当时我的 MySQL 已经安装成功了,只是在安装完自动启动 MySQL 服务的过程中,由于默认端口被占用,无法启动成功。

在 Windows 中确认端口 3306 和 33060 是否被占用:

bash 复制代码
netstat -aon | findstr :3306
netstat -aon | findstr :33060

在 WSL 中也检查端口:

bash 复制代码
sudo netstat -tuln | grep -E '3306|33060'

接着,检查 Windows 上是否正在运行 MySQL 服务:

  1. 打开 Windows 的"服务"管理工具:
    • 按 Win + R,输入 services.msc,按 Enter。
    • 查找名为 MySQL 或类似(如 MySQL80)的服务。
  2. 如果服务正在运行,右键点击并选择"停止"。

我上面说过,其实当时我已经安装 MySQL 成功了:

bash 复制代码
$ mysql --version
mysql  Ver 8.0.42-0ubuntu0.24.04.2 for Linux on x86_64 ((Ubuntu))

查看 MySQL 服务状态,如果不是正常开启状态,需要运行下面命令开启:

bash 复制代码
sudo service mysql start

MySQL 服务正常开启状态如下:

bash 复制代码
$ sudo service mysql status

● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Fri 2025-07-25 00:24:27 CST; 57min ago
    Process: 2583 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/>
   Main PID: 2592 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 19043)
     Memory: 339.5M (peak: 379.1M swap: 25.0M swap peak: 25.0M)
        CPU: 37.241s
     CGroup: /system.slice/mysql.service
             └─2592 /usr/sbin/mysqld

End: 其实可以做到 WSL 和 Windows 环境同时运行 MySQL 服务,改配置文件即可,让两个服务运行在不同端口。但是我不想这么做。

相关推荐
梁萌2 分钟前
mysql使用事件做日志表数据转移
数据库·mysql
lThE ANDE3 分钟前
MySQL中的TRUNCATE TABLE命令
数据库·mysql
晨曦夜月15 分钟前
进程的五大状态及特殊进程解析
linux·服务器·算法
生而为虫18 分钟前
Claude Code 最新版安装教程(Windows/Mac/Linux 全平台) 面向普通用户的 Claude Code 安装与模型接入指南
linux·windows·macos
STER labo35 分钟前
mysql配置环境变量——(‘mysql‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件解决办法)
数据库·mysql·adb
Sarvartha1 小时前
三目运算符
linux·服务器·前端
dreamZhanglx1 小时前
MySQL进阶
数据库·mysql
xmjd msup1 小时前
MySQL 函数
数据库·mysql
vortex51 小时前
Kali Linux 安装与使用 Code-OSS / VSCodium :VSCode 轻量替代
linux·运维·编辑器
.柒宇.2 小时前
AI掘金头条项目部署实践指南
linux·运维·python·fastapi