1. Relational and non-relational databases
Relational Database
A relational database is a structured database, built on a relational model and generally oriented to records
He works with data in databases using mathematical concepts and methods such as set algebra
Relational models refer to two-dimensional table models, so a relational database is a data organization composed of two-dimensional tables and their relationships
In the real world, all kinds of relationships between entities can be represented by relational models sql statements (standard data query language) is a relational database-based language for performing retrieval and operation on relational databases
Common relational databases:
Oracle, MySQL, SQL Server, Microsoft Access, DB2, etc.
Non-relational database
A common non-relational database, nosql (not only sql), means not just sql, but a generic term for a non-relational database
The mainstream nosql databases are redis, mongbd, hbase, couhdb and so on. Their storage, storage structure and usage scenarios are completely different, so we think it is a collection of non-relational databases, rather than a general term like relational databases.
In other words, databases other than the mainstream relational databases are considered non-relational
nosql database is considered the next generation of database products due to its non-relational, distributed, open source and horizontal expansion advantages
Background generation for non-relational databases
Relational databases have been around for a long time, and we've been using them all along, so there's nothing wrong with them
In this case, why does nosql still occur?
Here is a brief introduction to the background of nosql
With the week of web2.0 websites, relational databases expose difficult issues to solve when dealing with web2.0 websites, which despise huge amounts of data and highly concurrent web2.0 pure dynamic websites of the sns (social networking services) type, such as the three-high problem
- high performance - high concurrent read and write requirements for databases
Web 2.0 websites generate dynamic pages and provide dynamic information in real time based on the user's personalized information, so dynamic page static technology cannot be used.
Therefore, the concurrent load of the database will be very high, generally reaching more than 10,000 read and write requests per second
Relational databases can barely support tens of thousands of query requests, but with tens of thousands of write requests, the IO on your hard disk is already unbearable
For ordinary BBS websites, there are often high concurrent requests to write data, such as Lumou announcing a relationship on Weibo, which results in the paralysis of Weibo platform due to excessive traffic. - huge storage -- Enhance storage and access needs for large amounts of data
SNS websites like facebook and friendfeed generate a lot of dynamic user information every day
For example, friendfeed produces 250 million user dynamic information in a month. For relational databases, executing sql queries in a table containing 250 million records is very inefficient - High scalability && high availability - Requirements for high scalability and high availability of databases
In a web architecture, databases are the hardest to scale horizontally.As the number of users and accesses to an application system increases, databases cannot simply extend their performance and load capabilities by adding hardware and server nodes, just like web services.
Especially for some websites that need 24 hours of uninterrupted external service, database upgrade and expansion are often accompanied by downtime maintenance and data migration, which is a huge workload
Relational and non-relational databases have their own characteristics and application scenarios, and their close combination will bring new ideas to the development of web2.0 database
Keep relational databases focused on relationships and non-relational databases focused on storage.
For example, in a read-write separate mysql database environment, frequently accessed data can be stored in a non-relational database to improve access speed
Introduction to Redis
redis (remote dictionary server) is an open source nosql database written in C
redis runs in memory and supports persistence. It uses key-value pairs as storage form and 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 run simultaneously on a single server, while the actual processing speed of redis is entirely dependent on the execution efficiency of the main process
If only one redis process is run on the server, the processing power of the server will decrease to some extent when multiple clients access it simultaneously.
If multiple redis processes are opened on the same server, redis can increase concurrent processing power and put a lot of pressure on the server's cpu
That is, in a real production environment, you need to decide how many redis processes to start based on your actual needs
If the high concurrency requirement is higher, you might consider starting multiple processes on the same server.
If CPU resources are tight, a single process can be used.
Advantages of redis
- It has a very high speed of reading and writing data, with a speed of up to 110,000 times per second and a speed of up to 80,000 times per second.
- Supports rich data types, not only simple key-value data, but also strings, lists, hashes, sets, and ordered sets
- Supports persistence of data, can save data in memory on disk, restart can be loaded again for use
- Atomicity, redis All operations are atomic
- Supports data backup, that is, master-slave mode data backup
Deploy Redis
Redis installation is simpler than other services. First, you need to go to the Redis official network. https://www.redis.io) Download the corresponding source package And upload it to the server on the Linux system to unzip it
Typically, source code compilation and installation on Linux systems requires that you first perform a. /configure environment check and configuration to generate a Makefile file, then execute the make && make install command for compilation and installation.The Makefile file file is provided directly in the Redis source package, so after unpacking the package, you can go directly to the unpacked package directory and execute make and make install commands for installation.
make install only installs binaries into the system, and does not launch scripts and configuration files.An install_server.sh script file is provided by default in the package that allows you to 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
Install compilation tools, unzip Redis, compile and install[root@localhost ~]# yum install gcc gcc-c++ make -y [root@localhost ~]# mkdir /abc mkdir: cannot create directory '/abc': File exists [root@localhost ~]# mount.cifs //192.168.254.10/linuxs /abc Password for root@//192.168.254.10/linuxs: [root@localhost ~]# cd /abc [root@localhost abc]# tar zxvf redis-5.0.7.tar.gz -C /opt [root@localhost abc]# cd /opt/redis-5.0.7/ [root@localhost redis-5.0.7]# ls 00-RELEASENOTES deps README.md runtest-moduleapi tests BUGS INSTALL redis.conf runtest-sentinel utils CONTRIBUTING Makefile runtest sentinel.conf COPYING MANIFESTO runtest-cluster src [root@localhost redis-5.0.7]# make [root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install //Specify installation path [root@localhost redis-5.0.7]# cd /usr/local/redis/ [root@localhost redis]# ls bin [root@localhost redis]# cd bin/ [root@localhost bin]# ls redis-benchmark redis-check-rdb redis-sentinel redis-check-aof redis-cli redis-server [root@localhost bin]# cd /opt/redis-5.0.7/ [root@localhost redis-5.0.7]# ls 00-RELEASENOTES deps README.md runtest-moduleapi tests BUGS INSTALL redis.conf runtest-sentinel utils CONTRIBUTING Makefile runtest sentinel.conf COPYING MANIFESTO runtest-cluster src [root@localhost redis-5.0.7]# cd utils/ [root@localhost utils]# ls build-static-symbols.tcl hashtable redis_init_script.tpl cluster_fail_time.tcl hyperloglog redis-sha1.rb corrupt_rdb.c install_server.sh releasetools create-cluster lru speed-regression.tcl generate-command-help.rb redis-copy.rb whatisdoing.sh graphs redis_init_script [root@localhost utils]# . /install_server.sh //This script is in the utils of the installation package Please select the redis port for this instance: [6379] //Enter, here is the port number Please select the redis config file name [/etc/redis/6379.conf] //Enter, here is the profile location Please select the redis log file name [/var/log/redis_6379.log] //Enter, here is the log file location Please select the data directory for this instance [/var/lib/redis/6379] //Enter, data file location Please select the redis executable path [] //Extend path, need to input/usr/local/redis/bin/redis-server yourself, can not be deleted, note when writing Is this ok? Then press ENTER to go on or Ctrl-C to abort. //Enter 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/bin/redis-cli ####Client Command Line Tools Installation successful! //complete [root@localhost utils]# cd /etc/redis/ [root@localhost redis]# ls 6379.conf //Main Profile [root@localhost redis]# ln -s /usr/local/redis/bin/* /usr/local/bin/ [root@localhost redis]# netstat -natp | grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 21765/redis-server //Now that it is running, turn off using the following command [root@localhost redis]# /etc/init.d/redis_6379 stop Stopping ... Waiting for Redis to shutdown ... Redis stopped [root@localhost redis]# netstat -natp | grep 6379 tcp 0 0 127.0.0.1:6379 127.0.0.1:40470 TIME_WAIT - [root@localhost redis]# /etc/init.d/redis_6379 start Starting Redis server... [root@localhost redis]# /etc/init.d/redis_6379 start [root@localhost redis]# netstat -natp | grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 25610/redis-server tcp 0 0 127.0.0.1:6379 127.0.0.1:40470 TIME_WAIT - [root@localhost redis]# Redis-cli-h 192.168.247.206-p 6379 //connect redis Could not connect to Redis at 192.168.247.206:6379: Connection refused //Displays that the connection is not possible because the service is not fully enabled, no-h-p IP is specified, local logon is possible //If you want to specify an IP address, you need to add a listening address to the profile //vi /etc/redis/6379.conf //Bid 127.0.0.1 192.168.247.206 ##Need to change the IP address of the plus-end //port 6379 ## Port does not need to be changed by default //daemonize yes ###By default, no change is required to enable the daemon //pidfile /var/run/redis_6379.pid ##No change to the specified PID file is required by default //loglevel notice ###No change to log level is required by default //Logfile/var/log/redis_6379.log ###No change to the specified log file is required by default not connected> exit [root@localhost redis]# systemctl stop firewalld [root@localhost redis]# setenforce 0 [root@localhost redis]# redis-cli 127.0.0.1:6379> exit [root@localhost redis]# vim /etc/redis/6379.conf 70 bind 127.0.0.1 192.168.247.206 //Add Listening Address [root@localhost redis]# /etc/init.d/redis_6379 restart Stopping ... Waiting for Redis to shutdown ... Redis stopped Starting Redis server... [root@localhost redis]# redis-cli -h 192.168.247.206 -p 6379 192.168.247.206:6379>
Redis and deployment complete
In addition to the above configurations, Redis has other configuration parameters
Basic operations for redis
Redis software provides several command tools, which are installed when Redis is installed
In the system, it can be used directly in the system.The functions of these command tools are shown below.
redis-server: Tool to start Redis
redis-benchmark: Used to detect Redis's local operating efficiency
< redis-check-aof: Repair AOF persistence file
redis-check-rdb: Repair RDB persistence file
redis-cli: Redis command line tool
redis-setinel: A soft link to a redis-server file
redis-cli
When performing database connection operations, you can specify the Redis database on the remote host by choosing the command syntax redis-cli-h host-p Port-A password.Where -h specifies the remote host, -p specifies the port number of the Redis service, and -a specifies the password.Connect to the Redis database on your computer without adding any options; omit the -a option if the database password is not set.For example, execute the following command to connect to the Reedis database with host 192.168.10.161 and port 6379 and view statistics for the redis service.To exit the database operation environment, execute
The "exit" or "quit" command returns the restored Shell environment.
- Set: Stores data in the basic command format set key value.
- Get: Get data, the basic command format is get key.
- Keys: you can take a list of keys that match the rules, usually in combination with *,?And so on to use
- Exists: You can tell if a key value exists.
- del: the specified key for the current database can be deleted
- Type: you can get the value type for the key
- Rename: Renames an existing key with the command format rename source key and target key.When renamed using the rename command, the target key is renamed regardless of whether it exists or not, and the value of the source key overrides the item
The value of the label key.In practice, it is recommended that you use the exists command to see if the target key exists before deciding whether to execute the rename command
To avoid overwriting important data - Renamenx: Renames an existing key and detects if a new name exists.Its command format is basically the same as rename's command format except for the command keyword, renamenx source key target key.When renamed using the renamenx command, do not rename if the target key exists
- dbsize: View the number of key s in the current database
redis-benchmark test tool
Redis-benchmark is an official Redis performance test tool that can effectively test the performance of Redis services.The Basic test syntax is redis-benchmark [option] [option value].
-h: Specify the server host name.
-p: Specify the server port.
-s: Specify the 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.
-k: 1=keep alive 0=reconnect .
-r:SET/GET/INCR uses random keys, SADD uses random values.
-P: Pipe requests.
-q: Force exit redis.Show only query/sec values.
- csv: Output in CSV format.
-l: Generate a loop to permanently execute the test.
-t: Run only a comma-separated list of test commands.
-I:Idle mode.Open only N idle connections and wait.
Combining the above options, you can perform performance detection on a Redis server, such as redis-benchmark-h
192.168.191.132 -p 6379 -c 100 -n 100000 command to IP address 192.168.191.132, port
Send 100 concurrent connections and 100000 requests to 6379 Reedis server to test performance.[root@localhost redis]# redis-benchmark -h 192.168.191.132 -p 6379 -c 100 -n 100000 ====== SET ====== 100000 requests completed in 1.54 seconds ====== GET ====== 100000 requests completed in 1.47 seconds [root@localhost redis]# redis-benchmark -h 192.168.191.132 -p 6379 -q -d 100
Test libraries and libraries
Switching between multiple databases
Redis supports multiple databases. Redis includes 16 databases by default without any changes. The database names are named in turn using the numbers 0-15.
Use the Select command to switch between Redis's multiple databases in the form of selectindex, where index represents the serial number of the database.
When you connect to a Redis database using redis-cli, the default is to use a database with a sequence number of 0.When the database is switched using the select command, the database ordinal number such as "192.168.191.132:6379[10]>" is displayed in the prompt on the front end, indicating that the database with ordinal 10 is currently in use.
If the database currently in use is a database with a sequence number of 0, no sequence number is displayed in the prompt, such as "192.168.191.132:6379>" to indicate that the database with a sequence number of 0 is currently in use.
409