NoSQL Redis (1) (Relational and non-relational databases, introduction to Redis, installation and deployment, and common tools)


One, relational database and non-relational database

1. Relational database

  • Relational database is a structured database, created on the basis of a relational model (two-dimensional table model)
  • Generally record-oriented
  • Mainstream relational databases include Ocacle, MySQL, SQL Server, Microsoft, Access, DB2, etc.
  • SQL statement (standard data query language) is a language based on relational database, used to perform retrieval and operation of data in relational database

2. Non-relational database

  • NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is a general term for non-relational databases
  • Except for mainstream relational databases, all databases are considered non-relational
  • The mainstream NoSQL databases include Redis, MongBD, Hbase, Memcached, etc.

3. Difference

3.1 Different data storage methods

  • The main difference between relational and non-relational databases is the way data is stored
  • Relational data is naturally in table format, so it is stored in the rows and columns of the data table
  • Data tables can be stored in association with each other, and it is easy to extract data
  • On the contrary, non-relational data is not suitable for storing in the rows and columns of the data table, but is grouped together in large chunks
  • Non-relational data is usually stored in data sets, like documents, key-value pairs, or graph structures
  • Your data and its characteristics are the primary factors influencing the choice of data storage and retrieval methods

3.2 Different expansion methods

  • The biggest difference between SQL and NoSQL databases may be in the way of expansion. To support the increasing demand, of course, it must be expanded.
  • To support more concurrency, the SQL database is scaled vertically, which means to increase processing power and use faster computers, so that the same data set can be processed faster
  • Because the data is stored in relational tables, the performance bottleneck of the operation may involve many tables, all of which need to be serviced by improving computer performance. Although the SQL database has a lot of room for expansion, it will definitely reach the upper limit of vertical expansion in the end
  • The NoSQL database is scaled horizontally
  • Because non-relational data storage is naturally distributed, the expansion of NoSQL databases can share the load by adding more ordinary database servers (nodes) to the resource pool

3.3 Different support for transactional

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

4. The background of non-relational data

  • Both 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
  • Relational databases focus on relations, non-relational databases focus on storage
  • For example, in a MySQL database environment where read and write are separated, frequently accessed data can be stored in a non-relational database to improve access speed
  • Demand-driven development:
    • Demand for high concurrent reading and writing of the database
    • Demand for efficient storage and access to massive data
    • Demand for high scalability and high availability of the database

5. Summary

  • Relational Database:
    • Example -> database -> table (table) -> record row ((row), data field (column)
  • Non-relational database:
    • Example -> database -> collection -> key-value pair (key-value)
  • Non-relational databases do not need to manually build databases and collections (tables)

2. Introduction to Redis

Insert picture description here

1. Overview of Redis

  • Redis is an open source NoSQL database written in C language
  • Redis runs on memory and supports persistence. It uses a 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 to say, multiple Redis processes can be started at the same time on a server, but the actual processing speed of Redis is completely dependent on the execution efficiency of the main process
  • If only one Redis process is running on the server, but there are multiple clients accessing at the same time, the processing capacity of the server will be reduced to a certain extent
  • If multiple Redis processes are started on the same server, Redis will increase the concurrent processing capability and also put a lot of pressure on the server's CPU.
  • Summary: In an actual production environment, you need to decide how many Redis processes to start based on actual needs. If you have higher requirements for high concurrency, you may consider starting multiple processes on the same server, and if the CPU resources are relatively tight, Just use a single process

2. Advantages of Redis

  1. It has a very 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
  2. Support rich data types: support key-value, Strings, Lists, Hashes, Sets and Ordered Sets and other data type operations
  3. Support data persistence: the data in the memory can be saved in the disk, and it can be loaded again for use when restarting
  4. Atomicity: All Redis operations are atomic
  5. Support data backup: data backup in master-salve mode

Redis is a memory-based database, and cache is one of its most commonly used fields.
In addition, the common application scenarios of Redis also include the operation of obtaining the latest N data, ranking applications, counter applications, storage relationships, and real-time Analysis system, log record

3. The difference between Redis and Memcached

/ Memcached Redis
Types of Key-value database Key-value database
Expiration strategy stand by stand by
type of data Single data type Five data types
Endurance not support stand by
Master-slave replication not support stand by
Virtual Memory not support stand by

Three, Redis installation and deployment

Insert picture description here

1. Unzip the package

  • Download the software package portal : redis-5.0.7.tar.gz (extract code: qwer)
  • Transfer the downloaded software package to the /opt/ directory
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

yum -y install gcc gcc-c++ make

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

2. Source code compilation and installation

cd redis-5.0.7/
make -j 4
make PREFIX=/usr/local/redis install

#由于 Redis 源码包中直接提供了 Makefile 文件,所以在解压完软件包后,不用先执行./configure 进行配置
#可直接执行 make  make install 命令进行安装

3. Set up Redis related configuration files

cd /opt/redis-5.0.7/utils/

#执行软件包提供的 install_server.sh 脚本文件来设置 Redis 服务所需要的相关配置文件
./install_server.sh
......
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server
#一直回车,直到出现以下选项,手动修改为“/usr/local/redis/bin/redis-server”
#注意,需要一次性正确输入,且输入错误了是无法更改的

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-OqOzP8Rl-1614070207824)(http://xcf11.oss-cn-beijing.aliyuncs.com/blog/20210223 /154259867.png)]

Selected config:
Port: 6379 The default listening port is 6379
Config file: /etc/redis/6379.conf Configuration file path
Log file: /var/log/redis_6379.log Log file path
Data you / var / lib / redis / 6379 Data file path
Executable: /usr/local/redis/bin/redis-server Executable file path
Cli Executable: /usr/local/bin/redis-cli Client command tool
ln -s /usr/local/redis/bin/* /usr/local/bin/
# redis 的可执行程序文件放入路径环境变量的目录中便于系统识别

netstat -natp | grep "redis"
#当 install_server.sh 脚本运行完毕,Redis 服务就已经启动,默认侦听端口为 6379

vim /etc/redis/6379.conf
#修改配置参数

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

4. View the running status

/etc/init.d/redis_6379 restart
#重启服务,使配置生效

/etc/init.d/redis_6379 status
#查看状态

Insert picture description here

Redis service control (command line) Explanation
/etc/init.d/redis_6379 stop stop
/etc/init.d/redis_6379 start start up
/etc/init.d/redis_6379 restart Reboot
/etc/init.d/redis_6379 status status

Four, Redis common tools

1. Redis command tool

Command Line Explanation
redis-server Tools for starting Redis
redis- benchmark Used to detect the operating efficiency of Redis on the machine
redis-check-aof Repair AOF persistent files
redis-check-rdb Repair RDB persistent files
redis-cli Redis command line tool

2. Redis-cli command line tool

grammar:

redis-cli -h host -p port -a password

例:
redis-cli -h 192.168.126.15 -p 6379

Insert picture description here

Common options Explanation
-h Specify remote host
-p Specify the port number of the Redis service
-a Specify a password, this option can be omitted if the database password is not set

If no option is added, use 127.0.0.1:6379 to connect to the Redis database on the machine

3. Redis-benchmark test tool

redis-benchmark is the official Redis performance testing tool that can effectively test the performance of Redis services

Basic test syntax:

redis-benchmark [选项] [选项值]
Common options Explanation
-h Specify the server host name
-p Specify server port
-s Specify server socket
-c Specify the number of concurrent connections
-n Specify the number of requests
-d Specify the data size of the SET/GET value in bytes
-k 1=keep alive,0=reconnect
-r SET/GET/INCR, use random key, SADD use random value
-p Pipeline requests
-q Force redis to exit, only display query/sec value
–csv Output in CSV format
-l Generate loops and execute tests forever
-t Run only a comma-separated list of test commands
-I Idle mode, only open N idle connections and wait
redis-benchmark -h 192.168.126.15 -p 6379 -c 100 -n 100000
#向 IP 地址为 192.168.126.15,端口为 6379  Redis 服务器发送 100 个并发连接与 100000 个请求以测试性能

redis-benchmark -h 192.168.126.15 -p 6379 -q -d 100
#测试存取大小为 100 字节的数据包的性能

redis-benchmark -t set,lpush -n 100000 -q
#测试本机上 Redis 服务在进行 set  lpush 操作时的性能

Guess you like

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