CentOS下redis安裝使用

安裝

查看官網http://redis.io/downloadredis

登陸服務器後進入安裝目錄,自已選擇vim

$ cd /home

再執行如下命令,可根據須要選擇版本下載安全

$ wget http://download.redis.io/releases/redis-3.2.2.tar.gz

解壓編譯服務器

$ tar xzf redis-3.2.2.tar.gz #解壓
$ cd redis-3.2.2 #進入主目錄
$ make #編譯

啓動redis網絡

$ src/redis-server

此時會報沒有指定配置文件的錯誤,應該指定redis.confui

$ src/redis-server ./redis.conf

執行如下命令能夠進入客戶端this

$ src/redis-cli

以上操做redis就安裝完畢了,能夠在本地使用。但若是須要供其餘服務器訪問要從新配置。加密

配置內外網可訪問

發現其餘服務器用jedis鏈接該redis時老是報拒絕鏈接的錯誤。spa

打開redis.conf配置文件,找到bind 127.0.0.1,這裏默認是隻接收本地請求,能夠將其註釋或者指定爲redis所在服務器的內外網ip。修改後重啓redis。命令行

關於bind的配置網上有些解釋是錯誤的,說是訪問redis的請求來源ip,以此進行限制,結果我折騰半天。其實否則,能夠查看英文解釋。

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

文中提到的interface,是指網絡接口,服務器的網卡ip,例如設置爲bind 192.168.1.2,那麼redis只從該網卡地址接受外部請求。

設置密碼

redis默認是不須要密碼的,爲進一步增強安全配置,咱們能夠自已設置。

打開redis.conf配置文件,找到requirepass,去掉註釋並在後面添加密碼。保存重啓redis。

requirepass test123

或者能夠經過redis命令行界面進行修改。

$ src/redis-cli
redis 127.0.0.1:6379> config set requirepass test123

將redis設置爲開機自啓動的系統服務

首先修改redis.conf中的配置,將daemonize改成yes。

在/etc/init.d目錄下新建redis文件

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=/home/redis-3.2.2/src/redis-server  #根據安裝目錄而定 
REDIS_CLI=/home/redis-3.2.2/src/redis-cli  #根據安裝目錄而定
 
PIDFILE=/var/run/redis.pid   
CONF="/home/redis-3.2.2/src/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 exists, process is not running."  
                else  
                        PID=$(cat $PIDFILE)   
                        echo "Stopping..."  
                       $REDIS_CLI -p $REDISPORT  SHUTDOWN    
                        sleep 2  
                       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

設置權限

chmod 755 redis

設置開機自啓動

chkconfig redis on

執行

chkconfig --list

能夠看到redis有4個級別被設置爲on

圖片描述

最後reboot重啓服務器執行如下命令驗證redis是否自啓動

ps aux | grep redis
相關文章
相關標籤/搜索