Redis configuration and use of NoSQL

Table of contents

1. Relational and non-relational databases

1.1. Overview of relational databases

 1.2 Advantages and disadvantages of relational database

1.2.1 Advantages

1.2.2 Disadvantages

 1.3. Overview of non-relational databases

 2. The difference between relational database and non-relational database

2.1 Different data storage methods

 2.2 Different expansion methods

2.3 Different support for transactional

 2.4 Background of non-relational database

2.4.1 can be used to deal with the three high problems of Web2.0 pure dynamic website type.

 2.5 Summary

2.5.1 Relational database

 2.5.2 Non-relational databases

 2.5.3 Overview

3. Redis of NoSQL database

3.1 Introduction to Redis

 3.2 Single-threaded mode of Redis

3.3 Advantages of Redis

 3.4 Disadvantages of Redis

 3.5 Why Redis is so fast

 4. Redis installation and deployment

 4.1 Execute the install_server.sh script file that comes with the software package to set redid service related configuration

 4.2 Put the redis executable program file into the directory of the path environment variable for system identification

  4.3 Modify configuration /etc/redis/6379.conf parameters

  4.4 Redis Command Tool

4.5 redis-cli command line tool

 4.6 redis-benchmark testing tool

  4.7Common Commands of Redis Database

 4.8keys value

 4.9del delete key

 4.10 Determine whether the key exists

 4.11type Get the type of value

 4.12rename rename (overwrite)

  4.13 renamenx rename (do not overwrite)

 4.14dbsize View the number of keys in the library

  4.15 Set Password

 5. Redis multi-database common commands

 5.1 Switch between multiple databases

  5.2 Moving data between multiple databases

 5.3 Clear the data in the database (Warning! Warning! Warning!)

1. Relational and non-relational databases

1.1. Overview of relational databases

A structured database, built on a relational model (two-dimensional tabular model)

generally record oriented

Statement (standard data query language) is a language based on relational database, which is used to perform retrieval and operation of data in relational database

包括(Oracle,MySQL,SQL Server,Microsoft Access,DB2,PostgreSQL)

Summary: When using the above database, you must first build the database and design the structure of the table, and then store the data according to the table structure. If the data does not match the structure of the table, the storage will fail.

 1.2 Advantages and disadvantages of relational database

1.2.1 Advantages

  • Easy to maintain: all use the table structure and the format is consistent
  • Ease of use: SQL language is common and can be used for complex queries
  • Support complex operations: support SQL, which can be used for very complex queries between one table and multiple tables

1.2.2 Disadvantages

  • The reading and writing performance is relatively poor, especially the high-efficiency reading and writing of massive data
  • Fixed table structure, less flexibility
  • For high concurrent read and write requirements, the hard disk I/O of traditional relational database nodes is a big bottleneck

 1.3. Overview of non-relational databases

NoSQL (NoSQL= Not only SQL), which means not only SQL, but also the assembly of non-relational databases

Databases other than mainstream relational databases are considered non-relational

There is no need to build a database and table in advance to define the structure of the data storage table. Each record can have different data types and the number of fields (such as text, pictures, videos, music, etc. in WeChat group chats)

Including mainstream NoSQL databases include Redis, MongBD, Hbase, Memcached, etc.

 2. The difference between relational database and non-relational database

2.1 Different data storage methods

The main difference between relational and non-relational databases is the way data is stored .

Relational data is ​tabular​ by nature, so it is stored in rows and columns of a data table. Data tables can be associated with each other and stored collaboratively, and it is also easy to extract data.​​​​

Non-relational data does not fit well in rows and columns of a data table, but rather in chunks grouped together​​​​. Non-relational data is usually stored in datasets, like documents, key-value pairs, or graph structures. Your data and its characteristics are the number one influencing factor in choosing how to store and extract your data.

Relational

Rely on the relational model ER diagram while storing data in tabular form

non-relational In addition to being stored in tabular form, data is usually grouped together in large chunks to store data

 2.2 Different expansion methods

The biggest difference between SQL and NoSQL may be in the way of expansion. To support the growing demand, of course, it must be expanded.

 To support more concurrency, the SQL database is scaled up, that is to say, to increase the processing power and use a faster computer, so that the same data set can be processed faster. Because data is stored in relational tables, performance bottlenecks in operations may involve many tables, which need to be overcome by improving computer performance. Although the SQL database has a lot of room for expansion, it will eventually reach the upper limit of vertical expansion.

NoSQL databases, on the other hand, scale horizontally. Because non-relational data storage is naturally distributed, the expansion of NoSQL databases can share the load by adding more common database servers (nodes) to the resource pool.

relation portrait natural form
non-off horizontal Naturally distributed

2.3 Different support for transactional

        If data operations require high transactionality or complex data queries need to control the execution plan, then the traditional SQL database is your best choice in terms of performance and stability. SQL database supports fine-grained control over transaction atomicity, and it is easy to roll back transactions.
        Although NoSQL databases can also use transaction operations, they cannot compare with relational databases in terms of stability, so their real value lies in the scalability of operations and the processing of large amounts of data.

Relational Especially suitable for tasks with high transactional requirements and tasks that need to control the execution plan
non-relationship It will be a little bit true here, and its value lies in high expansion and large data volume processing

 2.4 Background of non-relational database

2.4.1 can be used to deal with the three high problems of Web2.0 pure dynamic website type.

         High performance-------high concurrent read and write requirements for the database

        HugeStorage--------------Requirements for efficient storage and access of massive data

        High Scalability && High Availability------- Requirements for high scalability and high availability of databases

        Relational databases and non-relational databases have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of Web2.0 databases. Let relational databases focus on relations, and non-relational databases focus on storage. For example, in a MySQL database environment where reads and writes are separated, frequently accessed data can be stored in a non-relational database to improve access speed.

 2.5 Summary

2.5.1 Relational database

Relational database: instance->database->table (table)->record row (row), data field (column)

  • High security (persistence)
  • Strong transaction processing ability
  • Strong mission control
  • It can do log backup and recovery, and the disaster recovery capability is stronger

 2.5.2 Non-relational databases

Instance->Database->Collection–>Key-value pair (key-value) Non-relational databases do not need to manually build databases and collections (tables).

  • High security (persistence)
  • Strong transaction processing ability
  • Strong mission control
  • Can do log backup, recovery, and disaster recovery capabilities are stronger

 2.5.3 Overview

  • Relational databases and non-relational databases have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of Web2.0 databases. Let relational databases focus on relations, and non-relational databases focus on storage. For example, in a MySQL database environment where reads and writes are separated, frequently accessed data can be stored in a non-relational database to improve access speed.
MySQL高热数据  ----》  redis
web  ----》  redis ----》  MySQL
CPU  ----》  内存/缓存  ----》  磁盘

3. Redis of NoSQL database

3.1 Introduction to Redis

        Redis is an open source NoSQL database written in C.

        Redis runs based on memory and supports persistence. It adopts key-value (key-value pair) storage form, which is an indispensable part of the current distributed architecture.

        The Redis server program is a single-process model, that is, multiple Redis processes can be started on one server at the same time, and the actual processing speed of Redis depends entirely on the execution efficiency of the main process. If only one Redis process is running on the server, when multiple clients access at the same time, the processing capacity of the server will decrease to a certain extent; if multiple Redis processes are opened on the same server, Redis will improve the concurrent processing capacity. At the same time, it will cause a lot of pressure on the CPU of the server. That is: in the actual production environment, it is necessary to decide how many Redis processes to open according to the actual needs. If you have higher requirements for high concurrency, you may consider opening multiple processes on the same server. If the CPU resources are relatively tight, a single process can be used.

 3.2 Single-threaded mode of Redis

        The Redis server program is a single-process model

        The Redis service can start multiple Redis processes on one server at the same time, and the actual processing speed of Redis depends entirely on the execution efficiency of the main process. If only one Redis process is running on the server, when multiple clients access at the same time, the processing capacity of the server will decrease to a certain extent; if multiple Redis processes are opened on the same server, Redis will improve the concurrent processing capacity. At the same time, it will cause a lot of pressure on the CPU of the server. That is: In the actual production environment, it is necessary to decide how many Redis processes to open according to the actual needs.

3.3 Advantages of Redis

Redis is a memory-based database, and caching is one of its most commonly used scenarios. In addition, common application scenarios of Redis also include operations to obtain the latest n data, leaderboard applications, counter applications, storage relationships, real-time analysis systems, and log records.

 High data read and write speed​: The data read speed can reach up to 110,000 times/s, and the data write speed can reach up to 81,000 times/s. ​​​​

Supports rich data types​​: supports data type operations such as key-value, Strings, Lists, Hashes, Sets, and Ordered Sets. ​​​​

Support data persistence​: The data in the memory can be saved in the disk, and can be loaded again for use when restarting. Atomicity
​: All Redis operations are atomic.
​​​​

Support data backup​: that is, data backup in master-salve mode.

 3.4 Disadvantages of Redis

  • The data capacity is limited by physical memory and cannot be used for high-performance reading and writing of massive data. Therefore, the suitable scenarios for Redis are mainly limited to high-performance operations and calculations with small data volumes.

 3.5 Why Redis is so fast

​​​​Note: The newly added multi-threading in Redis 6.0 only uses multi-linearity for processing network requests, while the data read and write commands are still processed by a single thread.

  • Redis is a pure memory structure, which avoids time-consuming operations such as disk work/o.
  • The core module of Redis command processing is single-threaded, which reduces the cost of lock competition, frequent creation and destruction of threads, and reduces the consumption of thread context switching
  • The I/O multiplexing mechanism is adopted to greatly improve the concurrency efficiency. ​​​​

 4. Redis installation and deployment

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
 
#安装gcc gcc-c++ 编译器
cd /opt/
yum install -y gcc gcc-c++ make
 
tar zxvf redis-5.0.7.tar.gz 
 
cd /opt/redis-5.0.7/
make && make PREFIX=/usr/local/redis install
 
#由于Redis源码包中直接提供了Makefile 文件,所以在解压完软件包后,不用先执行./configure 进行配置,可直接执行make与make install命令进行安装

 

 

 4.1 Execute the install_server.sh script file that comes with the software package to set redid service related configuration

cd /opt/redis-5.0.7/utils
./install_server.sh
……
慢慢回车
Please select the redis executable path []
手动输入
/usr/local/redis/bin/redis-server
#要一次性输入正确,不然还要重新执行
Selected config:
Port           : 6379               #默认侦听端口为6379
Config file    : /etc/redis/6379.conf       #配置文件路径
Log file       : /var/log/redis_6379.log      #日志文件路径
Data dir       : /var/lib/redis/6379        #数据文件路径
Executable     : /usr/local/redis/bin/redis-server  #可执行文件路径
Cli Executable : /usr/local/bin/redis-cli     #客户端命令工具

 4.2 Put the redis executable program file into the directory of the path environment variable for system identification

ln -s /usr/local/redis/bin/* /usr/local/bin/    #首先这里做个软链接
 
#当install_server.sh 脚本运行完毕,Redis服务就已经启动,默认侦听端口为6379
netstat -natp | grep redis
 
#redis服务控制
/etc/init.d/redis_6379 stop       #停止
/etc/init.d/redis_6379 start      #启动
/etc/init.d/redis_6379 restart      #重启
/etc/init.d/redis_6379 status     #状态

  4.3 Modify configuration /etc/redis/6379.conf parameters

vim /etc/redis/6379.conf
 
#70行;添加;监听的主机地址
bind 127.0.0.1 192.168.190.2   
#93行;Redis默认的监听端口
port 6379                 
#137行;启用守护进程
daemonize yes             
#159行;指定 PID 文件
pidfile /var/run/redis_6379.pid       
#167行;日志级别
loglevel notice               
#172行;指定日志文件
logfile /var/log/redis_6379.log       
 
/etc/init.d/redis_6379 restart

 

  4.4 Redis Command Tool

redis-server    用于启动 Redis 的工具
redis-benchmark   用于检测 Redis 在本机的运行效率
redis-check-aof   修复 AOF 持久化文件
redis-check-rdb   修复 RDB 持久化文件
redis-cli     Redis命令行工具

4.5 redis-cli command line tool

语法:redis-cli -h host -p port -a password
-h Specify the remote host
-p Specify the port number of the Redis service
-a Specify Mianmian, the -a option can be omitted if the database password is not set
若不添加任何选项表示,则使用 127.0.0.1:6379 连接本机上的 Redis 数据库
例:
redis-cli -h 192.168.40.17 -p 6379
#此时无密码,不需要-a直接登陆

 4.6 redis-benchmark testing tool

​​​​redis-benchmark is the official Redis performance testing tool that comes with it, which can effectively test the performance of Redis services.

基本的测试语法:redis-benchmark [选项] [选项值]。
 
-h  指定服务器主机名。
-p  指定服务器端口。
-s  指定服务器 socket
-c  指定并发连接数。
-n  指定请求数。
-d  以字节的形式指定 SET/GET 值的数据大小。
-k  1=keep alive 0=reconnect 。
-r  SET/GET/INCR 使用随机 key, SADD 使用随机值。
-P  通过管道传输请求。
-q  强制退出 redis。仅显示 query/sec 值。
--csv 以 CSV 格式输出。
-l  生成循环,永久执行测试。
-t  仅运行以逗号分隔的测试命令列表。
-I  Idle 模式。仅打开 N 个 idle 连接并等待。

At this time, send 100 concurrent connections and 100,000 requests to the Redis server with IP address 192.168.40.17 and port 6379 to test performance

 

 Test performance for accessing packets of size 100 bytes 

redis-benchmark -h 192.168.40.17-p 6379 -q -d 100

 Test the performance of the Redis service on this machine when performing set and lpush operations

redis-benchmark -t set,lpush -n 100000 -q

  4.7Common Commands of Redis Database

set stores data, the command format is set key value
get to get data, the command format is get key
keys command can get the key value list that conforms to the rules, usually it can be used in combination with *, ? and other options.
The exists command can determine whether a key value exists.
The del command can delete the specified key of the current database.
The type command can get the value type corresponding to the key.
 
set stores data, the command format is set key value
get gets data, the command format is get key Example:
 
example:
redis-cli -p 6379
 
set name hehe
get name

 

 4.8keys value

keys  命令可以取符合规则的键值列表,通常情况可以结合*、?等选项来使用。
 
redis-cli -p 6379
 
set name hehe
get name
 
set a1 1
set a2 2
set a33 3
set b1 4
set b2 5
set b33 6
 
keys *
keys b*
keys b?
keys b??

 

 4.9del delete key

keys *
del a1
keys *

 4.10 Determine whether the key exists

exists  命令可以判断键值是否存在。
 
exists name
exists hehe

 4.11type Get the type of value

 4.12rename rename (overwrite)

rename 命令是对已有 key 进行重命名。(覆盖)
命令格式:rename 源key 目标key
 
使用rename命令进行重命名时,无论目标key是否存在都进行重命名,且源key的值会覆盖目标key的值。在实际使用过程中,建议先用 exists 命令查看目标 key 是否存在,然后再决定是否执行 rename 命令,以避免覆盖重要数据。
 
例:
keys a*
rename a22 a2
keys a*
get a1
get a2
rename a1 a2
keys a*
get a2

  4.13 renamenx rename (do not overwrite)

The function of the renamenx command is to rename the existing key and check whether the new name exists. If the target key exists, the rename will not be performed. (Do not overwrite)
Command format: renamenx source key target key
 
example:
keys a*
get a2
get a1
renamenx a2 a1
keys a*
get a2
get a1

 

 4.14dbsize View the number of keys in the library

dbsize 命令的作用是查看当前数据库中 key 的数目。
例:
keys *
dbsize

 

  4.15 Set Password

使用config set requirepass password命令设置密码
使用config get requirepass命令查看密码(一旦设置密码,必须先验证通过密码,否则所有操作不可用)
例:
config set requirepass 123456
auth 123456
config get requirepass
quit
redis-cli
keys *
auth 123456
keys *

 5. Redis multi-database common commands

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

 5.1 Switch between multiple databases

命令格式:select 序号
使用 redis-cli 连接 Redis 数据库后,默认使用的是序号为 0 的数据库。
 
127.0.0.1:6379> select 10     #切换至序号为 10 的数据库
127.0.0.1:6379[10]> select 15   #切换至序号为 15 的数据库
127.0.0.1:6379[15]> select 0    #切换至序号为 0 的数据库

  5.2 Moving data between multiple databases

格式:move 键值 序号
例:
set nannan jiajia
get nannan
 
select 5
get nannan
 
select 0
move nannan 5
get nannan
 
select 5
get nannan

 5.3 Clear the data in the database (Warning! Warning! Warning!)

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

 

Guess you like

Origin blog.csdn.net/m0_57554344/article/details/131915571