欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

单机redis基本安装

程序员文章站 2022-07-05 10:18:30
...
单机redis基本安装

CentsOS 6.7 (Santiago)
Redis  3.2.11 64bit

-检测redis依赖包,参数
# rpm -qa | grep tcl
tcl-8.5.7-6.el6.x86_64
tcl-devel-8.5.7-6.el6.x86_64

vi /etc/security/limits.conf 最后增加

redis              soft    nofile  65536
redis              hard    nofile  65536 

vi /etc/pam.d/login文件最后增加
 
session    required     pam_limits.so

1.配置yum仓库,可以配本地或者阿里云的yum,不再详述

参考链接:http://blog.csdn.net/evils798/article/details/78401784

2.yum 安装gcc等依赖库

# yum install  -y gcc gcc-c++    

Installed:
  gcc.x86_64 0:4.4.7-18.el6                  
  gcc-c++.x86_64 0:4.4.7-18.el6                                                       

Dependency Installed:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6          
   cpp.x86_64 0:4.4.7-18.el6           
   libstdc++-devel.x86_64 0:4.4.7-18.el6           
   ppl.x86_64 0:0.10.2-11.el6          

Dependency Updated:
  libgcc.i686 0:4.4.7-18.el6             
  libgcc.x86_64 0:4.4.7-18.el6             
  libgomp.x86_64 0:4.4.7-18.el6             
  libstdc++.x86_64 0:4.4.7-18.el6            

Complete!

3.下载 3.2.11版本的redis  

# cd /usr/local/src/
# wget http://download.redis.io/releases/redis-3.2.11.tar.gz
--2017-11-08 18:30:18--  http://download.redis.io/releases/redis-3.2.11.tar.gz
Resolving download.redis.io... 109.74.203.151
Connecting to download.redis.io|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://111.1.62.113/cloud/111.1.50.15/files/516100000648EA55//download.redis.io/releases/redis-3.2.11.tar.gz [following]
--2017-11-08 18:30:18--  http://111.1.62.113/cloud/111.1.50.15/files/516100000648EA55//download.redis.io/releases/redis-3.2.11.tar.gz
Connecting to 111.1.62.113:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1550452 (1.5M) [application/octet-stream]
Saving to: “redis-3.2.11.tar.gz”

100%[=======================================================================>] 1,550,452   --.-K/s   in 0.1s    

2017-11-08 18:30:18 (10.5 MB/s) - “redis-3.2.11.tar.gz” saved [1550452/1550452]


3.下载完成后解压、编译、安装
# tar zxf redis-3.2.11.tar.gz  
# ls
redis-3.2.11  redis-3.2.11.tar.gz

# cd redis-3.2.11
# ls
00-RELEASENOTES  CONTRIBUTING  deps     Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             COPYING       INSTALL  MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests


4.编译安装redis,make之前用make test 检查是否存在错误

#make test
make[1]: Entering directory `/usr/local/src/redis-3.2.11/src'
39 seconds - unit/geo
  98 seconds - unit/type/list-3
  111 seconds - integration/replication-psync

\o/ All tests passed without errors!  --test测试成功,没有错误

Cleanup: may take some time... OK
make[1]: Leaving directory `/usr/local/src/redis-3.2.11/src'

5.编译安装
# make && make install
cd src && make all
make[1]: Entering directory `/usr/local/src/redis-3.2.11/src'
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/src/redis-3.2.11/src'
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-3.2.11/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-3.2.11/src'

6.查看redis进程,系统中无redis进程

# ps -ef | grep redis
root      9688  4443  0 18:35 pts/1    00:00:00 grep redis

7.创建相关文件夹

# mkdir -p /usr/local/redis
# mkdir -p /redis/data
# mkdir -p /usr/local/redis/bin

进入utils目录,使用install_server.sh脚本配置安装redis 
# cd utils/
# pwd
/usr/local/src/redis-3.2.11/utils
# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 7000                                          --指定端口 
Please select the redis config file name [/etc/redis/7000.conf] /usr/local/redis/redis_7000.conf        --指定conf配置文件目录
Please select the redis log file name [/var/log/redis_7000.log] /usr/local/redis/redis_7000.log           --指定log位置
Please select the data directory for this instance [/var/lib/redis/7000] /redis/data/                       --指定数据文件位置
Please select the redis executable path [/usr/local/bin/redis-server]   
Selected config:
Port           : 7000                                        
Config file    : /usr/local/redis/redis_7000.conf
Log file       : /usr/local/redis/redis_7000.log
Data dir       : /redis/data/
Executable     : /usr/local/bin/redis-server                      
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/7000.conf => /etc/init.d/redis_7000                     --生成的redis启动脚本redis_7000
Installing service...
Successfully added to chkconfig! 
Successfully added to runlevels 345!             
Starting Redis server...
Installation successful!

8.安装完成后查看redis进程
# netstat  -tpnl | grep 7000
tcp        0      0 127.0.0.1:7000              0.0.0.0:*                   LISTEN      9725/redis-server 1 

# /etc/init.d/redis_7000  status
Redis is running (9725)

9.增加redis用户来启动维护redis,  (-s 指定shell  -M 不创建HOME目录)

# useradd   redis -s  /sbin/nologin  -M

查看redis id

# id redis
uid=501(redis) gid=501(redis) groups=501(redis)

10.修改相关目录权限

# chown  -R redis.redis /usr/local/redis/
# ls -al /redis/

drwxr-xr-x.  2 root root 4096 Nov  8 18:36 data

# chown  -R redis.redis /redis/data/

# /etc/init.d/redis_7000 stop
Stopping ...
Redis stopped

11.修改启动脚本,增加切换用户redis 执行
# vi /etc/init.d/redis_7000 

注意以下几行变量的路径都要有redis用户的读写权限,否则在使用/etc/init.d/redis_7000脚本关闭redis服务的时候会报redis_7000.pid does not exist, process is not running错误,导致无法正常关闭redis

#!/bin/sh
EXEC=/usr/local/bin/redis-server             
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/usr/local/redis/redis_7000.pid     --指定pid文件路径,在/usr/local/redis/redis_7000.conf配置文件中的也要将pid文件的路径修改为/usr/local/redis/redis_7000.pid
CONF="/usr/local/redis/redis_7000.conf"    
REDISPORT="7000"

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
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

12.增加redis用户

在redis_7000脚本中的echo "Starting Redis server..."下面增加一段执行语句
                 sudo -u redis $EXEC $CONF --pidfile $PIDFILE
添加后如下:
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed"
        else
            echo "Starting Redis server..."
            sudo -u redis $EXEC $CONF --pidfile $PIDFILE
            
13.启动,关闭redis 测试相关功能          
# /etc/init.d/redis_7000 start
Starting Redis server...

# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
redis     9781  0.0  0.1 133524  7564 ?        Ssl  18:48   0:00 /usr/local/bin/redis-server 127.0.0.1:7000                                                    
root      9789  0.0  0.0 103304   900 pts/1    S+   18:49   0:00 grep redis

14.使用客户端工具登录测试redis(无密码验证模式)

[[email protected] redis]# redis-cli  -h  127.0.0.1 -p 7000

127.0.0.1:7000> ping
PONG

127.0.0.1:7000> echo hi
"hi"
127.0.0.1:7000> 
127.0.0.1:7000> 
127.0.0.1:7000> select 1
OK
127.0.0.1:7000[1]> INCR foo
(integer) 1
127.0.0.1:7000[1]> set bar 1
OK
127.0.0.1:7000[1]> get bar
"1"
关闭redis
# /etc/init.d/redis_7000 stop
Stopping ...
Redis stopped