CentOS 7手动编译安装Redis 6.2

一般通过仓库直接安装的Redis版本都比较老,所以想要用新版本的软件还是得自己动手。安装了6.2版本的Redis,并使用systemd管理。踩了些坑,记录一下

References:

环境:CentOS 7

首先下载并解压源码包:

[root@localhost soft]# wget https://download.redis.io/releases/redis-6.2.1.tar.gz
--2021-04-01 14:33:20--  https://download.redis.io/releases/redis-6.2.1.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2438367 (2.3M) [application/octet-stream]
Saving to: ‘redis-6.2.1.tar.gz’

100%[================================================================================================================>] 2,438,367   6.61MB/s   in 0.4s   

2021-04-01 14:33:21 (6.61 MB/s) - ‘redis-6.2.1.tar.gz’ saved [2438367/2438367]

[root@localhost soft]# ls
redis-6.2.1.tar.gz
[root@localhost soft]# tar -zxf redis-6.2.1.tar.gz 
[root@localhost soft]# ls
redis-6.2.1  redis-6.2.1.tar.gz
[root@localhost soft]# 

如果没有编译工具,先安装一个gcc:

[root@localhost ~]# yum install gcc -y

然后编译,由于想要systemd的支持,所以用make USE_SYSTEMD=yes编译:

[root@localhost soft]# cd redis-6.2.1
[root@localhost redis-6.2.1]# ls
00-RELEASENOTES  CONDUCT       COPYING  INSTALL   MANIFESTO  redis.conf  runtest-cluster    runtest-sentinel  src    TLS.md
BUGS             CONTRIBUTING  deps     Makefile  README.md  runtest     runtest-moduleapi  sentinel.conf     tests  utils
[root@localhost redis-6.2.1]# make USE_SYSTEMD=yes
cd src && make all
make[1]: Entering directory `/root/soft/redis-6.2.1/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/soft/redis-6.2.1/src'
make[1]: Entering directory `/root/soft/redis-6.2.1/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
rm -f adlist.d quicklist.d ae.d anet.d dict.d server.d sds.d zmalloc.d lzf_c.d lzf_d.d pqsort.d zipmap.d sha1.d ziplist.d release.d networking.d util.d object.d db.d replication.d rdb.d t_string.d t_list.d t_set.d t_zset.d t_hash.d config.d aof.d pubsub.d multi.d debug.d sort.d intset.d syncio.d cluster.d crc16.d endianconv.d slowlog.d scripting.d bio.d rio.d rand.d memtest.d crcspeed.d crc64.d bitops.d sentinel.d notify.d setproctitle.d blocked.d hyperloglog.d latency.d sparkline.d redis-check-rdb.d redis-check-aof.d geo.d lazyfree.d module.d evict.d expire.d geohash.d geohash_helper.d childinfo.d defrag.d siphash.d rax.d t_stream.d listpack.d localtime.d lolwut.d lolwut5.d lolwut6.d acl.d gopher.d tracking.d connection.d tls.d sha256.d timeout.d setcpuaffinity.d monotonic.d mt19937-64.d anet.d adlist.d dict.d redis-cli.d zmalloc.d release.d ae.d crcspeed.d crc64.d siphash.d crc16.d monotonic.d cli_common.d mt19937-64.d ae.d anet.d redis-benchmark.d adlist.d dict.d zmalloc.d release.d crcspeed.d crc64.d siphash.d crc16.d monotonic.d cli_common.d mt19937-64.d
(cd ../deps && make distclean)
make[2]: Entering directory `/root/soft/redis-6.2.1/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(cd hdr_histogram && make clean) > /dev/null || true
(rm -f .make-*)
...
...
ar crus lib/libjemalloc.a src/jemalloc.o src/arena.o src/background_thread.o src/base.o src/bin.o src/bitmap.o src/ckh.o src/ctl.o src/div.o src/extent.o src/extent_dss.o src/extent_mmap.o src/hash.o src/hooks.o src/large.o src/log.o src/malloc_io.o src/mutex.o src/mutex_pool.o src/nstime.o src/pages.o src/prng.o src/prof.o src/rtree.o src/stats.o src/sz.o src/tcache.o src/ticker.o src/tsd.o src/witness.o
make[3]: Leaving directory `/root/soft/redis-6.2.1/deps/jemalloc'
make[2]: Leaving directory `/root/soft/redis-6.2.1/deps'
    CC adlist.o
    CC quicklist.o
    CC ae.o
    CC anet.o
    CC dict.o
    CC server.o
In file included from server.c:30:0:
server.h:55:31: fatal error: systemd/sd-daemon.h: No such file or directory
 #include <systemd/sd-daemon.h>
                               ^
compilation terminated.
make[1]: *** [server.o] Error 1
make[1]: Leaving directory `/root/soft/redis-6.2.1/src'
make: *** [all] Error 2
[root@localhost redis-6.2.1]# 

嗯,报错了,正常,解决它。看报错的部分提示#include <systemd/sd-daemon.h>,去GitHub的README中找找,发现需要安装依赖:

To build with systemd support, you'll need systemd development libraries (such as libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS)

要使用systemd支持进行build,需要systemd开发库(Debian/Ubuntu上是libsystemd-dev,CentOS上是systemd-devel)

那就装一下:

[root@localhost redis-6.2.1]# yum search systemd-devel
...
systemd-devel.i686 : Development headers for systemd
systemd-devel.x86_64 : Development headers for systemd
...
[root@localhost redis-6.2.1]# yum install systemd-devel -y
...

然后重新编译,在编译前需要清理一下刚刚失败的时候产生的缓存,不然会一直编译失败,在README中也能找到清理的命令:

$ make distclean

接着再编译,不出意外就可以编译成功了

$ make USE_SYSTEMD=yes

...

    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/root/soft/redis-6.2.1/src'
[root@localhost redis-6.2.1]# 

然后在src目录下就会多几个编译好的可执行文件:

[root@localhost src]# pwd
/root/soft/redis-6.2.1/src
[root@localhost src]# ll redis-*
-rwxr-xr-x. 1 root root 4833376 Apr  1 14:49 redis-benchmark
-rw-rw-r--. 1 root root   76055 Mar  2 14:14 redis-benchmark.c
-rw-r--r--. 1 root root     408 Apr  1 14:49 redis-benchmark.d
-rw-r--r--. 1 root root  852424 Apr  1 14:49 redis-benchmark.o
-rwxr-xr-x. 1 root root 9451088 Apr  1 14:49 redis-check-aof
-rw-rw-r--. 1 root root    7175 Mar  2 14:14 redis-check-aof.c
-rw-r--r--. 1 root root     444 Apr  1 14:49 redis-check-aof.d
-rw-r--r--. 1 root root   30296 Apr  1 14:49 redis-check-aof.o
-rwxr-xr-x. 1 root root 9451088 Apr  1 14:49 redis-check-rdb
-rw-rw-r--. 1 root root   14745 Mar  2 14:14 redis-check-rdb.c
-rw-r--r--. 1 root root     444 Apr  1 14:49 redis-check-rdb.d
-rw-r--r--. 1 root root   83608 Apr  1 14:49 redis-check-rdb.o
-rwxr-xr-x. 1 root root 5003392 Apr  1 14:49 redis-cli
-rw-rw-r--. 1 root root  314541 Mar  2 14:14 redis-cli.c
-rw-r--r--. 1 root root     363 Apr  1 14:49 redis-cli.d
-rw-r--r--. 1 root root 1091824 Apr  1 14:49 redis-cli.o
-rwxr-xr-x. 1 root root 9451088 Apr  1 14:49 redis-sentinel
-rwxr-xr-x. 1 root root 9451088 Apr  1 14:49 redis-server
-rwxrwxr-x. 1 root root    3600 Mar  2 14:14 redis-trib.rb
[root@localhost src]# 

直接运行./redis-server 就能运行Redis了:

[root@localhost src]# ./redis-server 
61670:C 01 Apr 2021 14:54:19.300 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
61670:C 01 Apr 2021 14:54:19.300 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=61670, just started
61670:C 01 Apr 2021 14:54:19.300 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
61670:M 01 Apr 2021 14:54:19.300 * Increased maximum number of open files to 10032 (it was originally set to 1024).
61670:M 01 Apr 2021 14:54:19.300 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 61670
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

61670:M 01 Apr 2021 14:54:19.301 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
61670:M 01 Apr 2021 14:54:19.301 # Server initialized
61670:M 01 Apr 2021 14:54:19.301 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
61670:M 01 Apr 2021 14:54:19.302 * Ready to accept connections

这些编译好的文件总不能和源码放一起用吧,需要放我们指定的目录,这里以/opt/redis为例,将可执行文件复制过去并查看:

[root@localhost redis-6.2.1]# make PREFIX=/opt/redis install
cd src && make install
make[1]: Entering directory `/root/soft/redis-6.2.1/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/soft/redis-6.2.1/src'
make[1]: Entering directory `/root/soft/redis-6.2.1/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/root/soft/redis-6.2.1/src'
[root@localhost redis-6.2.1]# ls /opt/redis/
bin
[root@localhost redis-6.2.1]# ls /opt/redis/bin/
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server
[root@localhost redis-6.2.1]# 

然后就是用systemd管理Redis了,在Ubuntu和Debian系统下可以使用utils目录下的install_server.sh脚本安装,可惜我用的是CentOS,只能自己动手了。首先尝试运行一下这个脚本

[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
[root@localhost utils]# 

虽然不支持,但是提示我们目录下有现成的配置文件,改改就能用。哎呀,Ctrl CV嘛,这个我熟。将Systemd的配置改改文件路径,最终如下所示:

[root@localhost utils]# cp systemd-redis_server.service /etc/systemd/system/redis_6379.service
[root@localhost utils]# vim /etc/systemd/system/redis_6379.service 

  1 # example systemd service unit file for redis-server
  2 #
  3 # In order to use this as a template for providing a redis service in your
  4 # environment, _at the very least_ make sure to adapt the redis configuration
  5 # file you intend to use as needed (make sure to set "supervised systemd"), and
  6 # to set sane TimeoutStartSec and TimeoutStopSec property values in the unit's
  7 # "[Service]" section to fit your needs.
  8 #
  9 # Some properties, such as User= and Group=, are highly desirable for virtually
 10 # all deployments of redis, but cannot be provided in a manner that fits all
 11 # expectable environments. Some of these properties have been commented out in
 12 # this example service unit file, but you are highly encouraged to set them to
 13 # fit your needs.
 14 #
 15 # Please refer to systemd.unit(5), systemd.service(5), and systemd.exec(5) for
 16 # more information.
 17 
 18 [Unit]
 19 Description=Redis data structure server
 20 Documentation=https://redis.io/documentation
 21 #Before=your_application.service another_example_application.service
 22 #AssertPathExists=/var/lib/redis
 23 Wants=network-online.target
 24 After=network-online.target
 25 
 26 [Service]
 27 #ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no
 28 ## Alternatively, have redis-server load a configuration file:
 29 ExecStart=/opt/redis/bin/redis-server /etc/redis/redis_6379.conf
 30 ExecStop=/opt/redis/bin/redis-cli -p 6379 shutdown
 31 LimitNOFILE=10032
 32 NoNewPrivileges=yes
 33 #OOMScoreAdjust=-900
 34 #PrivateTmp=yes
 35 Type=notify
 36 TimeoutStartSec=90
 37 TimeoutStopSec=90
 38 UMask=0077
 39 #User=redis
 40 #Group=redis
 41 WorkingDirectory=/var/lib/redis_6379
 42 
 43 [Install]
 44 WantedBy=multi-user.target

将上面配置里的一些目录和文件搞一搞:

[root@localhost redis-6.2.1]# cp ./redis.conf /etc/redis/redis_6379.conf
[root@localhost redis-6.2.1]# mkdir /var/lib/redis_6379
[root@localhost redis-6.2.1]# 

再改改配置文件:redis_6379.conf

   1 # Redis configuration file example.
   2 #
   3 # Note that in order to read the configuration file, Redis must be
   4 # started with the file path as first argument:
   5 #
   6 # ./redis-server /path/to/redis.conf

...

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

...

 242 ################################# GENERAL #####################################
 243 
 244 # By default Redis does not run as a daemon. Use 'yes' if you need it.
 245 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
 246 # When Redis is supervised by upstart or systemd, this parameter has no impact.
 247 daemonize no
 248 
 249 # If you run Redis from upstart or systemd, Redis can interact with your
 250 # supervision tree. Options:
 251 #   supervised no      - no supervision interaction
 252 #   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
 253 #                        requires "expect stop" in your upstart job config
 254 #   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
 255 #                        on startup, and updating Redis status on a regular
 256 #                        basis.
 257 #   supervised auto    - detect upstart or systemd method based on
 258 #                        UPSTART_JOB or NOTIFY_SOCKET environment variables
 259 # Note: these supervision methods only signal "process is ready."
 260 #       They do not enable continuous pings back to your supervisor.
 261 #
 262 # The default is "no". To run under upstart/systemd, you can simply uncomment
 263 # the line below:
 264 #
 265 # supervised auto
 266 supervised systemd

...

 290 # Specify the log file name. Also the empty string can be used to force
 291 # Redis to log on the standard output. Note that if you use standard
 292 # output for logging but daemonize, logs will be sent to /dev/null
 293 logfile "/var/log/redis_6379.log"
 294 

...

重点是将supervised设置为systemd,不然起不来。有一点注意一下,上文中提到:当Redis由upstart或systemd管理时,daemonize参数就没有影响了。logfile为空字符串时,日志将输出到标准输出。其他的配置根据需求改即可。
启动:

[root@localhost redis-6.2.1]# systemctl start redis_6379
[root@localhost redis-6.2.1]# systemctl status redis_6379
● redis_6379.service - Redis data structure server
   Loaded: loaded (/etc/systemd/system/redis_6379.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-04-01 15:28:23 CST; 6s ago
     Docs: https://redis.io/documentation
 Main PID: 72131 (redis-server)
   Status: "Ready to accept connections"
   CGroup: /system.slice/redis_6379.service
           └─72131 /opt/redis/bin/redis-server 127.0.0.1:6379

Apr 01 15:28:23 localhost.localdomain systemd[1]: Starting Redis data structure server...
Apr 01 15:28:23 localhost.localdomain systemd[1]: Started Redis data structure server.
[root@localhost redis-6.2.1]# ps -ef | grep redis
root      72131      1  0 15:28 ?        00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6379
root      72138  53367  0 15:28 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis-6.2.1]#

由于客户端没有配置环境变量,先配下环境变量:
由于之前没有配置过环境变量,所以直接在profile文件最后追加两行:

REDIS_HOME=/opt/redis/
PATH=$PATH:$REDIS_HOME/bin

操作过程如下:

[root@localhost redis-6.2.1]# vim /etc/profile
[root@localhost redis-6.2.1]# source /etc/profile
[root@localhost redis-6.2.1]# redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

然后检查Redis的RDB文件和日志文件:

[root@localhost ~]# ll /var/lib/redis_6379/dump.rdb 
-rw-------. 1 root root 92 Apr  1 15:33 /var/lib/redis_6379/dump.rdb
[root@localhost ~]# ll /var/log/redis_6379.log 
-rw-------. 1 root root 1458 Apr  1 15:33 /var/log/redis_6379.log
[root@localhost ~]# 

最后:
虽然官方写了make USE_SYSTEMD=yes可以获得sytemd支持,但是我直接make编译也能使用systemd管理,不知道这两种方式编译的区别在哪,有大佬知道麻烦告知一下。

标签: none

添加新评论

ali-01.gifali-58.gifali-09.gifali-23.gifali-04.gifali-46.gifali-57.gifali-22.gifali-38.gifali-13.gifali-10.gifali-34.gifali-06.gifali-37.gifali-42.gifali-35.gifali-12.gifali-30.gifali-16.gifali-54.gifali-55.gifali-59.gif

加载中……