redis安装配置-开启密码验证

redis安装

1) 下载redis最新稳定版本官网地址:https://redis.io/,我下载的是http://download.redis.io/releases/redis-3.2.8.tar.gz

2) 将redis-3.2.8.tar.gz上传至Linux(CentOs6.5 64位)的/home目录下;

3) 解压redis-3.2.8.tar.gz 

1 tar -xvf redis-3.2.8.tar.gz
2 cd redis-3.2.8
3 make PREFIX=/usr/local/redis install #安装到指定目录/usr/local/redis

 4) 复制脚本redis_init_script 到/etc/rc.d/init.d目录下,并改名为redis

1 cp /home/redis-3.2.8/utils/redis_init_script    /etc/rc.d/init.d/redis

5) 修改/etc/rc.d/init.d/redis,修改后的内容如下:

 1 #!/bin/sh
 2 #chkconfig:2345 80 90 #新增
 3 # Simple Redis init.d script conceived to work on Linux systems
 4 # as it does use of the /proc filesystem.
 5 
 6 REDISPORT=6379
 7 EXEC=/usr/local/redis/bin/redis-server
 8 CLIEXEC=/usr/local/redis/bin/redis-cli   #/usr/local/redis/bin为redis实际安装目录
 9 
10 PIDFILE=/var/run/redis_${REDISPORT}.pid
11 CONF="/etc/redis/${REDISPORT}.conf"
12 
13 case "$1" in
14     start)
15         if [ -f $PIDFILE ]
16         then
17                 echo "$PIDFILE exists, process is already running or crashed"
18         else
19                 echo "Starting Redis server..."
20                 $EXEC $CONF &  #启动后在后台运行redis
21         fi
22         ;;
23     stop)
24         if [ ! -f $PIDFILE ]
25         then
26                 echo "$PIDFILE does not exist, process is not running"
27         else
28                 PID=$(cat $PIDFILE)
29                 echo "Stopping ..."
30                 $CLIEXEC -p $REDISPORT shutdown
31                 while [ -x /proc/${PID} ]
32                 do
33                     echo "Waiting for Redis to shutdown ..."
34                     sleep 1
35                 done
36                 echo "Redis stopped"
37         fi
38         ;;
39     *)
40         echo "Please use start or stop as first argument"
41         ;;
42 esac

6) 拷贝配置文件

/etc/rc.d/init.d/redis配置文件中指定了CONF="/etc/redis/${REDISPORT}.conf";所以我们要创建/etc/redis文件夹,把redis的配置文件拷贝进来,并且命名为6379.conf

cp /home/redis-3.2.8/redis.conf   /etc/redis/6379.conf

7) 开启密码验证(如果不需要开启,可以略过)

vi /etc/redis/6379.conf  #找到requirepass foobare去掉前面的#号,foobare为密码,可以改为自己需要的,我改成了myredis

8) 增加为服务

chkconfig -add redis

9)启动redis服务

service redis start

10)登录redis

密码登录方式:①在客户端登录时给出密码 ②在客户端登录时不给密码,开始执行命令时给出密码。如下:

方式①
[root@huarui bin]# ./redis-cli -a myredis
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 

方式②
[root@huarui bin]# ./redis-cli 
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.  #没给出密码时执行keys * 报错
127.0.0.1:6379> auth myredis  #给出密码
OK
127.0.0.1:6379> keys *  #再次执行keys *  ,OK!
(empty list or set)
127.0.0.1:6379> 

 

11)关闭redis服务

因为开启了密码验证,不能使用service redis stop方式停止,会报错。如下:

1 [root@huarui bin]# service redis stop
2 Stopping ...
3 (error) NOAUTH Authentication required.
4 Waiting for Redis to shutdown ...
5 Waiting for Redis to shutdown ...

解决:登录redis执行shutdown命令

停止前:
[root@huarui bin]# ps -ef|grep redis
root       2472      1  0 14:47 pts/0    00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379      
root       2476   2371  0 14:47 pts/0    00:00:00 grep redis

开始停止操作:
[root@huarui bin]# ./redis-cli 
127.0.0.1:6379> auth myredis
OK
127.0.0.1:6379> shutdown 
2472:M 11 Apr 14:51:40.207 # User requested shutdown...
2472:M 11 Apr 14:51:40.207 * Saving the final RDB snapshot before exiting.
2472:M 11 Apr 14:51:40.240 * DB saved on disk
2472:M 11 Apr 14:51:40.240 * Removing the pid file.
2472:M 11 Apr 14:51:40.240 # Redis is now ready to exit, bye bye...
not connected> 

停止后:
[root@huarui bin]# ps -ef|grep redis
root       2536   2371  0 14:52 pts/0    00:00:00 grep redis

已经没有了redis进程

12)redis环境变量配置

修改profile文件:
#vi /etc/profile
在最后行添加

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

 

posted @ 2017-04-11 14:32  杯中红茶  阅读(314)  评论(0编辑  收藏  举报