Detailed steps to install and deploy redis on Linux cloud server

Most enterprises deploy projects based on Linux servers, and Redis does not officially provide Windows version installation packages, so we need to master Linux-based systems to install Redis.

The Linux version selected here is CentOS 7.

The official website address of Redis: Redis

1. Install Redis on a single machine

1.1 Install Redis dependencies

Redis is written based on 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 decompress it

① On the official website address of Redis: Redis  download installation package 

② Use Xftp to upload the downloaded installation package to the /usr/local/src directory in the Linux server:

③ Enter /usr/local/src to decompress:

tar -xzf redis-6.2.6.tar.gz

④ Enter the redis directory after decompression:

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 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 redis server startup script

  • 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  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 the background, you must modify the Redis configuration file, which is under the redis installation package we decompressed earlier ( /usr/local/src/redis-6.2.6), and the name is redis.conf:

① Let's back up this configuration file first (to avoid mistakes):

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 (optional):

# 监听的端口
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
# 查看是否启动成功
ps -ef | grep redis
# 杀死redis进程
kill -9 进程号

⑤ Stop service:

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

1.3.3 Configure autostart

We can also implement boot self-starting 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 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 boot up automatically:

systemctl enable redis

2. Redis client

After installing Redis, we can operate Redis to realize CRUD of data. This requires the use of Redis clients, 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 connect to, the default is 127.0.0.1

  • -p 6379: Specify the port of the redis node to be connected, the default is 6379

  • -a 123321: Specify the access password of redis

The commonds are Redis operation commands, for example:

  • ping: Do a heartbeat test with the redis server, and the server will return normally pong

When no commond is specified,  redis-cli the interactive console will be entered:

2.2 Graphical desktop client

The great god on GitHub has written a graphical desktop client for Redis, address: https://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 repository: Releases · lework/RedisDesktopManager-Windows · GitHub  

Once downloaded and unzipped, run the .exe installer to install. After the installation is complete, find the rdm.exe file in the installation directory, and double-click it to run it.

After the installation is complete, click the Connect to redis server button in the upper left corner, fill in the Redis service information in the pop-up window, and click OK to connect successfully! ! !

Guess you like

Origin blog.csdn.net/weixin_52850476/article/details/124893005