热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

安装单机版redis

下载安装$wgethttp://download.redis.io/releases/redis-4.0.9.tar.gz$tarxzfred
下载安装
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar xzf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make && make test && make install
部署配置

redis作为daemon进程去运行的,每次系统启动,redis进程一起启动

拷贝启动脚本

  • redis utils目录下,有个redis_init_script脚本
cp utils/redis_init_script /etc/init.d/redis_6379
  • 将redis_init_script脚本拷贝到linux的/etc/init.d目录中,重命名为redis_6379,6379是我们希望这个redis实例监听的端口号。

修改端口

  • 修改redis_6379脚本的第6行的REDISPORT,设置为相同的端口号(默认就是6379)
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
COnF="/etc/redis/${REDISPORT}.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
        ;;
    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
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

创建配置目录和持久化目录

创建两个目录:/etc/redis(存放redis的配置文件),/var/redis/6379(存放redis的持久化文件)

mkdir -p /etc/redis
mkdir -p /var/redis/6379
  • 修改redis配置文件(默认在根目录下,redis.conf),拷贝到/etc/redis目录中,修改名称为6379.conf
cp redis.conf /etc/redis/6379.conf
  • 修改redis.conf中的配置参数
daemonize   yes                         让redis以daemon进程运行
pidfile     /var/run/redis_6379.pid     设置redis的pid文件位置
port        6379                        设置redis的监听端口号
dir         /var/redis/6379             设置持久化文件的存储位置

启动redis
  • 启动redis,确认redis进程是否启动
cd /etc/init.d
chmod 777 redis_6379
./redis_6379 start

root@VM-0-10-ubuntu:/etc/init.d# ./redis_6379 start
Starting Redis server...
3104:C 23 Jul 09:52:44.297 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3104:C 23 Jul 09:52:44.297 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3104, just started
3104:C 23 Jul 09:52:44.297 # Configuration loaded

root@VM-0-10-ubuntu:/etc/init.d# ps -ef |grep redis
root      3105     1  0 09:52 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379      
root      3120 31412  0 09:52 pts/0    00:00:00 grep --color=auto redis
  • 让redis跟随系统启动自动启动
root@VM-0-10-ubuntu:/etc/init.d# update-rc.d redis_6379 defaults 
update-rc.d: warning: /etc/init.d/redis_6379 missing LSB information
update-rc.d: see 
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379

centos在redis_6379脚本中,最上面,加入两行注释

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

命令行执行 :chkconfig redis_6379 on
redis cli的使用

redis-cli SHUTDOWN,连接本机的6379端口停止redis进程
redis-cli -h 127.0.0.1 -p 6379 SHUTDOWN,制定要连接的ip和端口号
redis-cli PING,ping redis的端口,看是否正常
redis-cli,进入交互式命令行

root@VM-0-10-ubuntu:~# ps -ef|grep redis
root      3105     1  0 09:52 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379      
root      6425  6376  0 10:16 pts/0    00:00:00 grep --color=auto redis

root@VM-0-10-ubuntu:~# redis-cli SHUTDOWN

root@VM-0-10-ubuntu:~# ps -ef|grep redis
root      7583  6376  0 10:25 pts/0    00:00:00 grep --color=auto redis

root@VM-0-10-ubuntu:/etc/init.d# ./redis_6379 start
Starting Redis server...
8436:C 23 Jul 10:31:57.824 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8436:C 23 Jul 10:31:57.824 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=8436, just started
8436:C 23 Jul 10:31:57.824 # Configuration loaded

root@VM-0-10-ubuntu:/etc/init.d# redis-cli PING
PONG

root@VM-0-10-ubuntu:/etc/init.d# redis-cli 
127.0.0.1:6379> set  k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> 

推荐阅读
author-avatar
blank
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有