- Redis is an open source NoSQL database written in C language. It runs based on memory and supports persistence. It adopts the storage form of key value (key value pair). It is an indispensable part of the current distributed architecture
1, Relational database and non relational database
(1) , relational database
Relational database is a structured database, which is created on the basis of relational model (two-dimensional table model) and is generally record oriented. SQL statement (standard data query language) is a language based on relational database, which is used to retrieve and operate the data in relational database. Mainstream relational databases include Oracle, MySQL(mariadb), SQL Server, Microsoft Access, DB2, etc.
(2) , non relational database
- NoSQL (NoSQL=NotOnlySQL), which means "not just SQL", is the general name of non relational databases.
- In addition to the mainstream relational database, all databases are considered non relational.
- Mainstream NoSQL databases include Redis, MongoDB, Hbase, Memcached, Postgresql, etc.
(3) , relational database and non relational database
(1) Different data storage methods
- The main difference between relational and non relational databases is the way of data storage. Relational data is naturally tabular, so it is stored in rows and columns of the data table. Data tables can be associated with each other, stored cooperatively, and data can be easily extracted.
- On the contrary, non relational data is not suitable to be stored in rows and columns of the data table, but large blocks are combined together. Non relational data is usually stored in data sets, such as documents, key value pairs, or graph structures.
Your data and its characteristics are the primary factors in choosing how to store and extract data. ① Relational: it depends on the E-R diagram of the relational model and stores data in a two-dimensional table format at the same time. ② non relational: in addition to storing in a table format, it usually stores data in large blocks
(2) . different expansion modes
- The biggest difference between SQL and NoSQL databases may be in the expansion mode. To support the growing demand, of course, it needs to be expanded.
- To support more concurrency, the SQL database is vertically expanded, that is, to improve processing capacity 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 operations may involve many tables, which need to be overcome by improving computer performance. Although the SQL database has a large expansion space, it will eventually reach the upper limit of vertical expansion.
- The NoSQL database is scale out. Because non relational data storage is naturally distributed, the expansion of NoSQL database can share the load by adding more common database servers (nodes) to the resource pool. ① Relationship: vertical (NATURAL table format) ② non closed: horizontal (NATURAL distributed)
(3) Support for transactional is different
- If the data operation needs high transaction or complex data query needs to control the execution plan, the traditional SQL database is the best choice in terms of performance and stability. SQL database supports atomicity and fine-grained control of transactions (ACID), 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 value is in the scalability of operations and large amount of data processing. ① Relational: especially suitable for tasks with high transactional requirements and the need to control the execution plan ② non relational: it will be slightly weak here, and its value lies in high scalability and large amount of data processing
(4) Background of non relational database
It can be used to deal with the three high problems of Web 2.0 pure dynamic website types.
- (1) High performance ---- high concurrent read and write requirements for the database
- (2) HugeStorage ---------- requirements for efficient storage and access of massive data
- (3) High scalability & & high availability ------ requirements for high scalability and availability of database
Relational database and non relational database have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the database development of Web 2.0. Let relational databases focus on relationships and non relational databases focus on storage. For example, in a MySQL database environment with separate reading and writing, frequently accessed data can be stored in a non relational database to improve access speed.
2, Introduction to Redis
- Redis is an open source NoSQL database written in C language.
- Redis runs based on memory and supports persistence (supports storage on disk). It adopts the storage form of key value (key value pair). It is an indispensable part of the current distributed architecture.
1.Redis server program is a single process model
- Redis service can start multiple redis processes on one server at the same time. The actual processing speed of redis completely depends 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 decline to a certain extent;
- If multiple Redis processes are started on the same server, Redis will put great pressure on the server CPU while improving the concurrent processing capacity. That is, in the actual production environment, you need to decide how many Redis processes to start according to the actual needs. It is suggested that two processes can be started
2. Advantages of Redis
- It has extremely high data 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.
- Support rich data types: support key value, Strings, Lists, Hashes, Sets, Ordered Sets and other data type operations.
- String string (can be integer, floating point and character type, collectively referred to as element)
- list: (implement queue, elements are not unique, first in first out principle)
- Set: (different elements) hash hash value: (the key of hash must be unique) set /ordered sets set / ordered set
- Support data persistence: the data in memory can be saved on disk and can be loaded again for use when restarting.
- Atomicity: all Redis operations are atomicity.
- Support data backup: data backup in master save mode.
Redis is a memory based database, and caching is one of its most common application scenarios. In addition, common redis application scenarios also include operations to obtain the latest N data, leaderboard applications, counter applications, storage relationships, real-time analysis systems, and logging.
[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# yum install -y gcc gcc-c++ make [root@localhost ~]# cd /opt [root@localhost opt]# rz -e [root@localhost opt]# tar zxvf redis-5.0.7.tar.gz [root@localhost opt]# cd redis-5.0.7/ [root@localhost redis-5.0.7]# make [root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install #Since the Makefile file is directly provided in the Redis source package, you do not need to execute. / configure to configure the package after decompressing it. You can directly execute the make and make install commands to install it #Execute the install provided by the software package_ The server.sh script file sets the relevant configuration files required by the Redis service [root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/ [root@localhost utils]# ./install_server.sh Please select the redis port for this instance:  #enter Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] #enter Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] #enter Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] #enter Selected default - /var/lib/redis/6379 Please select the redis executable path  /usr/local/redis/bin/redis-server #Manually add / usr / local / redis / bin / redis server ---------------------------------------------------------------------------- Selected config: Port : 6379 #Default listening port 6379 Config file : /etc/redis/6379.conf #Profile path Log file : /var/log/redis_6379.log #log file path Data dir : /var/lib/redis/6379 #Data file path Executable : /usr/local/redis/bin/redis-server #Executable path Cli Executable : /usr/local/redis/bin/redis-cli #Client command tool -----------------------------------------------------------------------------
#Put the redis executable file into the directory of path environment variable for system identification [root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/ #When install_ After the server.sh script runs, the Redis service has been started. The default listening port is 6379 [root@localhost utils]# netstat -natp | grep redis /etc/init.d/redis_6379 stop #stop it /etc/init.d/redis_6379 start #start-up /etc/init.d/redis_6379 restart #restart /etc/init.d/redis_6379 status #state #Modify the configuration / etc/redis/6379.conf parameter vim /etc/redis/6379.conf bind 127.0.0.1 192.168.184.10 #Line 70, add the listening host address port 6379 #Line 93, Redis's default listening port daemonize yes #Line 137, enable the daemon pidfile /var/run/redis_6379.pid #Line 159, specify the PID file loglevel notice #Line 167, log level logfile /var/log/redis_6379.log #Line 172, specify the log file /etc/init.d/redis_6379 restart netstat -natp | grep redis
vim /etc/redis/6379.conf bind 127.0.0.1 192.168.226.129 #Line 70, add the listening host address port 6379 #Line 93, Redis's default listening port daemonize yes #Line 137, enable the daemon pidfile /var/run/redis_6379.pid #Line 159, specify the PID file loglevel notice #Line 167, log level logfile /var/log/redis_6379.log #Line 172, specify the log file [root@localhost utils]# /etc/init.d/redis_6379 restart [root@localhost utils]# netstat -natp | grep redis
[root@localhost utils]# redis-cli -h 192.168.29.33 -p 6379 #Enter database 192.168.29.33:6379>
4, Redis command tool
redis-server: Used to start Redis Tools for redis-benchmark: For detection Redis Operating efficiency of the machine redis-check-aof: repair AOF Persistent file redis-check-rdb: repair RDB Persistent file redis-cli: Redis Command line tools.
rdb and aof yes redis Two forms of persistence in services RDB AOF redis-cli Commonly used to log in to redis database
1. Redis cli command line tool
redis-cli -h host -p port -a password -h Specify remote host -p appoint Redis Port number of the service -a Specify the password. If the database password is not set, it can be omitted-a option If no option representation is added, 127 is used.0.0.1:6379 Connect the on this machine Redis database
2. Redis benchmark test tool
Redis benchmark is the official redis performance test tool, which can effectively test the performance of redis services.
redis-benchmark [option] [Option value] -h Specify the server host name. -p Specify the server port. -s Specify server socket -c Specifies the number of concurrent connections. -n Specify the number of requests. -d Specified in bytes SET/GET The data size of the value. -k 1=keep alive 0=reconnect . -r SET/GET/INCR Use random key, SADD Use random values. -P Pipeline requests. -q forced return redis. Show only query/sec Value. –csv with CSV Format output. -l Generate a loop to permanently execute the test. -t Run only a comma separated list of test commands. -I Idle pattern. Open only N individual idle Connect and wait. Example: towards IP The address is 192.168.29.11,Port 6379 Redis The server sends 100 concurrent connections and 100000 requests to test the performance redis-benchmark -h 192.168.29.11 -p 6379 -c 100 -n 100000 Test the performance of accessing packets with a size of 100 bytes redis-benchmark -h 192.168.29.11 -p 6379 -q -d 100 Test on this machine Redis Service in progress set And lpush Performance during operation redis-benchmark -t set,lpush -n 100000 -q
5, Common commands of Redis database
set: Store data in command format set key value get: Get data in command format get key
#keys The command can take the list of key values that conform to the rules. Usually, it can be combined with *? And other options. keys * #View all data in the current database keys v* #View data starting with v in the current database keys v?? #View the data in the current database that starts with v and then contains any bit keys v?? #View the data in the current database that starts with v and then contains any two digits
#exists Command can determine whether the key value exists. exists [key] A return value of 1 indicates existence, and 0 indicates nonexistence #del The command can delete the specified key of the current database. del [key] #type The command can obtain the value type corresponding to the key. type [key]
#The rename command renames an existing key. (overlay) rename source key target key use rename The command renames regardless of the target key Whether or not there are any files to be renamed, and the source key The value of overrides the target key Value of. In actual use, it is recommended to use it first exists Command view target key Whether it exists, and then decide whether to implement it rename Command to avoid overwriting important data. #The rename x command renames an existing key and detects whether the new name exists. If the target key exists, it will not be renamed. (do not overwrite) renamenx source key target key
#The dbsize command is used to view the number of key s in the current database.
#Set password config set requirepass password #View the password (once the password is set, the password must be verified first, otherwise all operations are unavailable) auth password config get requirepass #Delete password auth password config get requirepass '' Example: config set requirepass 010230 auth 010230 config get requirepass auth 010230 config get requirepass ''
6, Common commands for Redis multi database
- Redis supports multiple databases. By default, redis contains 16 databases, and the database names are named sequentially with the numbers 0-15.
- Multiple databases are independent and do not interfere with each other.
#Switching between multiple databases select Serial number use redis-cli connect Redis After the database, the database with sequence number 0 is used by default.
#Moving data between multiple databases move Key value serial number Example: select 1 get lucien select 0 get lucien move lucien 1 get lucien select 5 get lic select 0 get lucien
Clear data in database FLUSHDB : Clear current database data FLUSHALL : Clear all database data, use with caution!
- Relational database:
- Instance – > database – > table – > record row (row), data field (column)
- Non relational database:
- Instance – > database – > collection – > key value pair
- Non relational databases do not need to build databases and sets (tables) manually