Initial Redis and Redis installation

Table of contents

1. First introduction to Redis

1.1.Understand NoSQL

1.1.1. Structured and unstructured

1.1.2. Associated and non-associated

1.1.3. Query method

1.1.4.Transactions

1.1.5. Summary

1.2. Get to know Redis

1.3. Install Redis

1.3.1. Dependent libraries

1.3.2. Upload the installation package and decompress it

1.3.3.Startup

1.3.4.Start by default

1.3.5. Start by specifying configuration

1.3.6.Start automatically after power on


1. First introduction to Redis

Redis is a key-value NoSql database. There are two keywords here:

  • Key-value type

  • NoSql

The key-value type means that the data stored in Redis is stored in the form of key and value pairs, and value can be in various forms, such as strings, numeric values, or even json:

NoSql is a database that is very different from traditional relational databases.

1.1.Understand NoSQL

NoSql can be translated into Not Only Sql (not just SQL), or No Sql (non-Sql) database. It is a special database that is very different from the traditional relational database, so it is also called a non-relational database .

1.1.1. Structured and unstructured

Traditional relational databases are structured data, and each table has strict constraint information: field names, field data types, field constraints, etc. The inserted data must comply with these constraints:

NoSql, on the other hand, does not have strict constraints on the database format, and is often loose and free in form.

Can be key-value type:

It can also be a document type:

It can even be in graphic format:

1.1.2. Associated and non-associated

There are often relationships between tables in traditional databases, such as foreign keys:

Non-relational databases do not have relationships. To maintain relationships, you must rely on business logic in the code or coupling between data:

{
  id: 1,
  name: "张三",
  orders: [
    {
       id: 1,
       item: {
     id: 10, title: "荣耀6", price: 4999
       }
    },
    {
       id: 2,
       item: {
     id: 20, title: "小米11", price: 3999
       }
    }
  ]
}

Here, we need to maintain the relationship between "Zhang San's" order and the products "Glory" and "Xiaomi 11". We have to redundantly save these two products in Zhang San's order document, which is not elegant enough. It is still recommended to use business to maintain relationships.

1.1.3. Query method

Traditional relational databases will make queries based on Sql statements, and the syntax has a unified standard;

The query syntax of different non-relational databases is very different and diverse.

1.1.4.Transactions

Traditional relational databases can meet the transaction ACID principles.

Non-relational databases often do not support transactions, or cannot strictly guarantee ACID characteristics and can only achieve basic consistency.

1.1.5. Summary

In addition to the above four points, there are also significant differences between relational and non-relational types in terms of storage methods, scalability, and query performance. They are summarized as follows:

  • Storage method

    • Relational databases are stored on disks, and there will be a lot of disk IO, which will have a certain impact on performance.

    • For non-relational databases, their operations rely more on memory. The read and write speed of memory will be very fast, and the performance will naturally be better.

  • Scalability

    • The relational database cluster mode is generally master-slave, and the master-slave data is consistent and plays the role of data backup, which is called vertical expansion.

    • Non-relational databases can split data and store it on different machines. They can save massive amounts of data and solve the problem of limited memory size. It's called horizontal expansion.

    • Because there are relationships between tables in relational databases, horizontal expansion will bring a lot of trouble to data queries.

1.2. Get to know Redis

Redis was born in 2009. Its full name is Remote Dictionary Server. It is a memory-based key-value NoSQL database .

Features :

  • Key-value type, value supports a variety of different data structures and has rich functions

  • Single thread, each command is atomic

  • Low latency, fast (memory based, IO multiplexing, good encoding).

  • Support data persistence

  • Supports master-slave clusters and sharded clusters

  • Support multi-language clients

Author : Antirez

Redis official website address: Redis

1.3. Install Redis

Most companies deploy projects based on Linux servers, and Redis officially does not provide a Windows version installation package. Therefore, in the course we will install Redis based on Linux system.

The Linux version selected here is CentOS 7.

1.3.1. Dependent libraries

Redis is written based on C language, so you first need to install the gcc dependencies required by Redis:

yum install -y gcc tcl

1.3.2. Upload the installation package and decompress it

Then upload the Redis installation package to any directory of the virtual machine:

unzip:

tar -xzf redis-6.2.6.tar.gz

Enter the redis directory:

进入redis目录:

Run the compile command:

make && make install

If there are no errors, the installation should be successful.

The default installation path is in /usr/local/binthe directory:

This directory is configured as an environment variable by default, so these commands can be run from any directory. in:

  • redis-cli: is the command line client provided by redis

  • redis-server: is the server startup script of redis

  • redis-sentinel: is the sentinel startup script of redis

1.3.3.Startup

There are many ways to start redis, such as:

  • Start by default

  • Start with specified configuration

  • Start automatically at boot

1.3.4.Start by default

After the installation is complete, enter the redis-server command in any directory to start Redis:

redis-server

 As shown in the picture:

This kind of startup 前台启动will block the entire session window. CTRL + CRedis will stop when the window is closed or pressed. Not recommended.

1.3.5. Start by specifying configuration

If you want Redis to 后台start in the method, you must modify the Redis configuration file, which is under the redis installation package we decompressed before ( /usr/local/src/redis-6.2.6), and the name is redis.conf:

Then modify some configurations in the redis.conf file:  

# 允许访问的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0
bind 0.0.0.0
# 守护进程,修改为yes后即可后台运行
daemonize yes 
# 密码,设置后访问Redis必须输入密码
requirepass 123321

 Other common configurations of Redis:

# 监听的端口
port 6379
# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
dir .
# 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15
databases 1
# 设置redis能够使用的最大内存
maxmemory 512mb
# 日志文件,默认为空,不记录日志,可以指定日志文件名
logfile "redis.log"

Start Redis:

# 进入redis安装目录 
cd /usr/local/src/redis-6.2.6
# 启动
redis-server redis.conf

Out of service:  

# 利用redis-cli来执行 shutdown 命令,即可停止 Redis 服务,
# 因为之前配置了密码,因此需要通过 -u 来指定密码
redis-cli -u 123321 shutdown

1.3.6.Start automatically after power on

We can also configure it to start automatically at boot.

First, create a new system service file:

vi /etc/systemd/system/redis.service

The content is as follows:

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Then reload the system service:

systemctl daemon-reload

Now, we can use the following set of commands to operate redis:

# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis

Execute the following command to make redis start automatically at boot:

systemctl enable redis

Guess you like

Origin blog.csdn.net/m0_62609939/article/details/130569579