Redis [1] - NoSQL concept, start (background, logging, remote), detailed configuration (Customization: daemon, remote connection, set the password), normally closed, remote connection tools, using redis by Docker

Here Insert Picture Description

Here Insert Picture Description

Features:

  • Free open source under a BSD agreement
  • NOSQL
  • Using ANSI C language
  • Support network, memory, persistent key-value database journaling
  • API provides multilingual support

NoSQL

Non-relational databases (Not-Only SQL, NoSQL)


Problems faced:

Ultra-large-scale, high concurrent SNS type of Web2.0 pure dynamic site exposes a lot of insurmountable problems and needs:

  • High performance - the demand for high concurrent read-write database
  • Huge Storage - demand for efficient storage and access vast amounts of data
  • High Scalability && High Availability - the demand for database high scalability and high availability

Sum up by saying: Requires database to support large-scale data sets, multiple data types, big data applications

Traditional relational database solution does not work, NoSQL can.

# NoSQL type

  • Key (key-value) stored in the database
    Such as: Redis, Voldemort, DB, Tokyo Cabinet / Tyrant
    scenarios: content caching
    data model: key/value
    Advantage: Quick Query
    weaknesses: the lack of a structured data storage

  • Column store database
    , such as: HBase, Riak, Cassandra
    applications: distributed file system
    data model: Column cluster storage (the same column data exist together)
    Advantage: Finding fast, scalable (fast additions and deletions), easily distributed expansion
    disadvantages: relatively limited functions

  • Data database
    Such as: MongoDB, CouchDB
    application: web application
    data model: a series of key-value pairs (with kay-value is similar to a value of structured)
    advantage: the data structure is not critical
    disadvantage: Query performance is not high, lack of a unified query syntax

  • Graphics (Graph) databases
    such as: Neo4J, InfoGrid, Infinite Graph
    Application: Social network
    data model: Figure structure
    advantages: FIG structurally related algorithms
    disadvantage: the requirements of the entire chart to do the calculation in order to obtain results, not easy to do a distributed clustering scheme


Summary:
NoSQL application scenarios ⇒ high concurrency scenarios

  • Simple Data Model
  • High performance database requirements
  • It does not require a high degree of data consistency

Redis


advantage

  • High performance - read 110,000 times / s, write 81000 times / s
  • Rich data types - String, Hash, List, Set , Ordered Set
  • Atomicity - Redis All operations are atomic (MULTI through and EXEC instructions to cause the operation also supports multiple transaction, i.e. atomic)
  • Rich expansion - support publish / subscribe, notification, key features such as expired
  • High-speed read and write - to use their own implementation 分离器,Do not use lock (MySQL using the lock)

Shortcoming

  • Persistence - in memory, persistent data storage There are two redis:
    1. Time snapshot (snapshot):
      the timing of writing data to the disk, each are all read and write, the cost is very high
    2. Based on the statement added (aof):
      only changes to data in the database, it may lead to additional log is too large .
      And all operations append mode is re-run it again, reply to slow
  • Memory consumption , high memory usage.

Do database, caching, hot data (often queries, often without modification), messaging middleware

Scenes

  1. Cache
  2. Leaderboard
  3. counter
  4. Distributed session
  5. Distributed Lock
  6. Social network
  7. Current list
  8. Messaging System

# Installation

Official website: https://redis.io/
Download: https://redis.io/download
(IO, British Indian Ocean Territory, British Indian Ocean Territory)

First, we need c compiler environment

Install gcc

Redis need to unpack the archive file is compiled,

yum -y install gcc automake autoconf libtool make

Download redis archive

wget http://download.redis.io/releases/redis-5.0.8.tar.gz

Decompression

tar xzvf redis-5.0.8.tar.gz -C /opt
# x 解压
# z 支持gzip解压文件
# v 显示信息
# f 指定压缩文件
# -C 解压到指定目录

Compile

cd /opt/redis-5.0.8
make

If the Here Insert Picture Description
run (below) command, after recompiling redis

cd /opt/cd /opt/redis-5.0.8/deps
make lua hiredis linenoise

Specify the installation location

(Compiled file after installation) to install the specified directory:

make PREFIX=/usr/local/redis install

note:

  1. You must be in the directory compiled
  2. PREFIX must be capitalized, and it will automatically create redis directory for us, and the result is the installation directory

Check the installation directory
Here Insert Picture Description
on the left of rwxr stands for "write readable executable" soInstallation directory bin file is executable
among them

  • redis-cli - redis client
  • redis-server - redis server-side

# Start the server (back-end start, firewall, boot)

 /usr/local/redis/bin/redis-server

Starts successfully, the installation was successful

Note two things: the port number 6379, version 5.0.8
Here Insert Picture Description

Backgrounding

# 创建目录
# mkdir /dev/redis
nohup /usr/local/redis/bin/redis-server   > /dev/redis/logs &

View background tasks

jobs -l
# -l 列出 PID,用 kill PID 可以结束程序

Here Insert Picture Description
View Log (ctrl + c to exit)

tail -f /dev/redis/logs

Firewall open port

firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload

# Start the client (local, remote)

Start grammar

redis-cli -h IP地址 -p 端口 
# 默认IP本机 端口 6379

local connection

/usr/local/redis/bin/redis-cli
# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379

The connection is successful (below),ip:端口
Here Insert Picture Description

Connection remote connection tools

# Simple Commands

PING、PONG

Detecting whether the server starts
Here Insert Picture Description

get、set

Stored-value, value

Here Insert Picture Description

keys *

Check all Key

Here Insert Picture Description

ttl name

The remaining time to live (TTL, time to live)

Here Insert Picture Description

-1 Permanent representatives

expire k1 15

Here Insert Picture Description

setex key time value

The method of timing of specific string

Here Insert Picture Description

  • In addition to the string, the other is set to expire by the time
  • If you do not set the time, it will never become obsolete
  • If you set the time, after the want long-term storage, use persist key
    Here Insert Picture Description

# Configure (detail)

dump.rdb

redis data stored in memory, also timing data for persistence

dump.rdb which is the persistent storage of information (inside information that we have set the above)

Here Insert Picture Description

redis.conf

Redis.conf copy the configuration file to a directory under the installation file decompression stranger

Here Insert Picture Description

cp /opt/redis-5.0.8/redis.conf /usr/local/redis/

View redis.conf file

vim /usr/local/redis/redis.conf

Key to see (below) (the order 5.0.8) several configuration

# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf

# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.

################################## INCLUDES ###################################

# 指定包含其他的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件
# 而同时各个实例又拥有自己的特定配置文件
#
# include /path/to/local.conf
# include /path/to/other.conf

################################## MODULES #####################################

# 略

################################## MODULES #####################################

# 绑定的主机地址
bind 127.0.0.1

# 端口号
port 6379

# 客户端连接超时
timeout 0

# redis默认不是以守护进程方式(后台方式)进行
# yes 更换为守护进程开启
# Redis采用的是单进程多线程的模式。
# 在守护进程模式下(daemonize yes),redis会在后台运行,并将进程pid号写入至redis.conf选项pidfile设置的文件中,此时redis将一直运行,除非手动kill将该进程关闭。
# 而当不是守护进程时(daemonize no),开启进程的当前界面将进入redis命令行界面,exit强制退出、或者关闭连接工具(putty,xshell等)都会导致redis进程退出。
# 服务端开发的大部分应用都是采用后台运行的模式
daemonize no

# 当redis以守护进程方式运行,redis默认把pid写入pidfile指定的文件
pidfile /var/run/redis_6379.pid

# 日志级别
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice

# 日志记录方式,默认为标准输出
# 如果配置为守护京进程方式运行,而这里有配置标准输出,则日志将被发送给/dev/null
# logfile ""
logfile stdout


# 设置数据库的数量
# 默认数据库为0,可以使用 SELECT <dbid> 命令在连接上指定数据库id
# (后面会讲为什么是16个,每个库有什么用)
databases 16

################################ SNAPSHOTTING  ################################

# 指定在多长时间内,有多少次更新操作,就会将数据同步到数据文件,可以多个条件配合
# save <seconds> <changes>
# 默认分表表示为:
# 900秒内有1个更改,300秒内有10个更改,60秒内有10000个更改
save 900 1
save 300 10
save 60 10000

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

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

# 指定本地数据库存放目录
dir ./

################################# REPLICATION #################################

# 设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
#
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
# replicaof <masterip> <masterport>

# 当master服务设置了密码保护时,slav服务连接master的密码
#
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>

################################## SECURITY ###################################

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

################################### CLIENTS ####################################

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

############################## MEMORY MANAGEMENT ################################

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

# 指定内存维护策略(下面马上讲)
#
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones with an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
# Both LRU, LFU and volatile-ttl are implemented using approximated
# randomized algorithms.
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy noeviction

############################# LAZY FREEING ####################################

# 略

############################## APPEND ONLY MODE ###############################

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

# 指定更新日志名,默认为appendonly.aof
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

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

################################ LUA SCRIPTING  ###############################

# 略

################################ REDIS CLUSTER  ###############################

# 略

################################## SLOW LOG ###################################

# 略

################################ LATENCY MONITOR ##############################

# 略

############################### ADVANCED CONFIG ###############################

# 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# 指定是否激活重置哈希,默认为开启
# (后面再介绍Redis的哈希算法)
activerehashing yes

########################### ACTIVE DEFRAGMENTATION #######################

# 略

# Memory maintenance strategy

The maintenance memory data, there are two solutions in the redis

Set the timeout

That is above expire and setex

Dynamic Data Delete (LRU algorithm, LFU algorithm)

  • For but unused memory blocks (memory blocks) is called the LRU ( Least Recently Used, least recently used algorithm)
  • Page replacement algorithm by means of a memory management, the operating system determines which data belong to move it out of memory LRU and vacated system built to load additional data
  1. the LRU-volatile : data timeout setting, remove the least frequently used data
  2. the LRU-AllKeys : query all the key in the least recently used data to delete,This is the most widely used strategy
  3. volatile-random: timeout has been set randomly delete data
  4. allkeys-random: query all the key, then randomly deleted
  5. volatile-ttl: After setting all the data for a given query timeout, sorting, will soon be expired data deletion.
  6. noeviction: If you set for the property, it will not delete operation, if memory overflow error is returned. (default)
  7. volatile-lfu: expulsion from the expiration time of all of the keys arranged in use the least level of bond

    LFU ( Least Frequently Used, least recently used algorithm) is also a common caching algorithm

  8. allkeys-lfu: expulsion from all the keys in the key with a minimum of frequency

lfu is a new strategy after 4.0

# Custom Configuration (guard open, remote access, set a password)

Note:
Before modifying First off service
Here Insert Picture Description

Start modifying

vim /usr/local/redis/redis.conf
# 开启默认守护进程启动(当然,你可以使用nohup方法)
# daemonize no 
daemonize yes


# 注释掉,即可允许本机以外的机器访问Redis服务
# bind 127.0.0.1

# 设置密码 (1.保证安全、2.有些情况下不设定密码是无法进行远程连接访问的)
# requirepass 

Validate the configuration

The server starts: (every time you start you need to specify oh)

 /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

Here Insert Picture Description

Start the process by way of guard, jobs imperative can not be viewed.
Check whether to run, you need to

ps -auxf | grep redis

Here Insert Picture Description

Client Login:
with redis-cli password (redis-cli -a password)

# redis-cli -h PI地址 -p 端口 -a 密码
redis-cli -h host -p port -a password

# Redis Close

  1. Direct Close [undesirable]
    (de-energized, closed abnormally.Easy data loss)
    Query redis process id
    ps -ef | grep -i redis 
    # -i 不区分大小写
    
    id kill for queries were forced to shut down
    kill -9 PID
    # -9 表示强制关闭
    
  2. Let redis service initiative to close [Recommended]
    (normally closed, data storage)
    close redis service, carried out by the client shutdown
    Here Insert Picture Description

# Visualization tools: redis-desktop-manager

Download the official website should be charged, but I believe the problem is not you.
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description

Focus is on

  1. Configuration To configure the server password, not those who let connection
  2. To turn on the firewall, refer to the above "Starting the server"

Here Insert Picture Description
Here Insert Picture Description

We will find that our data set, by default there is 0 library.
why is it like this? Why 16 libraries? Here to talk

Here Insert Picture Description

Doker with Redis

docker install & use

Mirrored pull redis

docker pull redis

docker way : open redis server

# docker run -d --name docker容器名 -p 本地端口:映射容器端口 镜像名 --requirepass "密码"
# -d 守护进程模式运行
# --requirepass 是dockerfile指定的参数,这里为redis的登录密码
 docker run -d --name redis6379 -p 6379:6379 redis --requirepass "root"

Here Insert Picture Description

docker boot image very simple, but if you want to know the details, we need to look at the appropriate (redis) of dockerfile(Official website)

windows connect the virtual machine ip 192.163.64.3 redis

Here Insert Picture Description

# -t 让docker分配一个伪终端并绑定到容器的标准输入上
# -i 则让容器的标准输入保持打开
docker exec -it redis6379 bash

Here Insert Picture Description
Here Insert Picture Description
docker way : open redis client (redis server connection docker in)

# redis-cli -a root 开启redis客户端,指定密码
docker exec -it redis6379 redis-cli -a root

Here Insert Picture Description

Published 501 original articles · won praise 112 · views 20000 +

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/105087326