…生活與工作…個人的生活雜記….
redis

QNAP Ubuntu安裝Redis

Redis 是一個in-memory 的keyvalue database,因此常常被用在需要快取(Cache)一些資料的場合,可以減輕許多後端資料庫的壓力。

redis
redis

在QNAP的軟體容器工作站中安裝Redis跟一般Ubuntu安裝一樣,只是必須修改一個地方,否則將無法啟動。

安裝步驟:

 apt-get install redis-server 
 apt-get install php-redis 
 /etc/init.d/apache2 restart

這樣就安裝完成了,但是在Docker的環境之下, Redis 卻是無法順利啟動。

sudo /etc/init.d/redis-server status

● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2020-11-26 14:59:11 CST; 4s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 6258 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 47 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 74 (code=exited, status=0/SUCCESS)

Nov 25 19:51:05 WordPress systemd[1]: Starting Advanced key-value store...
Nov 25 19:51:05 WordPress systemd[1]: Started Advanced key-value store.
Nov 26 14:59:11 WordPress systemd[1]: Stopping Advanced key-value store...
Nov 26 14:59:11 WordPress systemd[1]: Stopped Advanced key-value store.

Port 6379也沒有服務占用

netstat -anl |grep 6379

這時必須修改 redis.conf ,將 bind 127.0.0.1 ::1 修改為 bind 127.0.0.1

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1   <----修改這裡

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# TCP listen() backlog.

完成後重新啟動 Redis 即可

sudo /etc/init.d/redis-server start
[ ok ] Starting redis-server (via systemctl): redis-server.service.
sudo /etc/init.d/redis-server status
● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-11-25 19:51:05 CST; 17h ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 47 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 74 (redis-server)
   CGroup: /lxc/Wordpress/system.slice/redis-server.service
           └─74 /usr/bin/redis-server 127.0.0.1:6379

Nov 25 19:51:05 WordPress systemd[1]: Starting Advanced key-value store...
Nov 25 19:51:05 WordPress systemd[1]: Started Advanced key-value store.

查詢服務狀態

netstat -anl |grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:46592         127.0.0.1:6379          TIME_WAIT