Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Talk about Redis (non-relational database) in Centos 7

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Blog catalogue

I. Relational database and non-relational database

II. Brief introduction of Redis database

III. Redis installation and deployment

4. Redis command tool

Redis database is a non-relational database. Before we discuss Redis, we first understand the concept of relational database and non-relational database.

I. Relational database and non-relational database

According to its structure, database can be divided into relational database and other databases, and these other databases are collectively referred to as non-relational databases.

1. Relational database

Relational database is a structured database, which is based on relational model and is generally record-oriented. It deals with the data in the database with the help of mathematical concepts and methods such as set algebra. A relational model refers to a two-dimensional table model, so a relational database is a data organization composed of two-dimensional tables and their relationships. In practice, all kinds of relationships between various entities can be represented by a relational model. SQL statement is a language based on relational database, which is used to retrieve and manipulate data in relational database.

The mainstream relational databases include Oracle, MySQL, SQL Server, Microsoft Access, DB2 and so on.

2. Non-relational database

NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is the general name of non-relational database. The mainstream NoSQL databases are Redis, MongBD, CouhDB and so on. The storage mode, storage structure and usage scenarios of these databases are completely different. So we think of it as a collection of non-relational databases, rather than a general term like relational databases. In other words, databases other than mainstream relational databases are non-relational. NoSQL database is considered as the next generation database product because of its advantages such as non-relational, distributed, open source and horizontal expansion.

3. Non-relational database can solve the three high problems caused by relational database.

High concurrent read and write requirements for database by High performance--

Huge Storage-- demand for efficient storage and access of massive data

High Scalability & & High Availability-- requires high scalability and high availability of database

Both relational database and non-relational database have their own characteristics and application scenarios. The precise combination of the two will bring new ideas to the development of Web 2.0database. Let relational databases focus on relationships, while non-relational databases focus on storage.

II. Brief introduction of Redis database

Redis is an open source, written in C language, supports the network, memory-based and persistent log type, key-value (key-value pair) database, is an indispensable part of the current distributed architecture.

Redis server is a single-process model, that is, multiple Redis processes can be started on one server at the same time, while 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, the server's processing capacity will decrease to a certain extent when multiple clients visit at the same time; if multiple Redis processes are started on the same server, Redis will cause great pressure on the server's CPU while improving the concurrent processing capacity. In other words, in the actual production environment, you need to decide how many Redis processes to start according to the actual needs. If you have a higher requirement for high concurrency, you may consider starting multiple processes on the same server; if CPU resources are tight, you can use a single process.

Redis has the following characteristics:

It has extremely high reading and writing speed, the maximum data reading speed can reach 110000 times / s, and the maximum data writing speed can reach 81000 times / s.

Supports rich data types, not only simple key-value data types, but also strings, lists, hashes, sets and ordered sets data type operations

Data persistence is supported. The data in memory can be saved on disk and can be loaded and used again when restarting.

Atomicity, all operations of Redis are atomic

Support data backup, that is, data backup in master-salve mode

As a memory-based database, caching is one of the most common application scenarios of Redis. In addition, Redis common application scenarios also include operations to obtain the latest N data, ranking applications, counter applications, storage relationships, real-time analysis systems, logging and so on.

III. Redis installation and deployment

Redis official website: https://www.redis.io or: https://pan.baidu.com/s/1xdDZzTGdOOWFwpJtZtwRaw

Extraction code: xuqg downloads the corresponding source code package, and then uploads it to the server of the Linux system for decompression and installation.

Usually, to compile and install the source code in the Linux system, you need to execute. / configure to check and configure the environment, so as to generate Makefile files, and then execute the make & & make install command to compile and install. The Makefile file is directly provided in the Redis source package, so after decompressing the package, you can directly enter the decompressed package directory and execute the make and make install commands to install it.

[root@centos01] # tar zxvf redis-3.2.9.tar.gz-C / usr/src/ [root@centos01 ~] # mv / usr/src/redis-3.2.9/ / usr/src/redis/ [root@centos01 ~] # cd / usr/src/redis/ [root@centos01 redis] # ls00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel testsBUGS deps MANIFESTO runtest sentinel. Conf utilsCONTRIBUTING INSTALL README.md runtest-cluster src [root@centos01 redis] # make & & make install

Make install simply installs binaries into the system without startup scripts and configuration files. An install_server.sh script file is provided by default in the package, through which you can set up the relevant configuration files required by the Redis service. When the script is finished, the Redis service is started, and the default listening port is 6379.

[root@centos01 ~] # cd / usr/src/redis/utils/ [root@centos01 utils] #. / install_server.sh Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/ etc/redis/6379.conf] Selected default-/ etc/redis/6379.confPlease select the redis log file name [ / var/log/redis_6379.log] Selected default-/ var/log/redis_6379.logPlease select the data directory for this instance [/ var/lib/redis/6379] Selected default-/ var/lib/redis/6379Please select the redis executable path [/ usr/local/bin/redis-server] Selected config:Port: 6379 Config file: / etc/redis/6379.conf Log file: / var/log/redis_6379.log Data dir: / var/lib/redis/6379 Executable: / usr/local/bin/redis-server Cli Executable: / usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort.Copied / tmp/6379.conf = > / etc/init.d/redis_6379Installing service...Successfully added to ChkconfigSuccessful added to runlevels 345 starting Redis server...Installation successful! [root@centos01 ~] # netstat-anptu | grep redistcp 0 0 127.0. 0. 1 LISTEN 5141/redis-server 6379 0. 0. 0. 0.

After installation, you can control the Redis service through the Redis service control script / etc/init.d/redis_6379, such as stopping the Redis service, starting the Redis service, restarting the Redis service, and checking the running status of the Redis.

[root@centos01 ~] # / etc/init.d/redis_6379 start [root@centos01 ~] # / etc/init.d/redis_6379 stop [root@centos01 ~] # / etc/init.d/redis_6379 restart [root@centos01 ~] # / etc/init.d/redis_6379 status 1, configuration parameters

The Redis main configuration file is / etc/redis/6379.conf, which consists of comment lines and settings lines. Like most Linux configuration files, annotative text begins with "#" and contains descriptions and explanations of the relevant configuration content. Content other than the comment line and the blank line is the setting line. The relevant parameters can be adjusted according to the needs of the production environment. Such as:

[root@centos01 ~] # vim / etc/redis/6379.conf 62 bind 192.168.100.10 85 port 6379 129 daemonize yes pidfile / var/run/redis_6379.pid loglevel notice logfile "/ var/log/redis_6379.log" [root@centos01 ~] # / etc/init.d/redis_6379 restart

In addition to the above configuration parameters, many other configuration parameters are included in the Redis main configuration file, as follows:

4. Redis command tool

Redis software provides multiple command tools. When Redis is installed, the included software tools will be installed into the system at the same time and can be used directly in the system. The functions of these command tools are as follows:

Redis-server: a tool for launching Redis

Redis-benchmark: used to test the running efficiency of Redis on the local machine

Redis-check-aof: repairing AOF persistence files

Redis-check-rdb: repairing RDB persistence files

Redis-cli:Redis command line tool

Soft connection of redis-setinel:redis-server files; 1. Redis-cli command line tools

Redis database system is also a typical application of client / server architecture. Special client software is needed to access Redis database. The client software of Redis service is its own redis-cli command line tool. Use redis-cli to connect to the specified database, and after a successful connection, you will enter the database operating environment with the prompt "remote host IP address: Port number >".

Users can enter various operation statements to manage the database, such as executing the ping command to detect whether the redis service is started.

[root@centos01 ~] # redis-cli 192.168.100.10 ping PONG192.168.100.10:6379 > exit

When making a database connection operation, you can specify the Redis database on the remote host through the option, and the command syntax is redis-cli-h host-p port-a password. Among them, "- h" specifies the remote host; "- p" specifies the port number of the service. If the password is set, you can specify the password with "- a". If you do not set the connection password, you can omit the "- a" option.

[root@centos01] # redis-cli-h 192.168.100.10-p 6379 192.168.100.10 info# Serverredis_version:3.2.9redis_git_sha1:00000000redis_git_dirty:0redis_build_id:2cc10a3065b1ebc2. Db0:keys=1,expires=0,avg_ttl=0192.168.100.10:6379 > exit

In the database operating environment, you can get help for command types by using the help command. There are three ways to get command help:

Help @: get the list of commands in

Help: get help for a command

Help: get a list of topics that may help

The specific operation mode is as follows:

[root@centos01 ~] # redis-cli 192.168.100.10 help @ list BLPOP key [key.] Timeout summary: Remove and get the first element in a list, or block until one is available since: 2.0.0 BRPOP key [key...] Timeout summary: Remove and get the last element ina list, or block until one is available since: 2.0.0 BRPOPLPUSH source destination timeout summary: Pop a value from a list, push it to another list and return it Or block until one is available since: 2.2.0 LINDEX key index summary: Get an element from a list by its index since: 1.0.0 LINSERT key BEFORE | AFTER pivot value summary: Insert an element before or after another element in a list since: 2.2.0 LLEN key summary: Get the length of a list since: 1.0.0 LPOP key summary: Remove and get the first element in a list since: 1.0.0 LPUSH key value [value...] Summary: Prepend one or multiple values to a list since: 1.0.0 LPUSHX key value summary: Prepend a value to a list Only if the list exists since: 2.2.0 LRANGE key start stop summary: Get a range of elements from a list since: 1.0.0 LREM key count value summary: Remove elements from a list since: 1.0.0 LSET key index value summary: Set the value of an element ina list by its index since: 1.0.0 LTRIM key start stop summary: Trim a list to the specified range since: 1.0.0 RPOP key summary: Remove and get the last element ina list since: 1.0.0 RPOPLPUSH source destination summary: Remove the last element ina list Prepend it to another list and return it since: 1.2.0 RPUSH key value [value...] Summary: Append one or multiple values to a list since: 1.0.0 RPUSHX key value summary: Append a value to a list, only if the list exists since: 2.2.0127.0.0.1 summary 6379 > help set SET key value [EX seconds] [PX milliseconds] [NX | XX] summary: Set the string value of a key since: 1.0.0 group: string127.0.0.1:6379 > help get GET key summary: Get the value of a key since: 1.0.0 group: string2, redis-benchmark testing tool

Redis-benchmark is the official Redis performance testing tool, which can effectively test the performance of Redis services. The basic test syntax is redis-benchmark [option] [option value]. The common options are as follows:

-h: specify the server hostname

-p: specify the server port

-s: specify server socket

-c: specify the number of concurrent connections

-n: specify the number of requests

-d: specifies the data size of the SET/ get value in bytes (B)

-k:1=keep alive 0=reconnect

-r:SET/GET/INCR uses random key,SADD to use random values

-p: transmit the request through the pipeline

-Q: forcibly exits redis. Show only query/ secvalue

-- csv: output in CSV format

-l: generate a loop that permanently executes the test

-t: only run a comma-separated list of test commands

-i:idle mode. Open only N idle connections and wait

Combined with the above options, you can test the performance of a Redis server, for example, by executing the redis-benchmark-h 192.168.100.10-p 6379-c 100-n 10000 command to send 100 concurrent connections and 10000 requests to the Redis server with IP address 192.168.100.10 and port 6379:

[root@centos01] # redis-benchmark-h 192.168.100.10-p 6379-c 100-n 10000. 5165.29 requests per second= MSET (10 keys) = 10000 requests completed in 0.18 seconds 100 parallel clients 3 bytes payload keep alive: 10.01% get xingming "liyanxin" 192.168.100.10 keys > get xingbie "nan" 1, Key related commands 1) keys

* use the keys command to obtain a list of key values that conform to the rules, which can usually be combined with,? And other options to use. **

192.168.100.10 2OK192.168.100.10:6379 6379 > set K1 1OK192.168.100.10:6379 > set K2 2OK192.168.100.10:6379 > set v1 3OK192.168.100.10:6379 > set v2 4OK192.168.100.10:6379 > set v33 6OK192.168.100.10:6379 > KEYS * 1) "xingming" 2) "xingbie" 3) "K1" 4) "K2" 5) "v1" 6) "v2" 7) "v33" 192.168.100. 10RV 6379 > KEYS v* 1) "v1" 2) "v2" 3) "v33" 192.168.100.10 KEYS v? 1) "v1" 2) "v2" 192.168.100.106379 > KEYS v1) "v33" 192.168.100.106379 > KEYS K1) "K2" 2) "K1" 192.168. 100.10 6379 > KEYS k? 1) "K2" 2) "K1" 2) exists

Use the exists command to determine whether a key value exists.

192.168.100.10 EXISTS xingming 6379 > EXISTS xingming (integer) 1192.168.100.10 EXISTS xingbie (integer) 1192.168.100.10 EXISTS xingbie 6379 > EXISTS danwei (integer) 0192.168.100.10 integer > EXISTS nianling (integer) 03) del

Use the del command to delete the specified key for the current database.

192.168.100.10xingbie * 1) "xingming" 2) "xingbie" 3) "K1" 4) "K2" 5) "v1" 6) "v2" 7) "v33" 192.168.100.10 del 6379 > del K2 (integer) 1192.168.100.10xingbie 6379 > del v2 (integer) 14) type

Use the type command to get the value type corresponding to key.

192.168.100.10 rename 6379 > type v33string192.168.100.10:6379 > type xingbiestring5)

Use the rename command to rename an existing key. (when renaming with the rename command, it is renamed regardless of whether the target key exists or not, and the value of the source key overrides the value of the target key. In practice, it is recommended to use the exists command to check whether the target key exists, and then decide whether to execute the rename command to avoid overwriting important data.

192.168.100.10xingbie 6379 > KEYS * 1) "xingming" 2) "xingbie" 3) "K1" 4) "K2" 5) "v1" 6) "v2" 7) "v33" 192.168.100.10 rename 6379 > rename v33 v3OK192.168.100.10:6379 > keys vane 1) "v1" 2) "v3" 192.168.100.106379 > get v1 "3" 192.168.100.10v6379 > get v3 "6" 192.168 .100.10get v1 v3OK192.168.100.10:6379 > get v1 (nil) 192.168.100.10 nil get v3 "3" 6) renamenx

Use the renamenx command to rename an existing key and detect whether the new name exists. (when renaming with the renamenx command, the rename is not performed if the destination key exists.)

192.168.100.10xingbie 6379 > KEYS * 1) "xingming" 2) "xingbie" 3) "K1" 4) "v3" 192.168.100.106379 > get xingming "liyanxin" 192.168.100.106379 > get K1 "127.0.0.16379 > renamenx K1 xingming (integer) 0192.168.100.106379 > KEYS * 1)" xingming "2)" xingbie "3)" K1 "4)" v3 "192.168.100.100.10 xingming 6379 > get K1" 1 "7) dbsize

Use the dbsize command to see the number of key in the current database.

192.168.100.10 6379 > dbsize (integer) 62, common commands for multiple databases 1) switching between multiple databases

Redis supports multiple databases, and Redis contains 16 databases by default without any changes, and the database names are named in turn using the number 0Mur15. Use the select command to switch between Redis's multi-database, with the command format "select index". Where index represents the serial number of the database. After using redis-cli to connect to the redis database, the database with serial number 0 is used by default.

As shown below, after using the select command to switch the database, the current database sequence number is displayed in the front prompt, such as "192.168.100.10pur6379 [5] >" indicates that the database with sequence number 5 is currently in use. If the database currently used is a database with a serial number of 0, the serial number is not displayed in the prompt, such as "192.168.100.10pur6379 >" indicates that the database with serial number 0 is currently being used.

192.168.100.10select 6379 > select 5 OK192.168.100.10:6379 [5] > keys * (empty list or set) 192.168.100.10keys 6379 [5] > select 10 OK192.168.100.10:6379 [10] > keys * (empty list or set) 192.168.100.106379 [10] > select 0 OK192.168.100.10:6379 > keys * 1) "K1" 2) "xingming" 3) "v3" 4) "key:__rand_int__" 5) "xingbie" 6) "mylist" 2) Mobile data between multiple databases

Redis's multiple repositories are quite independent to some extent. For example, W1 data stored on database 0 is not visible on other 1x 15 databases.

192.168.100.10OK192.168.100.10:6379 6379 > set w1 100 OK192.168.100.10:6379 > get w1 "100" 192.168.100.10 OK192.168.100.10:6379 6379 > select 1 OK192.168.100.10:6379 [1] > get w1 (nil) 192.168.100.10 OK192.168.100.10:6379 6379 [1] > select 0 OK192.168.100.10:6379 > get w1 "100" 192.168.100.10 move 6379 > move w1 (integer) 1192.168.100.10 integer > select OK192 1 .168.100.10: 6379 [1] > get w1 "100" 192.168.100.10 OK192.168.100.10:6379 637979 [1] > select 0 OK192.168.100.10:6379 > get w13) clear the data in the database

The deletion of the whole database of Redis database is mainly divided into two parts: clearing the data in the current database, using the flushdb command to achieve; emptying all the data in the database, using the flushall command. Database emptying operation is more dangerous, generally not recommended in the production environment!

192.168.100.10 purl 6379 > FLUSHALLOK

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report