Redis deploy the installation

A, Redis Introduction

Redis is one of the more popular NOSQL system, which is a key-value storage systems. And Memcache similar, but to a large extent compensate for the lack of Memcache, it supports the type of stored value relatively more, including string, list, set, zset and hash. These data types are supported push / pop, add / remove and on the intersection and union, and difference operations richer. On this basis, Redis supports a variety of different ways of sorting.
And as Memcache, Redis data is cached in computer memory, the difference is, Memcache only the data cached in memory, can not be automatically written to the hard disk on a regular basis, which means, a power failure or restart, clear the memory, data loss . So Memcache application scenarios without having to apply to a cache of persistent data. The different Redis is that it will periodically update the data written to disk or to modify the operation of the additional write log files, to achieve persistent data.

Two, Redis installation

Here in the Linux environment, Redis installation and deployment

1, the first official website to download the Redis archive, address: http: //redis.io/download can download the stable version 4.0.6.

2, the remote management tools, the compressed package copied to a Linux server, performs decompression operation

 
[root@CentOS6 ~]# tar zxvf redis-4.0.6.tar.gz -C /usr/local/
[root@CentOS6 ~]# cd /usr/local/
[root@CentOS6 local]# ls
apache-tomcat-8.5.24 etc include lib64 libexec memcached nginx-1.8.0 sbin src
bin games lib libevent maven-3.5.2 nginx redis-4.0.6 share tomcat
[root@CentOS6 local]# ln -sv redis-4.0.6/ redis
`redis' -> `redis-4.0.6/'

3, execute make to compile the file after decompression

 
[root@CentOS6 redis]# pwd
/usr/local/redis
[root@CentOS6 redis]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
[root@CentOS6 redis]# make

After the compilation is complete, you can see there will be redis extract the files corresponding to the src, conf and other folders, unzip it and the windows installation files, like most of the installation package will have a corresponding class files, configuration files, and some of the command file.

 

4, after the successful compilation, into the src folder, run make install Redis be installed

5, the installation is complete, the interface is as follows

 

Three, Redis deployment

After successful installation, following the deployment of Redis

1 , first of all for the convenience of management, the Redis file conf configuration files and frequently used commands move to unified file

a) Create a folder bin and etc

code show as below:

mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/ect

 

b) implementation of mobile Linux file command:

Copy the code code is as follows:

[root@CentOS6 redis]# mv redis.conf etc/
[root@CentOS6 redis]# cd src/
[root@CentOS6 src]# mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server /usr/local/redis/bin

2 , start the background service redis

a) Switch to the / usr / local / redis / etc directory, edit redis.conf file, daemonize property to yes (the need to run in the background)

#vim redis.conf

 

b)切换到/usr/local/redis/bin目录下执行Redis-server 命令,使用/usr/local/redis/etc/redis.conf 配置文件来启动Redis 服务

[root@CentOS6 redis]# cd bin/
[root@CentOS6 bin]# ls
mkreleasehdr.sh  redis-benchmark  redis-check-aof  redis-cli  redis-server
[root@CentOS6 bin]# ./redis-server /usr/local/redis/etc/redis.conf

4、服务端启动成功后,执行redis-cli启动Redis 客户端,查看端口号,默认是6379。

 

5、编辑服务启动脚本

 
vim /etc/init.d/redis

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 80 12
# description: Redis is an open source, advanced key-value store.
#
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
        if [ -e $PIDFILE ];then
             echo "$desc already running...."
             exit 1
        fi
        echo -n $"Starting $desc: "
        daemon $BIN/$prog $CONFIG
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
stop() {
        echo -n $"Stop $desc: "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
        return $RETVAL
}
restart() {
        stop
        start
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL

 

chmod +x /etc/init.d/redis

service redis start
service redis stop

chkconfig --add redis

6、设置环境变量,并使之生效

 
vim /etc/profile.d/redis.sh

export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin

#source /etc/profile.d/redis.sh

7、配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上/etc/sysctl.conf加上 

#vim /etc/sysctl.conf

vm.overcommit_memory = 1 

#sysctl -p 

 

四、总结Redis 操作常用命令

 

Redis

Redis-server /usr..../redis.conf 启动redis服务,并指定配置文件
Redis-cli 启动redis 客户端
Pkill redis-server 关闭redis服务
Redis-cli shutdown 关闭redis客户端
Netstat -tunpl|grep 6379 查看redis 默认端口号6379占用情况

 

4. Redis的配置参数

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

    daemonize no

当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定

    pidfile /var/run/redis.pid

指定Redis监听端口,默认端口为6379

    port 6379

绑定的主机地址

    bind 127.0.0.1

当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能

    timeout 300

指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose

    loglevel verbose

 日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null

    logfile stdout

设置数据库的数量,默认数据库为0,可以使用SELECT <dbid>命令在连接上指定数据库id

    databases 16

指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合

    save <seconds> <changes>

    Redis默认配置文件中提供了三个条件:

    save 900 1

    save 300 10

    save 60 10000

    分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。

 

 指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大

    rdbcompression yes

 指定本地数据库文件名,默认值为dump.rdb

    dbfilename dump.rdb

 指定本地数据库存放目录

    dir ./

设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步

    slaveof <masterip> <masterport>

 当master服务设置了密码保护时,slav服务连接master的密码

    masterauth <master-password>

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

    requirepass foobared

设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息

    maxclients 128

指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区

    maxmemory <bytes>

指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no

    appendonly no

指定更新日志文件名,默认为appendonly.aof

     appendfilename appendonly.aof

 指定更新日志条件,共有3个可选值: 
    no:表示等操作系统进行数据缓存同步到磁盘(快) 
    always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全) 
    everysec:表示每秒同步一次(折衷,默认值)

    appendfsync everysec

 

指定是否启用虚拟内存机制,默认值为no,简单的介绍一下,VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析Redis的VM机制)

     vm-enabled no

 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享

     vm-swap-file /tmp/redis.swap

将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0

     vm-max-memory 0

Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page大小最好设置为32或者64bytes;如果存储很大大对象,则可以使用更大的page,如果不 确定,就使用默认值

     vm-page-size 32

设置swap文件中的page数量,由于页表(一种表示页面空闲或使用的bitmap)是在放在内存中的,,在磁盘上每8个pages将消耗1byte的内存。

     vm-pages 134217728

设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4

     vm-max-threads 4

设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启

    glueoutputbuf yes

指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法

    hash-max-zipmap-entries 64

    hash-max-zipmap-value 512

 指定是否激活重置哈希,默认为开启(后面在介绍Redis的哈希算法时具体介绍)

    activerehashing yes

 指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件

    include /path/to/local.conf

Guess you like

Origin www.cnblogs.com/tudachui/p/11225357.html