Ubuntu安装Redis

安装环境: Ubuntu 18.04 64位

一、安装

1.1 更新仓库(非必须)

复制代码
sudo apt update

1.2 使用 apt 从官方 Ubuntu 存储库来安装 Redis

复制代码
sudo apt-get install redis-server

二、设置密码

2.1 打开Redis配置文件redis.conf

复制代码
sudo vi /etc/redis/redis.conf

2.2 找到# requirepass foobared这一行,将注释符号#去掉,将后面修改成自己的密码,例如,设置密码为123abc

复制代码
requirepass 123abc

三、开启远程访问

默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

1、打开Redis服务器的配置文件redis.conf

复制代码
sudo vi /etc/redis/redis.conf

2、使用注释符号#注释bind 127.0.0.1这行

复制代码
#注释bind
#bind 127.0.0.1

四、Redis服务控制命令

复制代码
/etc/init.d/redis-server start     #启动
/etc/init.d/redis-server stop      #关闭
/etc/init.d/redis-server restart   #重启

五、连接测试

直接输入redis-cli通过默认客户端来测试连接,正常情况下返回ping的对应值PONG

复制代码
root@iZm5eetszs07500os8erolZ:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

六、注意

修改配置文件之后需要重启Redis服务

七、Ubuntu 18.04安装过程中可能出现的问题

执行安装命令后,安装失败,错误信息如下:

复制代码
root@iZm5eetszs07500os8erolZ:~# sudo apt-get install redis-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libjemalloc1 redis-tools
Suggested packages:
  ruby-redis
The following NEW packages will be installed:
  libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 78 not upgraded.
Need to get 0 B/634 kB of archives.
After this operation, 3,012 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Selecting previously unselected package libjemalloc1.
(Reading database ... 110076 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.6.0-11_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-11) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_5%3a4.0.9-1ubuntu0.2_amd64.deb ...
Unpacking redis-tools (5:4.0.9-1ubuntu0.2) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_5%3a4.0.9-1ubuntu0.2_amd64.deb ...
Unpacking redis-server (5:4.0.9-1ubuntu0.2) ...
Setting up libjemalloc1 (3.6.0-11) ...
Setting up redis-tools (5:4.0.9-1ubuntu0.2) ...
Setting up redis-server (5:4.0.9-1ubuntu0.2) ...
Job for redis-server.service failed because a timeout was exceeded.
See "systemctl status redis-server.service" and "journalctl -xe" for details.
invoke-rc.d: initscript redis-server, action "start" failed.
● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: timeout) since Sun 2020-02-02 17:07:24 CST; 12ms ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 14903 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)

Feb 02 17:07:24 iZm5eetszs07500os8erolZ systemd[1]: Failed to start Advanced key-value store.
dpkg: error processing package redis-server (--configure):
 installed redis-server package post-installation script subprocess returned error exit status 1
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.31) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Errors were encountered while processing:
 redis-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

7.1 原因

Ubuntu 18.04默认主机上禁用了IPv6,而Ubuntu的redis-server软件包(版本5:4.0.9-1)附带了:绑定127.0.0.1 :: 1

7.2 解决步骤:

1、卸载

复制代码
sudo apt-get purge --auto-remove redis-server

2、取消禁用ipv6,修改/proc/sys/net/ipv6/conf/eth0/disable_ipv6的状态即可

复制代码
vim /etc/sysctl.conf 


#net.ipv6.conf.all.disable_ipv6 = 1  #注释这条。
net.ipv6.conf.eth0.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1 

3、重新安装

复制代码
sudo apt-get install redis-server

4、重新安装后,Redis启动仍然失败,我们修改redis.conf配置文件,注释127.0.0.1 :: 1或改成127.0.0.1

复制代码
sudo vi /etc/redis/redis.conf


# 127.0.0.1 :: 1

5、启动Reids

复制代码
/etc/init.d/redis-server start

显示启动成功

复制代码
[ ok ] Starting redis-server (via systemctl): redis-server.service.
相关推荐
我是小灰灰吖3 小时前
QT在线安装器使用指南:从下载到配置完整教程
linux·qt·ubuntu
MIXLLRED5 小时前
解决——Ubuntu远程桌面指令更换网络
linux·网络·ubuntu
MC皮蛋侠客5 小时前
Redis 系列(一):全景与最小闭环——从 `SET` 命令到内存数据结构
数据结构·数据库·redis
我是小灰灰吖7 小时前
Ubuntu 22.04 + Qt 6.9.3 应用程序打包与部署完整指南
qt·ubuntu
渣渣盟9 小时前
当 Redis 写入成为性能瓶颈时,如何利用 异步批量 Sink 将吞吐量从 1w QPS 提升到 10w+?
数据库·redis·php
自律懒人10 小时前
AI 沙箱逃逸 4 连发:GPT-5.6 黑进 Hugging Face、Kimi K3 抄答案,5 步给本地 Agent 装围栏
ubuntu
MC皮蛋侠客11 小时前
Redis 系列(六):过期、内存淘汰与内存优化——让 Redis 活得更久
数据库·redis·bootstrap
老王IT13 小时前
VMware虚拟机安装的ubuntu忘记用户密码怎么办
ubuntu
我是小灰灰吖13 小时前
Ubuntu22.04,OpenCV 4.5.5 环境搭建
linux·qt·ubuntu
MC皮蛋侠客13 小时前
Redis 系列(七):事务、Pipeline 与 Lua 脚本——从原子性到 Functions
数据库·redis·lua