Redis of NotSQL (2) (Redis database common commands and multi-database common commands, Redis high availability and persistence)


One, Redis database commonly used commands

1.SET and GET

#set: 存放数据,命令格式为 set key value
#get: 获取数据,命令格式为get key

[root@localhost ~]# redis-cli -h 192.168.126.15 -p 6379
192.168.126.15:6379> set teacher wanger
OK
192.168.126.15:6379> get teacher
"wanger"

mark

2.KEYS

#KEYS 命令可以取符合规则的键值列表,通常情况可以结合"*、?"等选项来使用
192.168.126.15:6379> set k1 1
OK
192.168.126.15:6379> set k2 2
OK
192.168.126.15:6379> set k3 3
OK
192.168.126.15:6379> set v1 4
OK
192.168.126.15:6379> set v5 5
OK
192.168.126.15:6379> set v22 5
OK


192.168.126.15:6379> KEYS *         #查看当前数据库中所有键

192.168.126.15:6379> KEYS v*        #查看当前数据库中以v开头的数据

192.168.126.15:6379> KEYS v?        #查看当前数据库中以v开头后面包含任意一位的数据

192.168.126.15:6379> KEYS v??       #查看当前数据库中以v开头v开头后面包含任意两位的数据

mark
mark

3.EXISTS

#exists 命令可以判断键值是否存在

192.168.126.15:6379> exists teacher     #返回值为 1 表示存在
(integer) 1
192.168.126.15:6379> exists tea         #返回值为 0 表示不存在
(integer) 0


#del命令可以删除当前数据库的指定 key

192.168.126.15:6379> KEYS *

192.168.126.15:6379> del v5
(integer) 1
192.168.126.15:6379> get v5
(nil)
192.168.126.15:6379> type k1    #type 命令可以获取 key 对应的 value 值类型
string

mark
mark

4.RENAME

# rename 命令是对已有key进行重命名(覆盖)

命令格式:rename 源key 目标key

#使用rename命令进行重命名时,无论目标key是否存在都进行重命名,且源key的值会覆盖目标key的值
#在实际使用过程中,建议先用 exists 命令查看目标 key 是否存在,然后再决定是否执行 rename 命令,以避免覆盖重要数据


192.168.126.15:6379> KEYS v*
1) "v1"
2) "v22"
192.168.126.15:6379> rename v22 v2
OK
192.168.126.15:6379> KEYS v*
1) "v2"
2) "v1"
192.168.126.15:6379> get v1
"4"
192.168.126.15:6379> get v2
"5"
192.168.126.15:6379> RENAME v1 v2
OK
192.168.126.15:6379> get v1
(nil)
192.168.126.15:6379> get v2
"4"

mark

5.RENAMENX

# renamenx 命令的作用是对已有key进行重命名,并检测新名是否存在,如果目标key存在则不进行重命名(不覆盖)
命令格式: renamenx 源key 目标key

192.168.126.15:6379> KEYS *
1) "v2"
2) "k3"
3) "teacher"
4) "k1"
5) "k2"
192.168.126.15:6379> GET teacher
"wanger"
192.168.126.15:6379> GET v2
"4"
192.168.126.15:6379> RENAMENX v2 teacher
(integer) 0

192.168.126.15:6379> KEYS *

192.168.126.15:6379> GET teacher

192.168.126.15:6379> get v2

mark

6.DBSIZE

#dbsize命令的作用是查看当前数据库中key的数目。
192.168.126.15:6379> DBSIZE
(integer) 5
192.168.126.15:6379> KEYS *

192.168.126.15:6379> CONFIG SET requirepass 123123      #设置密码为 123123
OK
192.168.126.15:6379> auth 123123                        #一旦设置密码,必须先验证通过密码,否则无法进行任何操作
OK
192.168.126.15:6379> CONFIG GET requirepass             #查看密码
1) "requirepass"
2) "123123"

mark


Two, Redis multi-database commonly used commands

  • Redis supports multiple databases. Redis contains 16 databases by default. The database names are sequentially named with numbers 0-15
  • Multiple databases are independent of each other and do not interfere with each other

1. Switch between multiple databases

命令格式: select 序号
使用 redis-cli 连接Redis数据库后,默认使用的是序号为 0 的数据库
192.168.126.15:6379> SELECT 10          #切换至序号为10的数据库
OK
192.168.126.15:6379[10]> SELECT 15      #切换至序号为15的数据库
OK
192.168.126.15:6379[15]> KEYS *
(empty list or set)
192.168.126.15:6379[15]> SELECT 0       #切换至序号为0的数据库
OK
192.168.126.15:6379> KEYS *

mark

2. Move data between multiple databases

格式: move 键值 序号


192.168.126.15:6379> set k1 100
OK
192.168.126.15:6379> get k1
"100"
192.168.126.15:6379> SELECT 1
OK
192.168.126.15:6379[1]> get k1
(nil)
192.168.126.15:6379[1]> SELECT 0        #切换至目标数据库0
OK
192.168.126.15:6379> get k1             #查看目标数据是否存在
"100"
192.168.126.15:6379> move k1 1          #将数据库0中k1移动到数据库1
(integer) 1
192.168.126.15:6379> SELECT 1           #切换至目标数据库1
OK
192.168.126.15:6379[1]> GET k1          #查看被移动数据
"100"
192.168.126.15:6379[1]> select 0
OK
192.168.126.15:6379> GET k1             #在数据库0中无法查看到k1的值
(nil)

mark

3. Clear the data in the database

FLUSHDB:清空当前数据库数据
FLUSHALL:清空所有数据库的数据,慎用!

Three, Redis high availability

  • In a web server, high availability refers to the time during which the server can be accessed normally, and the measurement standard is how long it can provide normal services (99.9%, 99.99%, 99.999%, etc.)
  • However, in the context of Redis, the meaning of high availability seems to be broader. In addition to ensuring the provision of normal services (such as master-slave separation, rapid disaster recovery technology), data capacity expansion also needs to be considered, and data security will not be lost, etc.

In Redis, the technologies to achieve high availability mainly include persistence, master-slave replication, sentinels, and clusters. The functions of them and the problems they solve are explained below:

1. Endurance

  • Persistence is the simplest high-availability method (sometimes not even classified as a high-availability method)
  • The main function is data backup, that is, the data is stored on the hard disk to ensure that the data will not be lost due to the process exit

2. Master-slave replication

  • Master-slave replication is the basis of high-availability Redis. Sentinel and clusters are all based on master-slave replication to achieve high availability.
  • Master-slave replication mainly implements multi-machine backup of data, as well as load balancing for read operations and simple failure recovery
  • The disadvantage is that failure recovery cannot be automated, write operations cannot be load balanced, and storage capacity is limited by a single machine

3. Sentry

  • On the basis of master-slave replication, Sentinel realizes automatic failure recovery
  • The disadvantage is that the write operation cannot be load balanced, and the storage capacity is limited by a single machine

4. Cluster

  • Through clustering, Redis solves the problem that write operations cannot be load balanced and storage capacity is limited by a single machine
  • A relatively complete high-availability solution has been realized

Fourth, Redis persistence

1. Persistent function

  • Redis is an in-memory database, and data is stored in memory. In order to avoid permanent loss of data after the Redis process exits abnormally due to server power failure, etc., it is necessary to periodically save the data in Redis in some form (data or command) from memory To hard drive
  • When redis restarts next time, use persistent files to achieve data recovery
  • In addition, for disaster backup, you can copy persistent files to a remote location

2. Redis provides two ways for persistence

  • RDB persistence: The principle is to save Reids' database records in memory to disk regularly
  • AOF persistence (append only file): The principle is to write the operation log of Reids to a file in an appended manner, similar to the binlog of MySQL
  • Because AOF persistence has better real-time performance, that is, less data is lost when the process exits unexpectedly, AOF is the current mainstream persistence method, but RDB persistence still has its place

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/114004877