Windows 环境安装 Redis

关注公众号【1024个为什么】,及时接收最新推送文章!

18 年的时候由于好奇 + 装X + 活动优惠,买了个 3 年的阿里云服务器,想着没事练练手,可直到服务器过期,也没练过几次,不知道有多少小伙伴跟我一样...

最近想在本地(Windows 系统)装个 Redis,发现得先装 Linux 环境,才发现当初年少无知,花了冤枉钱..

话不多说,直接看整个安装过程。重点是 使用 Xshell 连接 WSL 有点坑。

先确认 Windows 系统版本

fe015743f7aa168a08ec1ebc1340520d.png

33db6ee80bffa842d4894245cb69f7ae.png

必须高于 WIN10 (19041) ,或者 WIN11

安装 WSL

进入命令行,执行 wsl.exe

C:\Users\daojia>wsl.exe
Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Sep  2 15:31:22 CST 2022

  System load:    0.52      Processes:              7
  Usage of /home: unknown   Users logged in:        0
  Memory usage:   84%       IPv4 address for wifi2: 10.253.110.192
  Swap usage:     14%

0 updates can be installed immediately.
0 of these updates are security updates.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once once a day. To disable it please create the
/home/unix/.hushlogin file.

以 root 用户身份启动 Linux(很多东西都需要 root 权限),执行 wsl.exe --user root

C:\Users\daojia>wsl.exe --user root
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#

安装 ssh

由于我们都习惯用 Xshell 等终端去操作,所以我们把本机当做远程服务器,通过 Xshell 连接登录,像真正使用 Linux 服务器一样。

首先要安装 ssh,执行 apt-get install openssh-server

root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  openssh-client openssh-sftp-server
Suggested packages:
  keychain libpam-ssh monkeysphere ssh-askpass molly-guard
The following packages will be upgraded:
  openssh-client openssh-server openssh-sftp-server
3 upgraded, 0 newly installed, 0 to remove and 275 not upgraded.
Need to get 1099 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]

输入 Y,继续

Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 openssh-server amd64 1:8.2p1-4 [377 kB]
Fetched 377 kB in 2s (169 kB/s)
Preconfiguring packages ...
Selecting previously unselected package openssh-server.
(Reading database ... 31818 files and directories currently installed.)
Preparing to unpack .../openssh-server_1%3a8.2p1-4_amd64.deb ...
Unpacking openssh-server (1:8.2p1-4) ...
Setting up openssh-server (1:8.2p1-4) ...
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:Dpk45enJQgIQoz16NpGxc8R6d1ZnWx8/iyE5kNxpy9Q root@DESKTOP-LRCQE95 (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:CM78lkrFr4PHvCKsOcmmGSU8N3a0XVNWEEtbQt9GNVw root@DESKTOP-LRCQE95 (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:U1QoEqgH0oGBmreW0EUrRM/wBJsTzY/AXu8VKlc1RBA root@DESKTOP-LRCQE95 (ED25519)
invoke-rc.d: could not determine current runlevel
Processing triggers for systemd (245.4-4ubuntu3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ufw (0.36-6) ...
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#

修改配置文件

执行 vim /etc/ssh/sshd_config

root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# vim /etc/ssh/sshd_config

修改端口,系统默认的 ssh 端口是 22,为避免冲突,可以修改为 2222

0efe6cf2fc00597f83da839112fe5df0.png

允许 root 用户登录

9f29df9c122c3d91a9101341aed41b4f.png

打开身份验证 PasswordAuthentication,

本想不开启身份验证,但试了好多种方案,都没连接成功,还是采用身份验证的方式吧

305f5dbe6e67d2308576f3d4eabbd265.png

保存退出后重启 ssh,执行 service ssh --full-restart

root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# service ssh --full-restart                                                                               [ OK ]
 * Starting OpenBSD Secure Shell server sshd                                                                                                       [ OK ]
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#

启动服务的命令是 service ssh start

第一次启动时可能会有网络安全提示,选择“专用网络”即可。

重置系统密码,后面连接要用

root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# passwd
New password:
Retype new password:
passwd: password updated successfully

使用 Xshell 连接

配置 协议、host、端口

91c3549ac2332dc940179b618fc63dcb.png

配置用户名、密码,就是刚才前面设置的密码

6d84f816f59d60410b2226c1fb77dab1.png

点击连接

Connecting to 127.0.0.1:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Sep  2 21:18:32 CST 2022

  System load:    0.52      Processes:              14
  Usage of /home: unknown   Users logged in:        0
  Memory usage:   80%       IPv4 address for wifi2: 10.253.110.192
  Swap usage:     15%


308 updates can be installed immediately.
203 of these updates are security updates.
To see these additional updates run: apt list --upgradable

New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Fri Sep  2 20:35:58 2022 from 127.0.0.1
root@DESKTOP-LRCQE95:~#

如果连接失败,可以从以下几点排查

是否启动了 ssh 服务?

host、port 是否正确,单独 telnet 是否通?

是否开着防火墙?

用户名密码是否正确?

安装 Redis

按照官网的说明,依次执行下面的命令

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

启动服务端

root@DESKTOP-LRCQE95:~# sudo service redis-server start
Starting redis-server: redis-server.

启动客户端,并测试

root@DESKTOP-LRCQE95:~# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key"
127.0.0.1:6379>

有网友说,启动 WSL 后,命令行窗口不能关闭。我这个版本亲测关闭命令行窗口后,Xshell 还可以随意连接,只要电脑不重启,就可以一直使用。

接下来就可以为所欲为了。

参考文献

https://docs.microsoft.com/en-us/windows/wsl/install

https://redis.io/docs/getting-started/installation/install-redis-on-windows/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值