Most enterprises deploy projects based on Linux servers, and Redis does not officially provide an installation package for the Windows version. Therefore, in the course we will install Redis based on the Linux system.
The Linux version chosen here is CentOS 7.
Redis official website address: https://redis.io/
1. Install Redis on a single machine
1.1. Install Redis dependencies
Redis is written based on the C language, so first you need to install the gcc dependencies required by Redis:
yum install -y gcc tcl
1.2. Upload the installation package and extract it
Then upload the Redis installation package provided by the pre-class materials to any directory of the virtual machine:
For example, I put in the /usr/local/src directory:
unzip:
tar -xzf redis-6.2.6.tar.gz
After decompression:
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-jNagmQek-1646482409632) (I:\Data\redis\01-Practical articles\Data\assets\image-20211211080339076 .png)]
Enter the redis directory:
cd redis-6.2.6
Run the compile command:
make && make install
If there are no errors, the installation should be successful.
The default installation path is in the /usr/local/bin
directory :
This directory is configured to environment variables by default, so you can run these commands 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. Start
There are many ways to start redis, for example:
- Start by default
- Start with the specified configuration
- Auto-start
1.3.1. Default start
After the installation is complete, enter the redis-server command in any directory to start Redis:
redis-server
As shown in the figure:
This kind of startup 前台启动
will block the entire session window, and CTRL + C
Redis will stop when the window is closed or pressed. Not recommended.
1.3.2. Specify the configuration to start
If you want Redis to 后台
start in this way, you must modify the Redis configuration file, which is under the redis installation package we unzipped before ( /usr/local/src/redis-6.2.6
), named redis.conf:
Let's 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后即可后台运行
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.3. Self-start at boot
We can also achieve boot auto-start through configuration.
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 system services:
systemctl daemon-reload
Now, we can operate redis with the following set of commands:
# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis
Execute the following command to make redis start automatically:
systemctl enable redis
2. Redis client
After installing Redis, we can operate Redis and implement CRUD of data. This requires a Redis client, including:
- command line client
- Graphical desktop client
- programming client
2.1. Redis Command Line Client
After Redis is installed, it comes with a command line client: redis-cli, which is 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 connect to, the default is 127.0.0.1-p 6379
: Specify the port of the redis node to connect to, the default is 6379-a 123321
: Specify the access password of redis
The commonds are the operation commands of Redis, for example:
ping
: Do a heartbeat test with the redis server, the server will return normallypong
When commond is not specified, redis-cli
the interactive console that will be entered:
2.2. Graphical desktop client
The great god on GitHub wrote the graphical desktop client of Redis, address: https://github.com/uglide/RedisDesktopManager
However, the repository provides the source code of RedisDesktopManager and does not provide the windows installation package.
The installation package can be found in the following repository: https://github.com/lework/RedisDesktopManager-Windows/releases
2.2.1. Installation
Redis' graphical desktop client can be found in the pre-class materials:
Once unpacked, run the installer to install:
omitted here.
After the installation is complete, find the rdm.exe file in the installation directory:
Double click to run:
2.2.2. Establish a connection
Click the 连接到Redis服务器
button in the upper left corner:
Fill in the Redis service information in the pop-up window:
After clicking OK, this link will appear on the left menu:
Click to establish a connection:
Redis has 16 warehouses by default, numbered from 0 to 15. The number of warehouses can be set through the configuration file, but it does not exceed 16, and the warehouse name cannot be customized.
If you are connecting to the Redis service based on redis-cli, you can use the select command to select the database:
# 选择 0号库
select 0