0%

wsl

wsl 的问题锦集

问题记录

WSL 升级到 WSL2 时,使用其内核升级包,提示“无法打开指定的文件或设备”

解决:
①移动内核补丁包到桌面或是其它盘符试一试
②打开BIOS的虚拟化

VS Code server for WSL closed unexpectedly check WSL terminal for more details

解决:

1
netsh winsock reset  //使用powershell(管理员)运行

WSL2 安装 mongod(v4)报错:System has not been booted with systemd as init system (PID 1). Can't operate.

分析:
所有报错信息都指向关键的System has not been booted with systemd as init system (PID 1). Can’t operate.,这个报错指的是我们没有启动systemd,使用service –status-all命令可以看出,我们的数据库的确没有启动。根据我网上搜索的结果systemd主要用来对数据库进行一些操作,但wsl系统中并不能使用systemd,所有我们可以大胆的在此推测,在创建阶段就失败了。这里我们的解决方案就出来了,我们可以尝试手动建立数据库。

解决:
①获取 MongoDB 安装包
方案一:
打开 MongoDB Download Center 页面,依次选择:Version:4.0.18Platform:Linux(legacy)Package:tgz,最后点击 Download 按钮下载即可。
方案二:

1
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.18.tgz

②安装 MongoDB
这里我将 MongoDB 安装在 /opt/mongodb 路径下,大家可以根据自己的实际情况修改安装路径。

解压 mongodb-linux-x86_64-4.0.18.gz 文件。

1
2
sudo tar -zxvf mongodb-linux-x86_64-4.0.18.gz
sudo mv /opt/mongodb-linux-x86_64-4.0.18 /opt/mongodb

进入 MongoDB 安装目录 /opt/mongodb,新建 dataetclog 三个目录。

③进入 /opt/mongodb/etc 目录,创建 MongoDB 配置文件 mongod.conf。这里需要注意的是 MongoDB 配置文件使用 YAML 格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

storage:
dbPath: /opt/mongodb/data
journal:
enabled: true
directoryPerDB: true

systemLog:
destination: file
logAppend: true
path: /opt/mongodb/log/mongod.log

net:
port: 27017
bindIp: 127.0.0.1

processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo

security:
authorization: disabled
DEFAULT CONFIGURATION FILE:
  • On Linux, a default /etc/mongod.conf configuration file is included when using a package manager to install MongoDB.
  • On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation.
  • On macOS, a default /usr/local/etc/mongod.conf configuration file is included when installing from MongoDB’s official Homebrew tap.

有关 MongoDB 配置文件选项,请参见 Configuration File Options

4.使用 --config 或着 -f 选项指定配置文件启动 MongoDB,以下两种方式任选一个即可。

1
2
mongod --config /opt/mongodb/etc/mongod.conf  //方式一
mongod -f /opt/mongodb/etc/mongod.conf //方式二

如果提示找不到 mongod 命令,有两种解决办法:

  1. 在 .bashrc 或 /etc/profile 文件中添加 MongoDB 环境变量。
  2. 进入 MongoDB 安装目录,将 bin 目录下的所有文件复制到 /usr/local/bin 目录。

上面的命令运行后,我们将在 Terminal 中看到以下提示信息,说明 MongoDB 启动成功。

最后,我们打开 Navicat 或 Robo 3T 客户端工具连接 WSL2 中的 MongoDB,测试连接是否成功。

来源: 筱杉少年,本文章著作权归原作者所有,任何形式的转载都请注明出处。