前言

说到NoSQL果然就要提到Redis了,本文中介绍了如何在Linux上安装Redis的过程。

下载

https://redis.io/download/ 中找到download x.x.x 的链接,右键复制下载地址,例如:

1
https://github.com/redis/redis/archive/7.2.1.tar.gz

在Linux的命令行使用

1
wget https://github.com/redis/redis/archive/7.2.1.tar.gz

注意在那之前进行权限提权

1
sudo -i

然后redis的tar.gz 压缩包就在 /root 的目录下,进行解压操作

1
tar -zxf 7.2.1.tar.gz

其中 -zxf 分别是:

  1. -z:使用 gzip 压缩/解压缩。这个选项告诉 tar 命令使用 gzip 压缩算法来处理文件。
  2. -x:提取文件。这个选项告诉 tar 命令要从归档文件中提取文件。
  3. -f:指定归档文件。这个选项后面需要接归档文件的路径或名称。

解压之后能获得一个 /redis.x.x.x 的文件夹

安装

将解压后的文件夹移动到/usr/local下,并命名为新的文件夹redis(当然你不在这么做也行)

1
mv redis-7.2.1 /usr/local/redis

执行以下程序进行编译

1
2
cd /usr/local/redis
make PREFIX=/usr/local/redis install

其中PREFIX 为指定编译安装后的目录,如果不指定 Linux会将可执行文件存放在/usr/local/bin目录,库文件会存放在/usr/local/lib目录

运行

完成安装之后 在/usr/local/redis目录下会多出 bin可执行文件目录

在运行之前 先了解 redis.conf ,如同字面意思,它是配置文件。根据文件中前几行的描述,可以这样使用

1
2
3
4
5
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf
./bin/redis-server ./redis.conf #在/usr/local/redis 目录下

文件中有大部分注释的英文解释,以下只讲几个配置时用得上的

1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interface. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv6
# bind * -::* # like the default, all available interfaces
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1

默认下 Redis只允许本机链接;如果需要局域网或者外网的另一台主机链接的话需要bind链接主机的ip地址,
如果希望任何的主机都能链接的话,修改或加入
1
bind * -::*

2.

1
2
3
4
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured.
protected-mode yes

还需要关闭protected-mode,才能从另一台主机链接
1
protected-mode no

3.

1
2
3
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

更改port 后的内容以更改端口

4.

1
2
3
4
5
6
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

以守护线程运行Redis,当Redis以守护进程方式运行时,它会在/var/run/redis.pid写入一个pid文件
1
daemonize no

5.
1
2
3
4
5
6
7
8
9
10
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
# requirepass foobared

通过 requirepass xxxxxx 可以设置默认用户认证密码,至少6位字符,例如
1
requirepass 1145141919810

最后

1
./bin/redis-server ./redis.conf 

后话

如果你需要完成Redis的开机启动的话,参考以下优秀文章:
https://blog.csdn.net/m0_60721514/article/details/123777643