Table of contents
Getting to know Redis
- Know NOSQL
- Meet Redis
- Install Redis
Redis common commands
- 5 Common Data Structures
- common command
- Operation commands for different data structures
Java client for Redis
- Jedis client
- Spring Data Redis client
Know NOSQL
Know NoSQL
SQL | NoSQL | |
---|---|---|
data structure | Structured | unstructured |
data association | Relational | unrelated |
inquiry mode | SQL query | non-SQL |
transaction characteristics | ACID | BASE |
storage method | disk | Memory |
Scalability | vertical | level |
scenes to be used | 1) The data structure is fixed 2) Related businesses have high requirements for data security and consistency | 1) The data structure is not fixed 2) The requirements for consistency and security are not high 3) The performance requirements |
NoSQL unstructured includes:
- Key-value type (Redis)
- Document Type (MongoDB)
- Column type (HBase)
- Graph type (Neo4j)
Meet Redis
Redis was born in 2009 and its full name is Remote Dictionary Server. The remote dictionary server is a memory-based key-value NoSQL database.
feature:
- Key-value (key-value) type, value supports a variety of different data structures, rich in functions
- Single thread, each command is atomic
- Low latency, fast (memory based, IO multiplexing, good encoding).
- Support data persistence
- Support master-slave cluster and shard cluster
- Support multilingual clients
Getting to know Redis-Three ways to install Redis and start it
Most enterprises deploy projects based on Linux servers, and Redis does not officially provide a Windows version of the installation package. So the following uses the Linux system to install Redis.
Redis official website address: http://redis.io/
Download the redis installation package from the official website
1. Install Redis on a single machine
1.1 Install Redis dependencies
Redis is written based on C language, so you first need to install the gcc dependencies required by Redis:
yum indtall -y gcc tcl
1.2 Upload the installation package and decompress it
Upload the Redis installation package to any directory of the virtual machine, for example, into the /usr/local/src directory:
unzip:
tar -zxvf redis-6.2.6.tar.gz
After decompression:
enter the redis directory:
cd redis-6.2.6
Run the compile command:
make && make install
If there are no errors, the installation was successful.
The default installation path is under the /usr/local/bin directory
This directory has been configured as an environment variable by default, so these commands can be run in any directory. in:
- redis-cli: is the command line client provided by redis
- redis-server: is the server-side startup script of redis
- redis-sentinel: is the redis sentinel startup script
1.3 start
There are many ways to start redis, for example:
- start by default
- Specify the configuration to start
- boot up
1.3.1 Start by default
After the installation is complete, enter the redis-server command in any directory to start Redis:
redis-server
This kind of startup belongs to the foreground startup, which will block the entire session window, and Redis will stop when the window is closed or CTRL+C is pressed. Not recommended.
1.3.2 Specify the configuration to start
If you want Redis to start in the background, you must modify the Redis configuration file, under the decompressed redis installation package (/usr/local/src/redis-6.2.6), the name is redis.conf:
Make a backup of this configuration file first:
cp redis.conf redis.conf.bck
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后即可后台运行
daemoonize 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.3 Startup automatically
We can 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
PrivateTem=true
[Install]
WantedBy=multi-usr.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:
systemctl enable redis
Prohibit booting
systemctl disable redis
2. Redis client
After installing Redis, we can operate Redis to realize CRUD of data. Need to use Redis client, including:
- command line client
- Graphical desktop client
- programming client
2.1 Redis command line client
After the Redis installation is complete, it comes with a command line client: redis-cli, which can be used as follows:
redis-cli [options] [commonds]
Among the common options are:
- -h 127.0.0.1: Specify the IP address of the redis node to be connected, the default is 127.0.01
- -p 6379: Specify the port of the redis node to be connected, the default is 6379
- -a 123321: Specify the access password of redis
Among them, connonds is the operation command of Redis, for example:
- ping: Sit with the redis server for a heartbeat test, the server will return pong normally and
if the commond is not specified, it will enter the interactive console of redis-cli:
2.2 Graphical desktop client
The graphical desktop client of Redis on Github, address: http://github.com/uglide/RedisDesktopManager
However, the warehouse provides the source code of RedisDesktopManager, and does not provide the windows installation package.
The installation package can be found in the following warehouse: http://github.com/lework/RedisDesktopManager-Windows/release
2.2.1 Installation
Redis graphical desktop client:
after decompression, run the installer to install:
after the installation is complete, find the rdm.exe file in the installation directory:
double-click to run