redis连接与关闭

hello, 今天学习过程中需要使用redis,在使用过程中遇到了一些问题,在这里记录下,也希望给遇到相同问题的人一点帮助, ok开始描述今天遇到的问题和解决办法。

首页redis在linux的安装,请参考网站:点击打开链接 

http://www.redis.net.cn/tutorial/3503.html 这个是网站上安装的版本过于老旧,目前最新版是Redis 4.0.6,而我安装的是3.2.8。因为我早就安装了redis,刚好要用,我就没有再去安装最新版。

$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar xzf redis-3.2.8.tar.gz
$ cd redis-3.2.8
$ make

启动什么的上面给出的网站有讲过,我就不再赘述了,我要说些网站上没有的东西,一般启动我们会以配置文件(redis.conf)的方式来启动, make之后在redis-3.2.8文件夹下会产生redis.conf文件,这里有几个参数需要说明下

1、daemonize no:  Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程。

2、port 6379 : 这个我就是端口号,如果不想用默认的可以修改这个参数

3、bind 127.0.0.1: 绑定ip 127.0.0.1 只能本机访问,数据库来说至少公司内网都应该可能访问的,我改为0.0.0.0 表示所有网段都可以访问

4、requirepass 123456:  设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH <password>命令提供密码,默认关闭

更多参数请参考redis社区文档:点击打开链接

如果我没猜错的话,现在你应该已经启动了redis-server ,那么想再次启动你要怎么做呢?  redis-server restart? 哈哈哈,我这样试了下。

好吧,严肃点,redis提供了服务器命令,向下面这样,先关闭在重新启动

[root@localhost src]# redis-cli  #开始客户端连接
127.0.0.1:6379> auth 123456 #auth 密码登录
OK
127.0.0.1:6379> shutdown save    #shutdown 关闭服务  save
not connected> exit              # not connected 表示连接已经失去, exit 退出
[root@localhost src]# 
[root@loca [root@localhost src]# ps -ef | grep redis-   # ps 查找进程 redis-server 真的关闭了
root      2782  2508  0 05:57 pts/0    00:00:00 grep --color=auto redis-
[root@localhost src]# redis-server ../redis.conf  #重启

好的,如果只说到这里就太没意思了,我们介绍下 utils 下的install_server.sh 这个是启动redis系统服务的脚本,系统服务怎么理解呢? 果然你很懂操作系统自然明白,但是如果你不懂的话,看一下windows任务管理器的服务,这个服务是可以右键启动和关闭的,install_server.sh 就是在linux系统上创建这个任务,然后可以通过systemctl stop/start命令启动和关闭。


下面是执行脚本, 里面有几个需要说明的步骤,在脚本执行过程中会让你选择在启动端口,配置文件路径,log文件路径,和数据文件路径,port可以用默认的,也可以指定,配置文件路径就用你自己的文件路径就好。log和数据文件路径随意。

[root@localhost redis-3.2.8]# cd utils  
[root@localhost utils]# ls
build-static-symbols.tcl  hyperloglog            redis-sha1.rb
cluster_fail_time.tcl     install_server.sh      releasetools
corrupt_rdb.c             lru                    speed-regression.tcl
create-cluster            redis-copy.rb          whatisdoing.sh
generate-command-help.rb  redis_init_script
hashtable                 redis_init_script.tpl
[root@localhost 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] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /opt/redis-3.2.8
Please select the redis log file name [/var/log/redis_6379.log] /opt/redis-3.2.8/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] /opt/redis-3.2.8/redis_data_6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /opt/redis-3.2.8
Log file       : /opt/redis-3.2.8/redis_6379.log
Data dir       : /opt/redis-3.2.8/redis_data_6379
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.ok^H
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
[root@localhost utils]# 
如果你想知道这个脚本是如何创建系统服务的并且有shell能力的话,就可以读读install_server.sh这脚本

其中有一句 /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."  这句会在/etc/init.d/这个目录下创建一个redis_加上端口号的文件,这里我用的6379, 所以他的文件名叫redis_6379, 如果你想修改什么的话,可以通过修改这个文件。因为我写错了配置文件的路径,导致启动时使用了默认密码,所以我去改了这个文件的Config file    : /opt/redis-3.2.8/redis.conf  ,然后重启启动服务

[root@localhost init.d]# systemctl daemon-reload   #文件发生了变化,需要重新加载
[root@localhost init.d]# systemctl start redis_6379  #重新启动,恢复密码验证,说明修改起了作用了                           [root@localhost redis-3.2.8]# ./src/redis-cli
127.0.0.1:6379> get keys
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> 

# install_server.sh 脚本内容
[root@localhost init.d]# vi redis_6379 
#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli  
PIDFILE=/var/run/redis_6379.pid
CONF="/opt/redis-3.2.8"
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO

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

如果看客老爷看到了这里,那小的万分感谢,自己回头看了下,写的什么乱七八糟的东西。。哈哈哈,如果对你有那么一点帮助的话,请帮忙点个赞,留个言啥的,毕竟自己玩好无聊,如果没有任何帮助的,权当我自嗨了,有时候写点博客能让自己坐的住,安稳的研究下技术还是挺开心的。



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值