说明:
操作系统:Centos 6.9
[root@localhost /]# cat /proc/version
Linux version 2.6.32-696.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) ) #1 SMP Tue Mar 21 19:29:05 UTC 201
以下几条命令对Redis的源码进行了编译。
[root@localhost ~]# wget http://download.redis.io/redis-stable.tar.gz
[root@localhost ~]# tar xzvf redis-stable.tar.gz
redis-stable/
redis-stable/INSTALL
[root@localhost ~]# cd redis-stable
[root@localhost redis-stable]# make
使用make test命令测试Redis是否编译正确,报出以下错误。
[root@localhost redis-stable]# make test
cd src && make test
make[1]: Entering directory `/root/redis-stable/src'
CC Makefile.dep
make[1]: Leaving directory `/root/redis-stable/src'
make[1]: Entering directory `/root/redis-stable/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-stable/src'
make: *** [test] Error 2
解决方案,安装tcl,下面命令是对tcl进行源码编译安装
[root@localhost ~]# wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
[root@localhost ~]# tar xzvf tcl8.6.1-src.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/tcl8.6.1/unix/
[root@localhost unix]# ./configure
[root@localhost unix]# make
[root@localhost unix]# make install
再次使用make test命令测试Redis是否编译正确,结果成功。
[root@localhost unix]# cd ~/redis-stable
[root@localhost redis-stable]# make test
最后make install命令进行安装
[root@localhost redis-stable]# make install
首先我们了解Redis包含的可执行文件如下图
我们在执行 make install 命令后,这些程序会被复制到 /usr/local/bin 目录内,所以我们在命令行直接输入程序名称即可执行。
[root@localhost redis-stable]# cd /usr/local/bin/
[root@localhost bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server tclsh8.6
[root@localhost bin]# redis-server
62530:C 26 Feb 23:38:03.190 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
62530:C 26 Feb 23:38:03.191 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=62530, just started
62530:C 26 Feb 23:38:03.191 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
62530:M 26 Feb 23:38:03.194 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 62530
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
62530:M 26 Feb 23:38:03.206 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
启动成功,这也说明了我们安装的没有问题。
Redis 服务器默认使用 6379 端口,可通过 --port 参数自定义端口号:
[root@localhost bin]# redis-server --port 6380
在Linux 系统中可以通过初始化脚本启动Redis,使Redis 能随系统自动运行,在生成环境中推荐使用此方法运行Redis。在Redis 源代码目录的 utils 文件夹中有名为 redis_init_script 的初始化脚本文件,内容如下:
[root@localhost utils]# cat redis_init_script
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
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"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
[root@localhost utils]#
我们需要配置Redis 的运行方式和持久化文件、日志文件的存储位置等,如下:
将初始化脚本复制到 /etc/init.d 目录中,文件名为redis_端口号,其中端口号表示让Redis监听的端口号,客户端通过该端口号连接Redis。然后修改脚本第12行的REDISPORT变量的值为同样的端口号。
[root@localhost utils]# cp redis_init_script /etc/init.d/redis_6379
[root@localhost utils]# ls /etc/init.d/redis_6379
/etc/init.d/redis_6379
/etc/redis 存放 Redis 配置文件
/var/redis/端口号 存放Redis 的持久化文件
将配置文件模板 redis.conf 复制到 /etc/redis 目录中 更名为6379.conf,然后按如下修改
现在就可以使用/etc/init.d/redis_端口号start 来启动Redis了,而后需要执行下面命令使Redis随系统自动启动:
[root@localhost etc]# ln -s /etc/init.d/redis_6379 /etc/rc0.d/k90redis
[root@localhost etc]# ln -s /etc/init.d/redis_6379 /etc/rc6.d/k90redis
[root@localhost etc]# ln -s /etc/init.d/redis_6379 /etc/rc5.d/S90redis
考虑到Redis有可能正将内存中的数据同步到硬盘中,强行终止Redis进程可能导致数据丢失。正确停止Redis方法为
[root@localhost /]# redis-cli SHUTDOWN
或
kill redis进程的PID,Redis可以妥善处理SIGTERM信号。