Linux_CentOS7下redis搭建过程记录
1、下载
下载链接:
http://download.redis.io/releases/
2、安装
tar ......
mv redis-xxx redis
cd redis
make
make install //可选
注意:Centos 下make 的时候如果报错多半是gcc没有安装
yum -y install gcc
启动服务器:
src/redis-server
查看是否成功启动:
$ps -ef |grep redis
配置后台启动:
vim /usr/local/redis/redis.conf
修改如下内容:
daemonize yes
后台启动
src/redis-server /usr/local/redis/redis.conf
说明:
/user/local/redis/redis.conf 是手动加载指定配置文件,实现后台启动,默认不手动加载的话会自动加载程序内部默认配置,不会读取这个配置文件
3、开机自启动
1) 创建脚本:
方法1
用redis自带脚本,脚本只支持 start|stop 命令
cp/usr/local/redis/utils/redis_init_script /etc/rc.d/init.d/redis //redis本身已经有写好的脚本
vim /etc/rc.d/init.d/redis
修改如下相应内容:
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis.conf"
方法2(推荐):
自定义脚本,支持start|restart|stop
vim /etc/rc.d/init.d/redis
内容:
#!/bin/sh
###########################
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
REDIS_CLI=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis.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
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
##############################
问题:redis.pid 找不到
redis.conf配置文件中指定的pid路径地址,这里说明一下,在 redis.conf配置文件中需要将 daemonize这个参数项设置为 yes才会在redis启动时生成pid文件,很多新人不知道,没有生成pid文件,所以脚本里根据pid文件关闭redis就失败
开启服务后到 /var/run 下看有没有相关pid, 核实名字跟配置是否一致
2)加入开机自启动(CentOS7)
vim /lib/systemd/system/redis.service
内容如下:
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/redis start
ExecReload=/etc/init.d/redis restart
ExecStop=/etc/init.d/redis stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
4、客户端连接
添加环境变量:
ln -s /usr/local/redis/src/redis-cli /usr/local/bin/
连接:
redis-cli
相关文章:
Linux_CentOS7自定义开机自启动