Redis non-relational database deployment and use (linux)

Table of contents

1. Concept

2. redis installation (ubuntu)

1. Download tools and install

2. Redis uses commands

        1. String type use command

         2. list (list) type use command

        3. set (collection) type command

         4. SortedSet (ordered set type) command

           5.hsa (hash) type command

        6.key operation

3. redis configuration file

4. Redis data persistence


1. Concept

        A NoSQL non-relational database is a database that does not use a relational model to organize data. It is usually used to store unstructured or semi-structured data. It does not support or only partially supports the SQL language to meet eventual consistency. There are many types of non-relational databases, such as key-value databases, document databases, columnar databases, graph databases, time series databases , etc. The advantages of non-relational databases are high flexibility, high performance, and strong scalability , and are suitable for processing massive data and complex data types. The disadvantage of non-relational databases is that they do not provide transaction support, cannot guarantee data integrity and security , and their functions are not as perfect as relational databases.

        Redis is a non-relational database, which is a kind of key-value database . It uses key-value pairs to store data and supports multiple types of values, such as strings, hashes, lists, sets, ordered sets, etc. The characteristics of Redis are:

        High performance : Redis is a memory-based database with a very fast read and write speed, which can reach 100,000 reads and writes per second.
        Persistence : Redis supports two persistence methods, one is snapshot (snapshotting), which saves the data in memory to the disk at regular intervals; Write operations are logged to a file.
        Distributed : Redis supports master-slave replication (master-slave replication), which can realize data backup and load balancing
        Rich functions : In addition to providing basic addition, deletion, modification and query operations, Redis also provides transactions, publish and subscribe, Lua scripts, pipelines, etc. Advanced Features.
        Easy to use : Redis is written in C language, the code is concise, and it is very convenient to install and use.

2. redis installation (ubuntu)

1. Download tools and install

                Chinese website: http://redis.cn

                Official website: http://redis.io

              Upload the downloaded package to the linux server, use the command to decompress and install as shown in the figure below

The command is as follows

解压命令
tar zxvf redis-4.0.8.tar.gz
进入到解压后目录,执行下面命令
make
sudo make install

Use the command to test whether the installation is successful

启动redis
redis-server
打开一个新终端,使用客户端连接
redis-cli           #默认连接本地主机,绑定6379端口
redis-cli -p 端口号    #指定端口连接本地
redis-cli -h IP地址 -p 端口号    #连接远程主机
通过客户端关闭服务器
shutdown
客户端的测试命令
ping

 As shown below

2. Redis uses commands

        1. String type use command

字符串类型都是以
key->string
value->string
的形式

命令1
set key value   #表示创建一个键值对,键值为key,值为value,如果已经存在则会被覆盖
命令2
keys *    #表示查看所有的键值
命令3
get key    #获取到指定键值的值
命令4
mset [key value]    #表示一次创建多个键值对,例:get h1 world1 h2 world2
命令5
mget [key]    #一次获取到多个指定键值的值
命令6
append key value    #在指定键值后追加字符,如果指定键值不存在则会创建该键值
命令7
strlen key    #查看指定键值的值的长度
命令8
decr key    #只可以使用到数字字符,指定的键值减1
命令9
incr key    #只可以使用到数字字符,指定的键值加1
命令10
decrby key 数字n    #只可以使用到数字字符,指定的键值的值减n
命令11
incrby key 数字n    #只可以使用到数字字符,指定的键值的值加n

The following is an example of operation

         2. list (list) type use command

列表类型以
key->string
value->list
形式

命令1
lpush key [value]    #创建一条数据,将一个或多个值插入到列表key的表头
rpush key [value]    #创建一条数据,将一个或多个值插入到列表key的表尾
命令2
lrange key start stop    #遍历列表
    key:遍历的列表名称
    start:遍历的起始位置
    stop:遍历的结束位置
命令3
lpop key    #删除列表为key的表头元素
rpop key    #删除列表为key的表尾元素
命令4
lindex key index    #通过下表获取对应的值
    key:要获取的列表名称
    index:下标值
命令5
llen key    #查看指定列表的长度
命令6
lrem key count value    #根据参数 count 的值,移除列表中与参数 value 相等的元素。
    count 的值可以是以下几种 :
        count > 0: 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
        count < 0: 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值
        count = 0 :移除表中所有与 value 相等的值

The following is an example of operation

        3. set (collection) type command

set类型以
key->string
value->set类型(string,string...)
形式存储

命令1
sadd key [member]    #创建一个名称为key的集合,member为插入的值,插入到头部,不会有重复数据
命令2
smembers key    #遍历集合
命令3
sdiff key1 key2    #求两个集合的差集
命令4
sinter key1 key2    #求两个集合的交集
命令5
sunion key1 key2    #求两个集合的并集
命令6
sdiffstore key key1 key2    #求key1和key2的集合的差集,并且把差集存放到key集合中
命令7
sinterstore key key1 key2    #和命令6一样,该命令求交集
命令8
sunionstore key key1 key2    #和命令6一样,该命令求并集
命令9
spop key    #随机删除集合key中的一个元素
命令10
srem key [member]    #删除集合key中指定的元素member

The following is an example of operation

         4. SortedSet (ordered set type) command

有序集以
key->string
value->sorted[score,member]
的形式存储

命令1
zadd key [score,member]    #创建一个有序集,插入一个或多个值到有序集key中
命令2
zrange key start stop [withscores]    #遍历有序集key,按升序输出
zrevrange key start stop [withscores]    #遍历有序集key,按降序输出
    start:遍历的开始位置
    stop:遍历的结束位置
命令3
zcount key min max    #计算有序集key中,score值在min到max之间的个数
命令4
zrank key member    #返回有序集key中成员member的排名,按成员score值递增排序
zrevrank key member    #返回有序集key中成员member的排名,按成员score值递减排序
命令5
zrem key [member]    #移除一个或多个元素
命令6
zscore key member    #返回有序集key中成员member的score值

           5.hsa (hash) type command

hash类型以
key->string
value->hash[field:value,field:value]
的形式存储

命令1
hset key field value    #创建一个名为key的哈希表,并插入键值对field:value
命令2
hget key field    #取出哈希表key中键为field的值
命令3
hmset key [field value]    #可以同时插入多个键值对
命令4
hmget key [field]    #可以同时获取多个值
命令5
hdel key [field]    #删除哈希表中键值为field的值
命令6
hgetall key    #输出哈希表的键和值
命令7
hkeys key    #输出键
命令8
hvals key    #输出值

 The following is an example of operation

        6.key operation

命令1
del [key]    #删除名称为key的键值对
命令2
keys * ? []    #匹配key值
    *:匹配一串字符
    ?:匹配一个字符
    []:匹配指定字符
命令3
expire key seconds    #设置名称为key的键值存活实际,seconds表示秒
命令4
ttl key    #查看存活时间,-1表示没有设置存活时间,-2表示不存在
命令5
persist key    #移除给定key的生成时间
命令6
type key    #返回key对应的value类型

3. redis configuration file

        The file redis.conf is in the directory where the package was decompressed above. If my package is redis-4.0.8.tar.gz, you need to enter the directory of redis-4.0.8 to find it.

The content meaning of some configuration files

1. bind 127.0.0.1    #绑定的ip地址,只有绑定的ip地址才可以访问到,注释默认所有ip地址都可访问
2. protected-mode yes    #包含模式,要远程连接需要关闭,yes表示开启,no表示关闭
3. port 6379    #使用的端口号,默认6378
4.timeout 0    #超时时间,0表示不启用,>0表示启用
5.daemonize no    #守护进程,no表示不是守护进程,yes表示是守护进程
6.pidfile /var/run/redis-b379.pid    #如果不是守护进程不生效,如果是会生成一个pid文件,可以修改目录,./ -> 表示在redis启动的目录下
7.loglevel notice    #日志级别
8.logfile    ""    #日志文件,要是守护进程才生效,这样表示不输出,


配置好文件后使用一下命令启动以及连接
启动:redis-server redis.conf
连接:redis-cli -p 端口号

4. Redis data persistence

Way generate file storage method advantage shortcoming
rdb mode Generate .rdb file, open by default Will write the data in the memory to the disk file in binary form The file is relatively small, the recovery time is short, and the efficiency is high Synchronize data at the frequency set by the user, which is easy to lose data and has low data integrity
aof way Generate .aof file Will write the command to generate the data to the disk file Data synchronization every 1 second, high data integrity Large files, recovery time bed, low efficiency

        Some configuration file content is also in the redis.conf file

1.rdb的刷新频率,任何一个满足都会刷新
save 900 1
save 300 10
save 60 10000

2.dbfilename dump.rdb    #rdb文件的名字
3.dir ./    #生成的持久化文件保存的目录,rdb和aof
4.appendonly no    #是否要打开aof模式,yes表示是,no表示否
5.appendfilename "appendonly.aof"    #设置aof文件的名字

        some problems

                1. Can aof and rdb be opened at the same time

                        Can

                2. Can aof and rdb be closed at the same time

                        Can

                        Close rdb mode: save ""

                3. The two modes are turned on at the same time. If data recovery is performed, how to choose

                        Efficiency considerations: rdb mode

                        Data integrity considerations: aof mode

Guess you like

Origin blog.csdn.net/weixin_62859191/article/details/130179571