4、centos安装Redis
1、下载安装包并解压
wget http://download.redis.io/releases/redis-5.0.3.tar.gz tar -zxvf redis-5.0.3.tar.gz -C /usr/local/redis/
2、安装
安装 redis 到 /usr/local/redis
make PREFIX=/usr/local/redis install 注:如果出现错误 1、没有安装gcc,这会导致我们无法make成功。使用yum安装: yum -y install gcc 2、jemalloc重载了Linux下的ANSI C的malloc和free函数。解决办法:make时添加参数。 make MALLOC=libc
3、启动redis
/usr/local/redis/bin/redis-server
这样启动的 redis 是默认形式,只能在前台运行,关闭命令窗口就会结束 redis。
现在需要把 redis 改成可以在后台运进
复制一份redis 配置文件 redis.conf 到 /usr/local/redis/bin/
cp redis.conf /usr/local/redis/bin/
将redis改为以守护进程方式运行
修改 redis 配置文件 cd /usr/local/redis/bin/ vim redis.conf 查找daemonize no 改为以守护进程方式运行 daemonize yes
启动 redis 并指定配置文件
./redis-server redis.conf
开启客户端
./redis-cli
4、配置 redis 开机自启
将redis_init_script拷贝到/etc/init.d目录下并重命名为 redis
cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis cd /etc/init.d [root init.d]#ls bcm-agent functions hosteye mysqld netconsole network README redis 查看一下 redis 这个文件 [root init.d]# cat redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
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
这个 shell 脚本中这五个参数十分重要 REDISPORT=6379 //redis的端口号 EXEC=/usr/local/bin/redis-server //redis服务位置 CLIEXEC=/usr/local/bin/redis-cli //redis 客户位置 PIDFILE=/var/run/redis_${REDISPORT}.pid //这个文件是 redis 启动后自动生成的,可用于检测 redis 是否启动。不能修改
CONF="/etc/redis/${REDISPORT}.conf" //redis 配置文件位置
对比我的配置可以看出 EXEC、CLIEXEC 和 CONF 这三个参数要修改: EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli CONF=/usr/local/redis/bin/redis.conf
vim 修改并保存
[root init.d]# chkconfig --add redis [root init.d]# chkconfig bcm-agent 0:关 1:关 2:开 3:开 4:开 5:开 6:关 hosteye 0:关 1:关 2:开 3:开 4:开 5:开 6:关 mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关 redis 0:关 1:关 2:开 3:开 4:开 5:开 6:关 重启 Linux [root init.d]# reboot
查看 redis-server 进程是否存在 [root ~]# ps -ef | grep redis root 1068 1 0 15:42 ? 00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:6379 root 6770 6485 0 15:58 pts/0 00:00:00 grep --color=auto redis 关闭 redis 服务 [root ~]# service redis stop Stopping ... Redis stopped [root ~]# ps -ef | grep redis root 10114 6485 0 16:01 pts/0 00:00:00 grep --color=auto redis [root ~]#

浙公网安备 33010602011771号