天天看点

Linux 7 平台 Redis 7 安装并配置开机自启动 操作步骤演示

Linux 7 平台 Redis 7 安装并配置开机自启动 操作步骤演示

文章目录

  • ​​Linux 7 平台 Redis 7 安装并配置开机自启动 操作步骤演示​​
  • ​​1. 安装Redis​​
  • ​​从官网下载Redis 安装文件:​​
  • ​​安装依赖包:​​
  • ​​解压缩redis:​​
  • ​​安装Redis:​​
  • ​​启动Redis:​​
  • ​​2. 修改配置文件​​
  • ​​3. 配置开机自启动​​
  • ​​3.1 创建配置文件​​
  • ​​3.2 设置开机自启动​​
  • ​​4. 连接Redis​​
  • ​​结语​​
Linux 7 平台 Redis 7 安装并配置开机自启动 操作步骤演示

1. 安装Redis

从官网下载Redis 安装文件:
​​https://github.com/redis/redis/archive/7.0.2.tar.gz​​​​https://codeload.github.com/redis/redis/tar.gz/refs/tags/7.0.2​​​​https://redis.io/download/​​
安装依赖包:
[dave@OAK社区 ~]# yum install gcc tcl –y      
解压缩redis:
[dave@OAK社区 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[dave@OAK社区 ~]# tar xzvf redis-7.0.2.tar.gz
[dave@OAK社区 ~]# cd redis-7.0.2/
[dave@OAK社区 redis-7.0.2]# ls
00-RELEASENOTES  CONDUCT       COPYING  INSTALL   MANIFESTO  redis.conf  runtest-cluster    runtest-sentinel  sentinel.conf  tests   utils
BUGS             CONTRIBUTING  deps     Makefile  README.md  runtest     runtest-moduleapi  SECURITY.md       src            TLS.md
[dave@OAK社区 redis-7.0.2]#      
安装Redis:
[dave@OAK社区 redis-7.0.2]# make
[dave@OAK社区 redis-7.0.2]# make PREFIX=/usr/local/redis install
cd src && make install
which: no python3 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mongodb/bin:/root/bin)
make[1]: Entering directory `/root/redis-7.0.2/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: Leaving directory `/root/redis-7.0.2/src'
[dave@OAK社区 redis-7.0.2]#

这里建议加PREFIX 选项,否则bin 文件会在当前目录的src 中生成。 

[dave@OAK社区 bin]# pwd
/usr/local/redis/bin
[dave@OAK社区 bin]# ll
total 21480
-rwxr-xr-x 1 root root  5197728 Jun 12 11:36 redis-benchmark
lrwxrwxrwx 1 root root       12 Jun 12 11:36 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root       12 Jun 12 11:36 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root  5411416 Jun 12 11:36 redis-cli
lrwxrwxrwx 1 root root       12 Jun 12 11:36 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 11381776 Jun 12 11:36 redis-server
[dave@OAK社区 bin]#      

这里生成了3个程序:

redis-server: 服务端,启动Redis 实例。

redis-cli: 客户端,连接Redis。

redis-benchmark:Redis 性能测试工具。

启动Redis:
[dave@OAK社区 bin]# redis-server
7607:C 12 Jun 2022 11:40:42.545 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7607:C 12 Jun 2022 11:40:42.545 # Redis version=7.0.2, bits=64, commit=00000000, modified=0, pid=7607, just started
7607:C 12 Jun 2022 11:40:42.545 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
7607:M 12 Jun 2022 11:40:42.546 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 7.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```//    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7607
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

7607:M 12 Jun 2022 11:40:42.547 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7607:M 12 Jun 2022 11:40:42.547 # Server initialized
7607:M 12 Jun 2022 11:40:42.547 * Ready to accept connections      

这里已经启动完成,但是是以前台方式启动。

2. 修改配置文件

默认配置文件在安装目录下,我们复制到/etc 路径下:

[dave@OAK社区 redis-7.0.2]# pwd
/root/redis-7.0.2
[dave@OAK社区 redis-7.0.2]# ll redis.conf
-rw-rw-r-- 1 root root 106547 Jun  8 17:56 redis.conf
[dave@OAK社区 redis-7.0.2]# cp redis.conf /etc/      

我们这里修改3个配置:

  1. 配置 Redis 为后台服务: 将配置文件中的 daemonize no 改成 daemonize yes,配置 redis 为后台启动。
  2. Redis 设置访问密码: 在配置文件中找到 requirepass,去掉前面的注释,并修改后面的密码。
  3. 修改绑定IP改为 0.0.0.0 :方便远程连接,不受ip限制

这里仅列出修改后的值:

[dave@OAK社区 redis-7.0.2]# cat /etc/redis.conf |grep -E "bind|requirepass|daemonize" |grep -v "^#"
bind 0.0.0.0 -::1
daemonize yes
requirepass redis
[dave@OAK社区 redis-7.0.2]#      

3. 配置开机自启动

3.1 创建配置文件

创建服务文件:/etc/systemd/system/redis.service

在文件中添加如下内容:

[dave@OAK社区 bin]# vim /etc/systemd/system/redis.service
[dave@OAK社区 bin]# cat /etc/systemd/system/redis.service
[Unit]
# 自定义的服务描述
Description=redis-server
# 依赖,在network服务之后启动
After=network.target

[Service]
Type=forking
# redis启动的命令
ExecStart=/usr/local/redis/bin/redis-server /etc/redis.conf
# redis重启的命令
ExecReload=/usr/local/redis/bin/redis-server -s reload
# redis停止的命令
ExecStop=/usr/local/redis/bin/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[dave@OAK社区 bin]#      
3.2 设置开机自启动
# 重新加载所有的service服务
[dave@OAK社区 bin]# systemctl daemon-reload

# 设置redis.service开机自动
[dave@OAK社区 bin]# systemctl enable redis.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.

# 启动redis 服务
[dave@OAK社区 bin]# systemctl start redis.service
[dave@OAK社区 bin]#
[dave@OAK社区 bin]# ps -ef|grep redis
root     17476     1  0 12:01 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root     17499  8572  0 12:01 pts/1    00:00:00 grep --color=auto redis
[dave@OAK社区 bin]#      

其他操作:

[dave@OAK社区 ~]# systemctl restart redis
[dave@OAK社区 ~]# systemctl stop redis
[dave@OAK社区 ~]# ps -ef|grep redis
root     20423 18246  0 12:06 pts/1    00:00:00 grep --color=auto redis
[dave@OAK社区 ~]# systemctl start redis
[dave@OAK社区 ~]# ps -ef|grep redis
root     20476     1  0 12:06 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root     20544 18246  0 12:06 pts/1    00:00:00 grep --color=auto redis
[dave@OAK社区 ~]#      

4. 连接Redis

[dave@OAK社区 ~]# redis-cli

第一次对key 指定值时,提示我们需要认证。
127.0.0.1:6379> set OAK社区 dave
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> set OAK社区 dave
OK
127.0.0.1:6379> get OAK社区
"dave"
127.0.0.1:6379>      

结语