Ubuntu上安装Redis的详细教程

1、安装redis

首先,访问Redis官网,点击首页的【Get Started】,然后点击Install Redis on Linux

安装

终端依次输入以下命令,如果过程中没有错误提示,则redis安装完成。

sudo apt install lsb-release curl gpg
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

执行过程

ubuntu@ip-172-0-0-00:~$
ubuntu@ip-172-0-0-00:~$ sudo apt install lsb-release curl gpg
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
lsb-release is already the newest version (12.0-2).
curl is already the newest version (8.5.0-2ubuntu10.2).
gpg is already the newest version (2.4.4-2ubuntu17).
gpg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
ubuntu@ip-172-0-0-00:~$
ubuntu@ip-172-0-0-00:~$ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
ubuntu@ip-172-0-0-00:~$
ubuntu@ip-172-0-0-00:~$ 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
deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb noble main
ubuntu@ip-172-0-0-00:~$
ubuntu@ip-172-0-0-00:~$ sudo apt-get update
Hit:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble InRelease
Hit:2 http://cn-north-1.ec2.archive.ubuntu.com/ubuntu noble InRelease
Get:3 http://cn-north-1.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Hit:4 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:5 http://cn-north-1.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease
Get:6 https://packages.redis.io/deb noble InRelease [3854 B]
Get:7 https://packages.redis.io/deb noble/main amd64 Packages [1645 B]
Get:8 https://packages.redis.io/deb noble/main all Packages [747 B]
Ign:9 https://download.docker.com/linux/ubuntu focal InRelease
Ign:9 https://download.docker.com/linux/ubuntu focal InRelease
Ign:9 https://download.docker.com/linux/ubuntu focal InRelease
Err:9 https://download.docker.com/linux/ubuntu focal InRelease
  Cannot initiate the connection to download.docker.com:443 (2a03:2880:f131:83:face:b00c:0:25de). - connect (101: Network is unreachable) Could not connect to download.docker.com:443 (67.228.235.93), connection timed out
Fetched 132 kB in 37s (3549 B/s)
Reading package lists... Done
W: https://mirrors.aliyun.com/docker-ce/linux/ubuntu/dists/noble/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
W: Failed to fetch https://download.docker.com/linux/ubuntu/dists/focal/InRelease  Cannot initiate the connection to download.docker.com:443 (2a03:2880:f131:83:face:b00c:0:25de). - connect (101: Network is unreachable) Could not connect to download.docker.com:443 (67.228.235.93), connection timed out
W: Some index files failed to download. They have been ignored, or old ones used instead.
ubuntu@ip-172-0-0-00:~$
ubuntu@ip-172-0-0-00:~$ sudo apt-get install redis
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  redis-server redis-tools
Suggested packages:
  ruby-redis
The following NEW packages will be installed:
  redis redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.
Need to get 1633 kB of archives.
After this operation, 9595 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://packages.redis.io/deb noble/main amd64 redis-tools amd64 6:7.4.0-1rl1~noble1 [1554 kB]
Get:2 https://packages.redis.io/deb noble/main amd64 redis-server amd64 6:7.4.0-1rl1~noble1 [63.9 kB]
Get:3 https://packages.redis.io/deb noble/main amd64 redis all 6:7.4.0-1rl1~noble1 [15.3 kB]
Fetched 1633 kB in 3s (520 kB/s)
Selecting previously unselected package redis-tools.
(Reading database ... 98676 files and directories currently installed.)
Preparing to unpack .../redis-tools_6%3a7.4.0-1rl1~noble1_amd64.deb ...
Unpacking redis-tools (6:7.4.0-1rl1~noble1) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_6%3a7.4.0-1rl1~noble1_amd64.deb ...
Unpacking redis-server (6:7.4.0-1rl1~noble1) ...
Selecting previously unselected package redis.
Preparing to unpack .../redis_6%3a7.4.0-1rl1~noble1_all.deb ...
Unpacking redis (6:7.4.0-1rl1~noble1) ...
Setting up redis-tools (6:7.4.0-1rl1~noble1) ...
Setting up redis-server (6:7.4.0-1rl1~noble1) ...
Setting up redis (6:7.4.0-1rl1~noble1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Scanning processes...
Scanning candidates...
Scanning linux images...

Pending kernel upgrade!
Running kernel version:
  6.8.0-1010-aws
Diagnostics:
  The currently running kernel version is not the expected kernel version 6.8.0-1013-aws.

Restarting the system to load the new kernel will not be handled automatically, so you should consider rebooting.

Restarting services...

Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart networkd-dispatcher.service
 systemctl restart systemd-logind.service
 systemctl restart unattended-upgrades.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
ubuntu@ip-172-0-0-00:~$

2、修改配置

允许外网端访问

redis的配置文件在/etc/redis/redis.conf,打开这个文件

sudo vim /etc/redis/redis.conf
#注释下面内容
bind 127.0.0.1 -::1

#添加
bind 0.0.0.0

修改为no
protected-mode no

增加密码

 3、相关命令

启动方式一
/etc/init.d/redis-server start
启动方式二
systemctl start redis-server
启动方式三
service redis-server start
重启
service redis-server restart
关闭
service redis-server stop
查看状态
service redis-server status

操作记录

ubuntu@ip-172-0-0-00:/etc$ sudo /etc/init.d/redis-server start
Starting redis-server (via systemctl): redis-server.service.
ubuntu@ip-172-0-0-00:/etc$
ubuntu@ip-172-0-0-00:/etc$ service redis-server status
● redis-server.service - Advanced key-value store
     Loaded: loaded (/usr/lib/systemd/system/redis-server.service; disabled; preset: enabled)
     Active: active (running) since Tue 2024-08-13 11:46:55 UTC; 15min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 503818 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 6 (limit: 18951)
     Memory: 3.5M (peak: 4.1M)
        CPU: 2.882s
     CGroup: /system.slice/redis-server.service
             └─503818 "/usr/bin/redis-server 127.0.0.1:6379"

Aug 13 11:46:55 ip-172-0-0-00 systemd[1]: Starting redis-server.service - Advanced key-value store...
Aug 13 11:46:55 ip-172-0-0-00 systemd[1]: Started redis-server.service - Advanced key-value store.
ubuntu@ip-172-0-0-00:/etc$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值