前言: 虽然我们可以通过,类似 yum, apt-get 等包管理工具安装 redis,但是仍有一些弊端,这一篇我们介绍一下,如何通过编译源码的方式来安装 redis(本文非原创,转自 https://fqk.io/redis-installation,非特意标注的皆为原创)
包管理工具安装 redis 的劣势
- 不同的包管理工具,安装的规则不同,譬如,安装路径,默认配置等
- 包管理工具只会安装基础的功能,不方便进行功能扩展(譬如安装第三方模块)
- 包管理工具自带的版本通常比较比官网滞后,无法安装最新版本
源码安装 redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| # wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make
# sudo cp src/redis-server /usr/local/bin/ sudo cp src/redis-cli /usr/local/bin/
# sudo mkdir /etc/redis
# sudo mkdir /var/redis
# sudo cp redis.conf /etc/redis/6379.conf
# sudo mkdir /var/redis/6379
|
配置 redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| sudo vi /etc/redis/6379.conf
daemonize => yes pidfile => /var/run/redis_6379.pid port => 6379 logfile => /var/log/redis_6379.log dir => /var/redis/6379
bind 127.0.0.1 rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command CONFIG "" rename-command EVAL ""
|
启动脚本
编写启动脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| echo '#!/bin/sh # # Provides: redis_6379 # Required-Start: $syslog $remote_fs # Required-Stop: $syslog $remote_fs # Should-Start: $local_fs # Should-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: redis_6379 - Persistent key-value db # Description: redis_6379 - Persistent key-value db #
REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" ' > ./redis_6379
cat utils/redis_init_script.tpl >> ./redis_6379 sudo chmod +x ./redis_6379
|
部署启动脚本
1 2 3 4 5
| ## For Linux sudo cp ./redis_6379 /etc/init.d/
## For OS X,因为没有 init.d 目录,故放在 /etc/redis 下 sudo cp ./redis_6379 /etc/redis/redis_6379
|
Linux
开机启动
1 2 3 4 5
| echo "/etc/init.d/redis_6379 start" | sudo tee --append /etc/rc.d/rc.local
sudo update-rc.d redis_6379 defaults
|
运行和停止
1 2 3 4 5
| # sudo /etc/init.d/redis_6379 start
# sudo /etc/init.d/redis_6379 stop
|
OS X
开机启动
1
| sudo vi /Library/LaunchDaemons/io.redis.6379.plist
|
内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>io.redis.6379</string> <key>ProgramArguments</key> <array> <string>/etc/redis/redis_6379</string> <string>start</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
|
启动项
1 2 3 4 5
| # sudo launchctl load -w /Library/LaunchDaemons/io.redis.6379.plist
# sudo launchctl unload -w /Library/LaunchDaemons/io.redis.6379.plist
|
运行和停止
1 2 3 4 5
| # sudo /etc/redis/redis_6379 start
# sudo /etc/redis/redis_6379 stop
|