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.

你可能感兴趣的:(redis,ubuntu,数据库,服务,开发语言)