阅读目录:
1. 关闭防火墙和Selinux
2. 下载并安装Redis 3.2.x版本
3. 启动并验证
4.配置Redis 3.2.x自启动
5.说明
1. 关闭防火墙和Selinux
Linux的防火墙是咱们新手的噩梦,很多情况会出现能ping通,但是访问不了Web页面。所以开始就干掉它!
1.1 关闭防火墙
[root@localhost ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
1.2 开机自动关闭防火墙
[root@localhost ~]# chkconfig iptables off
1.3 查看Selinux状态
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
1.4 关闭selinux
[root@localhost ~]# vi /etc/selinux/config
修改 SELINUX=disabled
注:永久开启->改成:SELINUX=enforcing
2. 下载并安装Redis 3.2.x版本
注:为了方便管理,创建一个文件夹专门放所需软件
[root@localhost /]# mkdir developer
[root@localhost /]# cd developer
2.1 安装redis编译组件
[root@localhost developer]# yum -y install gcc-c++ tcl
2.2 下载Redis 3.2.x安装包
[root@localhost developer]# wget http://download.redis.io/releases/redis-3.2.1.tar.gz
2.3 解压Redis 3.2.x
[root@localhost developer]# tar -zxvf redis-3.2.1.tar.gz
2.4 进入Redis文件夹
[root@localhost developer]# cd redis-3.2.1
2.5 编译
[root@localhost redis-3.2.1]# make
2.6 指定安装的路径
[root@localhost redis-3.2.1]# make PREFIX=/usr/local/redis-3.2.1 install
2.7 编辑配置文件
[root@localhost redis-3.2.1]# vim redis.conf
把daemonize no改为yes,意思是后台运行,如图
注:如果想用远程调用redis,找到 #bind 127.0.0.1,改为bind 0.0.0.0,如图
2.8 拷贝配置文件
[root@localhost redis-3.2.1]# cp redis.conf /usr/local/redis-3.2.1/
3. 启动并验证
3.1 启动redis客户端
[root@localhost redis-3.2.1]# cd /usr/local/redis-3.2.1/
[root@localhost redis-3.2.1]# ./bin/redis-server ./redis.conf
3.2 查看redis的启动
[root@localhost redis-3.2.1]# ps -ef | grep -i redis
注:关闭redis的操作是:
[root@localhost redis-3.2.1]# ./bin/redis-cli shutdown
3.3 启动redis命令行(服务端)
[root@localhost redis-3.2.1]# ./bin/redis-cli
127.0.0.1:6379> ping
127.0.0.1:6379> set name zhangsan
127.0.0.1:6379> get name
127.0.0.1:6379> keys *
注:以上是在redis中的操作。
4. 配置Redis 3.2.x自启动
4.1 创建编辑自启动文件
[root@localhost redis-3.2.1]# vim /etc/init.d/redis
#chkconfig: 2345 10 90
# description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
#客户端所在路径
EXEC=/usr/local/redis-3.2.1/bin/redis-server
#命令行所在路径
REDIS_CLI=/usr/local/redis-3.2.1/bin/redis-cli
PIDFILE=/var/run/redis.pid
#配置所在路径
CONF="/usr/local/redis-3.2.1/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
4.2 授予文件权限
[root@localhost redis-3.2.1]# chmod +x /etc/init.d/redis
4.3 设置自启动
[root@localhost redis-3.2.1]# chkconfig redis on
4.4 启动redis
[root@localhost redis-3.2.1]# service redis start
5. 说明
说明:本次使用
操作系统:CentOS 6.8 64位
Redis版本:3.2.1