ubuntu 18.04 安装 redis 时出错

huazi 2020-03-06 PM 411℃ 0条

ubuntu 18.04 安装 redis 时,出现了报错:

Do you want to continue? [Y/n] Y
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 Fri 2020-03-06 15:53:54 CST; 9ms ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)

Mar 06 15:53:54 huazi systemd[1]: redis-server.service: Start operation timed out. Terminating.
Mar 06 15:53:54 huazi systemd[1]: redis-server.service: Failed with result 'timeout'.
Mar 06 15:53:54 huazi 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
dpkg: dependency problems prevent configuration of redis:
 redis depends on redis-server (<< 5:4.0.9-1ubuntu0.2.1~); however:
  Package redis-server is not configured yet.
 redis depends on redis-server (>= 5:4.0.9-1ubuntu0.2); however:
  Package redis-server is not configured yet.

dpkg: error processing package redis (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 redis-server
 redis

网上查了资料说是 系统默认禁用了 ipv6

取消禁用 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 

然后执行

sysctl -p

让设置生效,再重新安装 redis 即可。

apt install redis-server

如果还是启动不了,就去修改 redis 的配置:

vim /etc/redis/redis.conf

找到

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 ::1

注释掉 bind 127.0.0.1 ::1 或者 改为 bind 127.0.0.1

然后重启 redis :

service redis start

查看 redis 的状态 :

service redis status

发现有异常信息:

Mar 06 15:58:26 huazi systemd[1]: redis-server.service: Can't open PID file /var/run/redis/redis-server.pid (yet?) after start: No such file or directory

● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-03-06 15:58:26 CST; 7min ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
 Main PID: 23859 (redis-server)
    Tasks: 4 (limit: 2340)
   CGroup: /system.slice/redis-server.service
           └─23859 /usr/bin/redis-server *:6379

Mar 06 15:58:26 huazi systemd[1]: redis-server.service: Service hold-off time over, scheduling restart.
Mar 06 15:58:26 huazi systemd[1]: redis-server.service: Scheduled restart job, restart counter is at 10.
Mar 06 15:58:26 huazi systemd[1]: Stopped Advanced key-value store.
Mar 06 15:58:26 huazi systemd[1]: Starting Advanced key-value store...
Mar 06 15:58:26 huazi systemd[1]: redis-server.service: Can't open PID file /var/run/redis/redis-server.pid (yet?) after start: No such file or directory
Mar 06 15:58:26 huazi systemd[1]: Started Advanced key-value store.

解决办法如下:

vim /etc/systemd/system/redis.service

[Service]

ExecStop=/bin/kill -s TERM $MAINPID

下面添加一行

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

添加后为:

[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"
PIDFile=/var/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis

最后重启 redis 。

systemctl daemon-reload
service redis restart
service redis status

结果:

● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-03-06 16:28:30 CST; 4s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 24615 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 24620 ExecStartPost=/bin/sh -c echo $MAINPID > /var/run/redis/redis.pid (code=exited, status=0/SUCCESS)
  Process: 24618 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 24619 (redis-server)
    Tasks: 4 (limit: 2340)
   CGroup: /system.slice/redis-server.service
           └─24619 /usr/bin/redis-server *:6379

Mar 06 16:28:30 huazi systemd[1]: Stopped Advanced key-value store.
Mar 06 16:28:30 huazi systemd[1]: Starting Advanced key-value store...
Mar 06 16:28:30 huazi systemd[1]: Started Advanced key-value store.

状态正常。

连接 redis 成功:

redis-cli
127.0.0.1:6379>

参考:

https://blog.csdn.net/zhangpeterx/article/details/104093275

标签: Ngixn, Redis, Ubuntu

非特殊说明,本博所有文章均为博主原创。

评论啦~