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
相关文章
相关标签/搜索