CentOS下redis安装使用

安装

查看官网http://redis.io/download

登录服务器后进入安装目录,自已选择

$ 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.conf

$ src/redis-server ./redis.conf

执行以下命令可以进入客户端

$ src/redis-cli

以上操作redis就安装完毕了,可以在本地使用。但如果需要供其他服务器访问要重新配置。

配置内外网可访问

发现其他服务器用jedis连接该redis时总是报拒绝连接的错误。

打开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

《CentOS下redis安装使用》

最后reboot重启服务器执行以下命令验证redis是否自启动

ps aux | grep redis
    原文作者:LikeDege
    原文地址: https://segmentfault.com/a/1190000006191535
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞