redis details ----- introduction, optimization. . . .

A, SQL and NoSQL

1、SQL

1, a structured database, created in the relational model, based on generally oriented in the record;
2, including Oracle, Mysql, SQL Server, Microsoft Access, DB2 , etc.
Advantages:
1. Easy maintenance: the table structure is used, a consistent format;
2, easy to use: SQL common language, for complex queries;
3, complicated operations: support the SQL can be used very complex between a table and a plurality of tables query.
Disadvantages:
1, read and write performance is relatively poor, especially efficient mass data read and write;
2, a fixed table structure, flexibility less impressive;
3, for concurrent read and write high demand, traditional relational databases, disk I / O is a big bottleneck.

2、NoSQL

1, in addition to a database other than the mainstream relational database, are considered non-relational;
2, including Redis, MongBD, Hbase, CouhDB, Memcache and so on;
3, non-relational database strictly not a database, it should be a a set of structured data storage method, key-value pair may be a document or the like.
Advantages:
1, flexible format: The format of the stored data can be key-value (key - value) form, the form of documents, pictures, forms, etc., in the form of documents, pictures, forms, etc., flexible, wide application scenarios, and relational database only supports basic types;
2, speed: nosql random access memory or a hard disk may be used as a carrier, and a hard disk can use the relational database;
3, high scalability;
4, low cost: nosql database deployment simple, basically open software.
Disadvantages:
1, sql does not provide support, learning and use of high cost;
2, does not support the transaction, fault tolerance is low;
3, a data structure is relatively complicated, complex queries from less impressive.

3, NoSQL background generated presentation

(1) High performance: high concurrent read and write to the database needs;
(2) the Storage, Huge: mass data storage and access efficiency requirements;
(. 3) && High High Availability Scalability: high scalability of the database with high availability requirements.

Two, redis

(1) Redis and supports persistent memory to run on;
using (2) key-value (key-value pairs) in the form of a memory;
(3) Advantages:

Read and write data with high speed;
supports various data types;
support persistent data;
atomicity;
support data backup

(4) Redis and Memcache comparison:
1, storage:
Memecache all the data present in the memory, will hang power failure, data can not exceed the memory size;
the Redis has upper portion in the hard disk, this will ensure that persistent data sex.
2, the data types supported:
the Memcache relatively simple data types supported;
the Redis complex data types.
3, using different underlying model:
the underlying application protocol between them to achieve the communication between the client and the way is not the same;
Redis VM build their own direct mechanism, because most of the system call system function, it will waste some time to move and requests.
4, value size:
Redis can be up to 1GB, while memcache only 1MB.
(5) What are the benefits of using redis?
1, fast, because the data stored in memory, similar to HashMap, HashMap advantage is to find the time and complexity of the operation is O (1)
2, support for rich data types, support string, list, set, sorted set , hash
3, support services, operations are atomic, so-called atomic changes to the data that is either all executed or not executed all.
4, feature-rich: can be used for caching, message, press the key to set the expiration time, it will be automatically deleted after expiration.
redis compared to memcached advantages:

All values are simple string memcached, Redis As an alternative, supporting richer data type;
Redis much faster than memcached;
Redis can persist its data;
the Memcache structural support, but the support structure of Redis and unstructured.

Third, installation and deployment redis

1, redis installation environment
latest installation package: redis-5.0.7.tar.gz
system: Linux
2, steps
redis details ----- introduction, optimization.  .  .  .
Step one: by remotely mounted a way to download the installation package on the windows to Linux locally.
Step two: Install the compilation tools

yum install gcc gcc-c++ make -y

The third step: decompression

tar zxvf redis-5.0.7.tar.gz -C /opt/

The fourth step: mounting
cd redis-5.0.7 /

make
make PREFIX=/usr/local/redis install

Step five: Create a soft link

ln -s /usr/local/redis/bin/* /usr/local/bin/

Step Five: Start the installation script, see the port status

cd  /redis-5.0.7/utils/

./install_server.sh        ###启动脚本,注意:这步一路回车到底就行了
将最后一个文件路径改为: /usr/local/redis/bin/redis-server

netstat -natp | grep 6379

In addition, it may also be used to control redis_6379 turned off:

/etc/init.d/redis_6379 stop       //关闭

/etc/init.d/redis_6379 start      //开启

/etc/init.d/redis_6379 restart    //重启

redis-cli remote connection operations:

1, first modify the configuration file, add the host IP address

vim /etc/redis/6379.conf

bind 127.0.0.1 192.168.220.131
###添加主机的IP地址,不添加监听地址则无法连接

As shown in FIG:
redis details ----- introduction, optimization.  .  .  .
2, to restart the service
3, a remote connection:

redis-cli -h 192.168.220.131 -p 6379
//-h:指定主机IP地址,-p:指定端口

redis details ----- introduction, optimization.  .  .  .

Four, redis profile

Configuration parameters (/etc/redis/6379.conf):
redis details ----- introduction, optimization.  .  .  .

Five, redis database commonly used commands

1, commonly used commands
(1) to connect to the local database

/usr/local/redis/bin/redis-cli

(2) connect to a remote database

redis-cli  -h  192.168.220.131  -p  6379

(3) to store data: the SET
(4) to obtain data: GET
redis details ----- introduction, optimization.  .  .  .
2, Key Commands
(1) keys: get in line with the rules of the list of keys

>keys *        //查看当前数据库中所有的键

>keys v*       //查看当前数据库中以v开头的键

>keys v?       //查看当前数据库中以v开头后面包含任意一个字符的键

>keys v??      //查看当前数据库中以v开头后面包含任意二个字符的键

(2) exists: determining whether there is a key
redis details ----- introduction, optimization.  .  .  .
(3) del: delete the current database designated key
redis details ----- introduction, optimization.  .  .  .
(. 4) type: Get value corresponding to the key value type
redis details ----- introduction, optimization.  .  .  .
(5) rename (cover) / renamenx (not covered): existing the key to rename:
redis details ----- introduction, optimization.  .  .  .
(6) dbsize: View the number of key current database
redis details ----- introduction, optimization.  .  .  .
(7) redis-benchmark testing tools:
redis details ----- introduction, optimization.  .  .  .

redis-benchmark -h 192.168.220.131 -p  6379  -c 100 -n 100000
//向IP地址为192.168.220.131、端口为6379的redis服务器发送 100个并发连接与 100000个请求测试性能
redis-benchmark -h 192.168.220.131 -p 6379 -q -d 100
//测试存取大小为100字节的数据包的性能

3、Redis 多数据操作
(1)Redis 支持多数据库,默认支持16个数据库,0-15 命名;
(2)多数据相互独立,互不干扰;
(3)多数据常用命令;
1、select:多数据库间的切换(默认是在数据库0中)
redis details ----- introduction, optimization.  .  .  .
2、move:多数据库间移动数据
redis details ----- introduction, optimization.  .  .  .
3、flushdb:清除数据库内数据
redis details ----- introduction, optimization.  .  .  .

六、Redis 持久化:

(1)持久化概述:

Redis 是运行在内存中,内存中的数据断电就会丢失;
为了能重用 Redis 数据,或者防止系统故障,需要将 Redis 中的数据写入到磁盘空间中,即持久化。

(2)持久化分类:
1、RDB 方式:创建快照的方式获取某一时刻 Redis 中所有数据的副本。
2、AOF 方式:将执行的写命令写到文件的末尾,以日志的方式来记录数据的变化。
(3)RDB持久化:
1、它是 Redis的默认持久化方式,默认文件名为 dump.rdb
2、触发条件:
—在指定的时间间隔内,执行指定次数的写操作(配置文件控制);
—执行 save 或者是 bgsave (异步)命令;
—执行 flushall 命令,清除数据库所有数据;
—执行 shutdown 命令,保证服务器正常关闭且不丢失任何数据。

3、优缺点:

适合大规模的数据恢复;
如果业务对数据完整性和一致性要求不高,RDB 是很好的选择;
数据的完整性和一致性不高;
备份时占用内存。

4、通过 RDB 文件恢复数据:

Dump.rdb copy the file to the bin directory of the installation directory redis, redis restart the service.
5, profile options

vim /etc/redis/6379.conf

save 900 1              
save 300 10
save 60 10000
//900秒之内至少一次写操作、300秒之内至少发生10次写操作、60秒之内发生至少10000次写操作,只要满足其一都会触发快照操作,注释所有的save项表示关闭 RDB

dbfilename dump.rdb         // RDB文件名称
dir /var/lib/redis/6379     // RDB文件路径
rdbcompression yes          // 是否进行压缩

(4) AOF persistence:

Redis is not turned on by default;
to make up for the lack of RDB (data inconsistency);
in the form of a log to record the write and append to the file for each;
executed after the restart Redis will write instructions based on the contents of the log file to the front time to complete recovery of data.

1, according to AOF recover data files:

Copy the file to the bin directory appendonly.aof redis installation directory, and restart redis access to.
2, profile options

vim /etc/redis/6379.conf

appendonly yes                     //开启AOF持久化
appendfilename "appendonly.aof"    //AOF文件名称

# appendfsync always    
appendfsync everysec
#appendfsync no
//解释:always:同步持久化,每次发生数据变化会立刻写入磁盘;
       everysec:默认推荐,每秒异步记录一次(默认值);
       no:不同步,交给操作系统决定如何同步。

aof-load-truncated yes         //忽略最后一条可能存在问题的指令

redis details ----- introduction, optimization.  .  .  .
3, AOF rewrite mechanisms:

AOF works is to write to append to a file, the file will be more and more redundancy;
when the size of the AOF file exceeds the threshold set, Redis will be the content of AOF file compression;

4, AOF rewrite principle:

Redis will fork out a new process, read the data (and not read the old files) in memory, and again to a temporary file, and finally replace the old aof file
5, AOF rewrite configuration:

vim /etc/redis/6379.conf

在日志进行 BGREWRITEAOF 时,如果设置为 yes 表示新写操作不进行同步 fsync,只是暂存在缓冲区里,避免造成磁盘 IO 操作冲突,等重写完成后在写入。redis 中默认为 no

no-appendfsync-on-rewrite no
//当前 AOF文件大小是上次日志重写时 AOF 文件大小两倍时,发生 BGREWRITEAOF操作

auto-aof-rewrite-percentage 100
//当前 AOF文件执行 BGREWRITEAOF命令的最小值,避免刚开始启动 redis 时由于文件尺寸较小导致频繁的 BGREWRITEAOF

auto-aof-rewrite-min-size 64mb

Guess you like

Origin blog.51cto.com/14475593/2457904