图片 2

Redis的安装与使用

CentOS 6.5安装Redis-2.8.23

样例参考视频教程:

安装环境:

CentOS 6.5

Redisredis-2.8.23

http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53

下载安装:

1、切换至/usr/local/src/目录下:

[[email protected]
~]# cd /usr/local/src

下载源码包:

[[email protected]
src]# wget

2、解压:

[[email protected]
src]# tar xf redis-2.8.23.tar.gz

3、切换至redis-2.8.23目录下:

[[email protected]
src]# cd redis-2.8.23

4、安装redis至/usr/local/redis目录:

[[email protected]
redis-2.8.23]# make PREFIX=/usr/local/redis install

5、为redis提供SysV init脚本:

[[email protected]
redis-2.8.23]# vim /etc/rc.d/init.d/redis

#!/bin/sh

# chkconfig: 2345 83 93

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

#

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF=”/etc/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

;;

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

6、给脚本赋予执行权限:

[[email protected]
redis-2.8.23]# chmod u+x /etc/rc.d/init.d/redis

7、添加至服务管理列表,并让其开机自动启动:

[[email protected]
redis-2.8.23]# chkconfig –add /etc/rc.d/init.d/redis

[[email protected]
redis-2.8.23]# chkconfig redis on

8、创建redis持久化文件存放目录

mkdir -pv /var/redis/data

9、创建配置文件存放目录

[[email protected]
redis-2.8.23]# mkdir /etc/redis

10、提供配置文件

[[email protected]
redis-2.8.23]# cp /usr/local/src/redis-2.8.23/redis.conf /etc/redis/

11、编辑配置文件更改以下选项为如下值

[[email protected]
redis-2.8.23]# vim /etc/redis/redis.conf

daemonize yes#使Redis以守护进程模式运行

dir /var/redis/data #设置持久化文件存放位置,路径可自定义

12、启动服务

[[email protected]
redis-2.8.23]# service redis start

13、检查服务端口

[[email protected]
redis-2.8.23]# netstat -tunl | grep 6379

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN

14、修改PATH环境变量,让系统可以直接使用redis的相关命令:

[[email protected]
~]# echo ‘export PATH=/usr/local/redis/bin:$PATH’ >
/etc/profile.d/redis.sh

[[email protected]
~]# source /etc/profile.d/redis.sh

15、使用redis客户端工具连上redis进行测试:

[[email protected]
~]# redis-cli

127.0.0.1:6379> set hello world

OK

127.0.0.1:6379>

127.0.0.1:6379> get hello

“world”

127.0.0.1:6379> quit

[[email protected]
~]#

到此Redis的安装测试已经完成。

6.5安装Redis-2.8.23 安装环境: CentOS 6.5
Redisredis-2.8.23 下载安装: 1、切换至/usr/local/src/目录下:
[[email protected]
~]# cd /usr/local/src 下载源码包:…

IP:192.168.4.111

环境:CentOS 6.6

Redis版本:redis-3.0(考虑到Redis3.0在集群和性能提升方面的特性,rc版为正式版的候选版,而且很快就出正式版)

安装目录:/usr/local/redis

用户:root

编译和安装所需的包:

#yum install gcc
tcl

下载3.0版Redis(当前最新版redis-3.0.0-rc5.tar.gz,请学员们在安装时自行选用最新版)

#cd /usr/local/src

#wget
https://github.com/antirez/redis/archive/3.0.0-rc5.tar.gz

创建安装目录:

#mkdir /usr/local/redis

解压:

#tar -zxvf 3.0.0-rc5.tar.gz

#mv redis-3.0.0-rc5 redis3.0

#cd redis3.0

安装(使用PREFIX指定安装目录):

#make PREFIX=/usr/local/redis install

安装完成后,可以看到/usr/local/redis目录下有一个bin目录,bin目录里就是redis的命令脚本:

redis-benchmark  redis-check-aof  redis-check-dump  redis-cli 
redis-server

将Redis配置成服务:

按上面的操作步骤,Redis的启动脚本为:/usr/local/src/redis3.0/utils/redis_init_script

将启动脚本复制到/etc/rc.d/init.d/目录下,并命名为redis:

#cp /usr/local/src/redis3.0/utils/redis_init_script
/etc/rc.d/init.d/redis

编辑/etc/rc.d/init.d/redis,修改相应配置,使之能注册成为服务:

#vi /etc/rc.d/init.d/redis

#!/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

查看以上redis服务脚本,关注标为橙色的几个属性,做如下几个修改的准备:

(1)在脚本的第一行后面添加一行内容如下:

#chkconfig: 2345 80 90

(如果不添加上面的内容,在注册服务时会提示:service redis does not
support chkconfig)

(2)REDISPORT端口保持6379不变;(注意,端口名将与下面的配置文件名有关)

(3)EXEC=/usr/local/bin/redis-server改为EXEC=/usr/local/redis/bin/redis-server

(4)CLIEXEC=/usr/local/bin/redis-cli
改为CLIEXEC=/usr/local/redis/bin/redis-cli

(5)配置文件设置:

创建redis配置文件目录

#mkdir /usr/local/redis/conf

复制redis配置文件/usr/local/src/redis3.0/redis.conf到/usr/local/redis/conf目录并按端口号重命名为6379.conf

#cp /usr/local/src/redis3.0/redis.conf /usr/local/redis/conf/6379.conf

做了以上准备后,再对CONF属性作如下调整:

CONF=”/etc/redis/${REDISPORT}.conf”
改为CONF=”/usr/local/redis/conf/${REDISPORT}.conf”

(6)更改redis开启的命令,以后台运行的方式执行:

$EXEC $CONF&#“&”作用是将服务转到后面运行

修改后的/etc/rc.d/init.d/redis服务脚本内容为:

#!/bin/sh

#chkconfig: 2345 80 90

#

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid

CONF=”/usr/local/redis/conf/${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

以上配置操作完成后,便可将Redis注册成为服务:

#chkconfig –add redis

防火墙中打开对应的端口

#vi /etc/sysconfig/iptables

添加:

-A INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT

重启防火墙:

#service iptables restart

修改redis配置文件设置:

#vi /usr/local/redis/conf/6379.conf

修改如下配置

daemonize no 改为>daemonize yes

pidfile /var/run/redis.pid 改为>pidfile /var/run/redis_6379.pid

启动Redis服务

#service redis start

将Redis添加到环境变量中:

#vi /etc/profile

在最后添加以下内容:

## Redis env

export PATH=$PATH:/usr/local/redis/bin

使配置生效:

#source /etc/profile

现在就可以直接使用redis-cli等redis命令了:

图片 1

关闭Redis服务

#service redis stop

默认情况下,Redis开启安全认证,可以通过/usr/local/redis/conf/6379.conf的requirepass指定一个验证密码。

Redis的使用的Demo样例讲解与演示:

具体内容请参考样例代码和视频教程

关注公众号:

图片 2