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.
相关推荐
Gl�ria4 小时前
Redis 高可用架构对比
数据库·redis
Zenova EdgeOS6 小时前
Python 工业边缘 redis-py 异步实战
开发语言·redis·python
hanchenxing6 小时前
前端异步任务三方案:Celery vs Redis Stream vs BullMQ 实战对比消息队列
前端·数据库·redis·异步任务·方案对比
星间都市山脉11 小时前
Android16 SystemService.onBootPhase 调用时机
android·java·linux·windows·ubuntu
余生爱静11 小时前
从零搭建 SIP 全栈通话系统:SIP 协议 + PJSIP + Asterisk + Android 实战
ubuntu·pjsip
刃神太酷啦14 小时前
Redis 核心进阶:哨兵、集群、缓存问题与分布式锁详解----《Hello Redis!》(6)
linux·c语言·数据库·c++·redis·分布式·缓存
suweijie76815 小时前
Redis 7.0.2 Docker Compose 部署
redis·docker
爱和冰阔落16 小时前
【Linux】手写日志与固定线程池:任务队列、工作线程和安全退出
linux·运维·c++·redis·安卓
像风一样自由202016 小时前
29.Redis在大模型应用中有哪些用途缓存会话与限流
数据库·人工智能·redis·缓存·大模型·milvus·智能体
weixin_4604435620 小时前
考试监控大屏为什么不能直接查数据库?WebSocket、Redis与实时数据聚合架构设计
数据库·redis·在线考试系统·培训考试系统·煤矿培训考试系统·煤矿在线考试系统·宏远培训考试系统