Centos7 install Redis database and simple use

One, Redis introduction

Redis database is a non-relational database.

1.1 Relational database and sub-relational database

Databases can be divided into relational databases and other databases according to their structure. And these other databases are collectively called non-relational databases

1.2 Relational database

A relational database is a structured database, created on the basis of a relational model, generally record-oriented. It uses mathematical concepts and methods such as set algebra to process data in the database. The relational model refers to a two-dimensional table model, so a relational database can be a data organization composed of two-dimensional tables and the connections between them. In the real world, various entities and various connections between entities can be represented by relational models. SQL (Structured Query Language, structured query language) statement is based on a relational database language, used to perform the retrieval and operation of data in the relational database.
主流的关系型数据库包括Oracle,MySQL,SQL Server,Microsoft Access,DB2等

1.3 Non-relational database

NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is the general term for non-relational databases. 主流NoSQL数据库由Redis,MongBD,Hbase,CouhDB等。These databases are completely different in their storage methods, storage structures, and usage scenarios. So we think it is a collection of non-relational databases. Rather than being a general term like relational databases. In other words, databases other than mainstream relational databases are all relational.

1.4 Background of non-relational databases

  • High performance-high concurrent read and write requirements for the database
  • Huge Storage-the need for efficient storage and access to massive data
  • High Scalability && Hige Availability——Requirements for database high scalability and high availability

2. Introduction to Redis

Redis is an open source, written in C language, supports the network, and can be based on memory and is also sustainable. The key-value (key-value) database is an indispensable part of the current distributed architecture.
Redis has the following advantages:

  • 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.
  • Supports rich data types, not only supports simple key-value data types, but also supports data type operations such as Strings, Lists, Hashes, Sets and Orered Sets.
  • Atomicity, all Redis operations are atomic.
  • Support data backup, that is, data backup in master-slave mode.

3. Redis installation and deployment

1) Preparation for
a Centos7, Redis source code package can
be downloaded from Redis official https://www.redis.io, or from the link below
https://pan.baidu.com/s/1JlhmlSrK1xWerRE5oSgubA
extraction Code: 3gj6
2) Install Redis database

[root@Redis ~]# mount /dev/cdrom /media/cdrom   #挂光盘
[root@Redis ~]# yum -y install gcc*             #安装依赖包
[root@Redis ~]# rz   #上传软件包
[root@Redis ~]# tar zxf redis-3.2.9.tar.gz -C /usr/src/
[root@Redis ~]# cd /usr/src/redis-3.2.9/
[root@Redis redis-3.2.9]# make && make PREFIX=/usr/local/redis install

Insert picture description here
3) Set up related configuration files for Redis through the install_server.sh script file provided by the software package by default

[root@Redis redis-3.2.9]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@Redis redis-3.2.9]# cd utils/
[root@Redis utils]# ./install_server.sh
依次回车即可或自己定义

Insert picture description here
After the installation is complete, the Redis service can be controlled through the Redis service control script /etc/init.d/redis_6379

[root@Redis utils]# netstat -anpt | grep redis      #查看redis端号
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5058/redis-server 1 
[root@Redis utils]# /etc/init.d/redis_6379 stop     #关闭Redis服务
Stopping ...
Redis stopped
[root@Redis utils]# /etc/init.d/redis_6379 start    #启用Redis服务
Starting Redis server...
[root@Redis utils]# /etc/init.d/redis_6379 status   #查看Redis运行状态
Redis is running (5146)

Insert picture description here

3. Configuration parameters

The main Redis configuration file is /etc/redis/6379.conf
common configuration items:

  • bind: listening address
  • port: port number
  • daemonize yes: enable the daemon
  • pidfile: Specify PID file
  • loglevel notice: log level
  • logfile: Specify the log file

Two, Redis command tool

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

  • redis-server: a tool for enabling Redis
  • redis-benchmark: used to detect Redis operating efficiency on the machine
  • redis-check-aof: repair AOF persistent files
  • redis-check-rdb: repair RDB persistent files
  • redis-cli: Redis command tool
  • redis—setinel: redis—server command tool soft connection

1. redis-cli command line tool

1) Connect to the local redis database and use the ping command to test whether the redis service is started.
Insert picture description here
2) Connect to the redis service on other servers.
Add the local IP address after the bind item in the Redis main configuration file

[root@Redis ~]# vim /etc/redis/6379.conf

Insert picture description here

[root@Redis ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@Redis ~]# redis-cli -h 192.168.1.10 -p 6379
192.168.1.10:6379> exit
[root@Redis ~]#

Insert picture description here

2. redis-benchmark test tool

Common options:

  • -h: Specify the server host name
  • -p: Specify the 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 (B)
  • -k:1=keep alive 0=reconnect
  • -r: SET/GET/INCR uses a random key, SADD uses a random value
  • -P: Transfer the request through the pipe
  • -q: Forcibly exit redis. Only display query/sec value
  • — —Csv: output in CSVA format
  • -l: Generate a loop and execute the test permanently
  • -t: Run only a comma-separated list of test commands
  • -l: ldle mode. Only open N idle connections and wait

1) Performance test: Send 100 concurrent connections and 100000 requests to test performance

[root@Redis ~]# redis-benchmark -h 192.168.110 -p 6379 -c 100 -n 100000

2) Performance test: test the performance of data packets with a size of 100B

[root@Redis ~]# redis-benchmark -h 192.168.1.10 -p 6379 -q -d 100

3) Performance test: test the performance of the native redis service during set and lpush operations

[root@Redis ~]# redis-benchmark -t set,lpush -n 100000 -q
SET: 99206.34 requests per second
LPUSH: 97465.88 requests per second
[root@Redis ~]# 

Three, Redis database commonly used commands

The Redis database adopts a key-value (key-value pair) data storage form. So the commands used are set and get commands

  • set: store data, the basic command format is set key value
  • get: Get data, the basic command format is get key
[root@Redis ~]# redis-cli -h 192.168.1.10 -p 6379
192.168.1.10:6379> set xingming zhangsan
OK
192.168.1.10:6379> get xingming
"zhangsan"

Insert picture description here

1.Key related commands

1)keys
取符合规则的键值列表,通常情况可以结合*(所有),?(单个字符) 等选项来使用

192.168.1.10:6379> set a1 1           
192.168.1.10:6379> set a2 2
192.168.1.10:6379> set v1 3
192.168.1.10:6379> keys *             #显示当前库所有键
192.168.1.10:6379> keys xing????      #显示键以xing开头后面有4个字符的
192.168.1.10:6379> keys a*            #显示以a开头的所有键

Insert picture description here
2)exists
判断键值是否存在

192.168.1.10:6379> exists xingming
(integer) 1
192.168.1.10:6379> exists nianling
(integer) 0

Insert picture description here
3) of
删除当前数据库的指定key

192.168.1.10:6379> get a1
"1"
192.168.1.10:6379> del a1
(integer) 1
192.168.1.10:6379> get a1
(nil)

Insert picture description here
4)type
获取key对应的value值类型,默认情况下redis键值类型都是字符串string类型

192.168.1.10:6379> type xingming
string

5)rename
对已有key进行重命名,无论目标key是否存在都进行重命名,且源key的值会覆盖目标key的值。

192.168.1.10:6379> keys a*
1) "a2"
192.168.1.10:6379> rename a2 a33
OK
192.168.1.10:6379> keys a*
1) "a33"

Insert picture description here
6)renamenx
对已有key进行重命名,并检测新名是否存在。跟rename区别则是当renamenx进行重命名时,如果目标key存在则不进行重命名。

192.168.1.10:6379> set a1 1
OK
192.168.1.10:6379> set a2 2
OK
192.168.1.10:6379> keys a*
1) "a2"
2) "a33"
3) "a1"
192.168.1.10:6379> renamenx a33 a1
(integer) 0
192.168.1.10:6379> keys a*
1) "a2"
2) "a33"
3) "a1"
192.168.1.10:6379> renamenx a33 a4
(integer) 1
192.168.1.10:6379> keys a*
1) "a2"
2) "a4"
3) "a1"

Insert picture description here
7)dbsize
查看当前数据库中key的数目

192.168.1.10:6379> keys *
1) "a2"
2) "xingming"
3) "mylist"
4) "a4"
5) "v1"
6) "key:__rand_int__"
7) "counter:__rand_int__"
8) "a1"
192.168.1.10:6379> dbsize
(integer) 8

Insert picture description here

2. Common commands for multiple databases

1) Switch between multiple databases The
redis database supports multiple databases, which contains 16 databases by default, and the names are represented by numbers 0-15. After redis-cli enters the database, the default database 0 is used.

192.168.1.10:6379> select 11
OK
192.168.1.10:6379[11]> select 16
(error) ERR invalid DB index

Insert picture description here
2) Move data between multiple databases
Most of Redis databases cannot be relatively independent to a certain extent. For example, the data of k1 stored in database 0 cannot be viewed on other databases ranging from 1 to 15.

192.168.1.10:6379[11]> select 0
OK
192.168.1.10:6379> set k1 99
OK
192.168.1.10:6379> get k1
"99"
192.168.1.10:6379> select 1
OK
192.168.1.10:6379[1]> get k1
(nil)

Insert picture description here
Redis database provides a move command, which can move data from multiple databases.

192.168.1.10:6379[1]> select 0
OK
192.168.1.10:6379> get k1
"99"
192.168.1.10:6379> move k1 3
(integer) 1
192.168.1.10:6379> select 3
OK
192.168.1.10:6379[3]> get k1
"99"
192.168.1.10:6379[3]> select 0
OK
192.168.1.10:6379> get k1
(nil)

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/109012221