Non-relational databases (NoSQL) - Redis Detailed installation and deployment

In the current tide of the Internet, NoSQL are household names, Redis NoSQL and its importance as one, so the only way we move towards a framework of the road. As the operation and maintenance engineers, it is necessary to master!

As we mentioned in the Redis database non-relational data, and need to know Redis database. So the basic concepts of relational databases and non-relational databases is essential to understand.

First, the basic concepts of relational databases and non-relational databases:

Database structure can be divided according to their relational database with other databases, and these other databases, we referred to a non-relational database.

1. Relational Databases

Relational database is a structured database, created in the relational model, based on the general-oriented record. It is set by means of mathematical concepts and methods to process algebra data in the database. Relational model refers to the form of two-dimensional model, which is a relational database and a data link between the organization of two-dimensional tables, the real world, the linkages between the various entities and entity relationship model can be used To represent. SQL (Structured Query Language) statements is based on relational database language for relational databases perform retrieval of data and operations.

Mainstream relational databases including Oracle, MySQL, SQL Server, Microsoft Access, DB2, etc.

2. Non-relational databases

NoSQL: means "not only SQL", the general term for a non-relational database. Mainstream NoSQL database has Redis, MongDB, Hbase, CouhDB and so on. These databases, their storage, storage structure and usage scenarios are completely different. So we think it is a collection of non-relational database, rather than a relational database, is a generic name. In short: a database outside the mainstream relational database, can be a non-relational database. NoSQL databases by virtue of its non-relational, distributed, open source and scale advantages, is considered the next generation of database products.

3. The non-relational database background:

Relational databases have been born a long time, and we have been using, what problems did not happen, the face of this situation, why can produce non-relational database?

With the rise of Web 2.0 sites, relational databases in response to Web 2.0 sites, especially when huge amounts of data and high concurrent SNS (Social Network Service) type of Web 2.0 website, exposed a lot of difficult issues. The main problem is that three high:

1) demand for high concurrent read and write database

Web 2.0 sites in real time to generate dynamic pages and provides dynamic information based on the user's personalized information. Therefore, you can not use dynamic pages static technology, so concurrent database load is very high, generally up to 10,000 times / s or more read and write requests. Relational database to query a million times on request or can barely support. When there is a million times on a request to write data, disk I / O has been unable to afford them. For ordinary BBS site, there will often write highly concurrent data requests. For example: the star broke the news and so on, there will be a lot of people comment. The results most likely because of the amount of traffic caused by website crashed.

2) efficient storage in the mass data access requirements

Similar to the large-scale social networking sites such as: Facebook, Friendfeed and so on. Every day will have a large user dynamic information. For example: Facebook month will have 250 million users dynamic information. For relational databases, execute SQL queries in a table that contains 250 million record. Efficiency is very low.

3) database scalability and high availability requirements

In the Web architecture, the database is the most difficult to scale-out. When the user application system and the amount of traffic increasing, the database is no way like Web servers, simply prepared by adding hardware and server nodes to extend its performance and load capacity. Especially for some sites provide up to 24 hours outside the server, the upgrade and expansion of the database is often accompanied by the maintenance and downtime data migration, its workload is very large.

Relational databases and non-relational database has its own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of a database of Web 2.0. Let relational databases attention on the relationship between non-relational databases attention on the storage.

By the above, we can generally understand some basic concepts of non-relational, followed by further understanding - Redis database .

Two, Redis database infrastructure

1.Redis Server Introduction

Redis is an open source, written in C, support network, based on the persistence of memory can log type, key-value (key-value pairs) database, currently distributed architecture is an integral part of a ring.

Redis server program is a single process model, that is, Redis can start multiple processes simultaneously on a single server, while the actual processing speed Redis is totally dependent on the efficiency of the primary process. If a Redis running processes on the server only, when multiple clients simultaneously access a server's processing capacity will be decreased to some extent, if the Redis open multiple processes on the same server, Redis while improving the ability of concurrent processing the server's CPU will cause a lot of pressure. In other words, in the actual production environment, according to the actual needs to decide how many Redis process has been started. If high concurrency requirements higher, may consider open multiple processes on the same server. If the CPU resource shortages, a single process can be.

redis has the following significant advantages:

  • Has a very high access speed, data read speed can reach 110,000 times / s; data write speeds of up to 81,000 times / s;
  • Support for rich data types, not only supports simple key-value data types, supports Strings, Lists, Hashes, Sets and Ordered Sets data such as the type of operation;
  • Support data persistence, data in memory can be saved on the hard disk, restart can be loaded using again;
  • Atomicity, Redis all operations are atomic;
  • It supports data backup, and data backup master-salve mode;

Redis database as running a database memory, the cache is one of its more scene often used, in addition, Redis common scenarios further comprising N data acquisition operation, the chart application, the application counter, the storage relationship, the implementation of the analysis system , logging and so on.

2.Redis database deployment

Installation is relatively simple phase Redis database for other services, it can be through the official website to download the appropriate software package, you can also use the network disk link I posted: https://pan.baidu.com/s/1bOhIr3yJmSVUpjTdv_x3Kw
extraction code: zp79
upload Linux servers to decompress, install it!

Typically in the Linux source code compile and install the system, you need to perform ./configure environmental inspection and configuration. Thereby generating a Makefile file and then make && make install commands compile and install, and Redis provides a direct source package Makefile file, unzip it after completion of the package, the package directly into the unpacked directory, execute make && make install command compile install it! as follows:

[root@localhost ~]# tar zxf redis-3.2.9.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/redis-3.2.9/
[root@localhost redis-3.2.9]# make && make install

In this way the above installation, redis service default installation location is /usr/src/redis-3.2.9/src

During installation, if you want to install to the specified directory it is best to enter the appropriate directory, and then perform the appropriate actions can be! You can also use the following command:

[root@localhost redis-3.2.9]# make && make PREFIX=/usr/local/ install
//安装到/usr/local目录

make install just the binary file to install the system, and no startup scripts and configuration files. The default package provides a install_server.sh script file. You can set the relevant configuration file Redis services required by the script file. When the script is finished, Redis service has been launched, the default listening port is 6379. Specific command is as follows:

[root@localhost redis-3.2.9]# cd /usr/src/redis-3.2.9/utils/
[root@localhost utils]# ./install_server.sh 
//进入指定的目录,运行脚本
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]                                             //设置端口号 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]                              //设置默认的配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]                             //设置日志文件
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]                  //设置数据目录
Selected default - /var/lib/redis/6379
Please 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_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
//以上这些信息选择默认即可!
[root@localhost ~]# netstat -anpt | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      6396/redis-server 1 
//确认监听端口为6379

After the installation is complete (default server has been started) you can use the service control script Redis Redis service control command is as follows:

[root@localhost ~]# /etc/init.d/redis_6379 status
Redis is running (6396)
[root@localhost ~]# /etc/init.d/redis_6379 stop
[root@localhost ~]# /etc/init.d/redis_6379 restart

3. Configuration Parameters

Redis master configuration file for /etc/redis/6379.conf, the default configuration file to meet the demand, of course, the configuration file can be modified according to the actual situation, the general content of the configuration file:

[root@localhost ~]# vim /etc/redis/6379.conf 
                  ………………                                                            //省略部分内容
bind 127.0.0.1 192.168.1.1                                                        //监听的主机地址(最好加上自己的IP地址)
port 6379                                                                                   //监听端口
daemonize yes                                                                          //启动守护进程
pidfile /var/run/redis_6379.pid                                                   //指定PID文件
loglevel notice                                                                           //日志级别
logfile /var/log/redis_6379.log                                                  //指定日志文件

//redis服务的配置文件几乎不需要修改任何内容就可直接工作!
//修改完配置文件重启服务,这是常识,不要大意!!!
[root@localhost ~]# /etc/init.d/redis_6379 restart

About Redis optimization services can refer to the parameters are:
Non-relational databases (NoSQL) - Redis Detailed installation and deployment

Three, Redis service command tool

Redis software provides a plurality of command tool, when Redis installation, the software tool will also be included mounted to the system, it can be used directly in the system.

Redis service tool command role:

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

这篇博文主要介绍redis-cli、redis-benchmark命令工具的使用。

1.redis-cli命令行工具

Redis数据库系统也是一个典型的C/S(客户端/服务器端)架构的应用,要访问Redis数据库需要使用专门的客户端软件。Redis服务的客户端软件就是自带的redis-cli命令行非法操作。使用redis-cli连接到指定数据库,连接成功后会进入提示符为“远程主机IP地址:端口号>”的数据库操作环境。用户就可以输入各种操作语句对数据库进行管理。比如:

[root@localhost ~]# redis-cli        //连接到本机的Redis数据库
127.0.0.1:6379> ping                  //检测Redis服务是否为启动状态
PONG

在进行数据库连接操作时,客户端(前提是支持redis命令工具)可以通过选项来指定远程主机上的Redis数据库(Redis服务器配置文件中必须有真实的IP地址),实验环境,我就把Redis服务器的防火墙强行关闭了,实际环境中,肯定是要开启相应端口。命令格式如下:

[root@localhost ~]# redis-cli -h 192.168.1.1 -p 6379
192.168.1.1:6379> info                        //登录成功,使用“info”可以查看Redis服务器的详细信息
//“-h”指定远程主机、“-p”指定Redis服务的端口号、“-a”指定redis数据库的密码(如果没有密码即可省略)

在数据库操作环境中,使用help命令可以获取命令的相应帮助。

有三种获取命令帮助的方式:

  • help @<group>:获取<group>中的命令列表
  • help <command>:获取某个命令的帮助
  • help <tab>:获取可能帮助的主题列表

具体操作方法如下:

[root@localhost ~]# redis-cli 
127.0.0.1:6379> help @list                           //查看所有与list数据类型相关的命令
…………               //内容太多,这里就省略了
127.0.0.1:6379> help set                             //查看set命令的命令格式及帮助

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

2. redis-benchmark 测试工具

redis-benchmark 是官方自带的Redis性能测试工具,可以有效地测试Redis服务的性能。常用的选项如图:
Non-relational databases (NoSQL) - Redis Detailed installation and deployment

根据以上 选项可以针对某台Redis服务器进行性能检测,命令格式如下:

 [root@localhost ~]# redis-benchmark -h 192.168.1.1 -p 6379 -c 100 -n 10000
                     ………………                 //省略部分内容
====== MSET (10 keys) ======
  10000 requests completed in 0.26 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

16.44% <= 1 milliseconds
95.07% <= 2 milliseconds
96.67% <= 3 milliseconds
97.93% <= 4 milliseconds
99.24% <= 5 milliseconds
99.44% <= 6 milliseconds
99.67% <= 7 milliseconds
99.92% <= 8 milliseconds
100.00% <= 9 milliseconds
38759.69 requests per second
//针对IP地址为192.168.1.1、端口号为6379的Redis服务器发送100个并发连接与10000个请求测试性能
[root@localhost ~]# redis-benchmark -h 192.168.1.1 -p 6379 -q -d 100
//针对IP地址为192.168.1.1、端口号为6379的Redis服务器测试存取大小为100B的数据包的性能
[root@localhost ~]# redis-benchmark -h 192.168.1.1 -p 6379 -t set,lpush -n 10000 -q
SET: 37313.43 requests per second
LPUSH: 45248.87 requests per second
//针对IP地址为192.168.1.1、端口号为6379的Redis服务器在进行set与lpush操作时的性能
//实验环境,性能较差,多多谅解!

四、Redis数据库常用命令

通过博文前面的介绍,已经知道Redis数据库采用key-value(键值对)的数据存储形式。

使用的命令大致是:

  • set: store data, basic format: set key value;
  • get: Obtains data, basic format: get key;

such as:

127.0.0.1:6379> set k1 qq
OK
127.0.0.1:6379> get k1
"qq"

1.key related commands

1)keys

Use the command keys can get the list of keys conform to the rules can usually be combined with "*", "?", Etc. option. E.g:

127.0.0.1:6379> keys *               //查看当前数据库的所有键
1) "mylist"
2) "k2"
3) "k11"
4) "counter:__rand_int__"
5) "key:__rand_int__"
6) "k1"
7) "k3"
//不光是自己设置的键,还有系统默认的键
127.0.0.1:6379> keys k*            //查看当前数据库中以“k”开头的键
1) "k2"
2) "k11"
3) "key:__rand_int__"
4) "k1"
5) "k3"
127.0.0.1:6379> keys k?            //查看当前数据库中以“k”开头后面包含任意一位的键
1) "k2"
2) "k1"
3) "k3"
127.0.0.1:6379> keys k??         //查看当前数据库中以“k”开头后面包含任意两位的键
1) "k11"

2)exists

Use command exists is determined whether there is a key, for example:

127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> exists k0
(integer) 0
//返回值1表示存在;0表示不存在

3) the

Del command to delete the currently designated key database, for example:

127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> get k1
(nil)
//删除之后,k1键对应的数据也就消失了

4)type

Type command value can be acquired using a value corresponding to the type of key, for example:

127.0.0.1:6379> type k2
string
//字符串类型

5)rename

rename command to rename existing key in the format: "rename the source key target key." When using the rename command to rename operation, regardless of whether there are key objectives rename, and value of the source key will cover the value of the target key. In actual use, the recommended target key exists command to see if there is, then decide whether to perform the rename command, to avoid overwriting important data. E.g:

127.0.0.1:6379> get k2
"1qq"
127.0.0.1:6379> get k3
"1q"
127.0.0.1:6379> rename k2 k3
OK
127.0.0.1:6379> get k3
"1qq"

6)renamenx

renamenx command syntax, the same effect with the rename command, except that use renamex rename command is renamed if the target key is present is performed. E.g:

127.0.0.1:6379> keys k??
1) "k11"
127.0.0.1:6379> renamenx k3 k11
(integer) 0
//返回值是0,表示失败

7)dbsize

Dbsize command role is to look at the number of the current database key, for example:

127.0.0.1:6379> dbsize 
(integer) 5

2. Multi-database commonly used commands

Between 1) switching multiple databases

Redis supports multiple databases, there are 16 default database, the database name is used to represent the numbers 0-15. Switching operation of the database, as follows:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> 
//使用select命令进行切换,端口号后面什么都没有,则表示0号数据库

2) to move data between multiple databases

Redis database of more than a certain extent, are relatively independent. E.g:

127.0.0.1:6379> set q1 1
OK
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get q1
(nil)

Redis database provides a "move" command, data can be moved multiple databases. E.g:

127.0.0.1:6379> move q1 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get q1
"1"
//将0号数据库中“q1”的数据移动到1号数据库

3) Clear the data in the database

Redis database of the entire library data deletion is divided into two parts:

  • Clear the current database data, use the "flushdb" command can be achieved;
  • Clear all of the database data, use the "flushall" command can be achieved.

However, the data erase more dangerous, in a production environment is generally not recommended.

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2439872