Linux Debian 安装与卸载 Redis 服务

Redis 是一个著名的开源键值对(Key-Value)存储数据库。今天来演示一下如下在 Linux Debian/Ubuntu 服务器上安装与卸载 Redis服务。

安装 Redis

(1)查看服务器系统与版本:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

$ cat /etc/issue
Debian GNU/Linux 11 \n \l

$ cat /etc/debian_version
11.5

可以看到本机使用 Debian 11.5 系统。

(2)要在 Debian 系统安装 Redis ,可以 apt 命令直接安装:

$ apt update 
$ apt install redis-server

(3)查看安装的版本:

$ redis-server -v
Redis server v=6.0.16 sha=00000000:0 malloc=jemalloc-5.2.1 
bits=64 build=6d95e1af3a2c082a

$ redis-cli -v
redis-cli 6.0.16

(4)检查服务状态:

使用 systemctl 命令查看 redis 服务状态:

$ systemctl status redis
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-16 21:51:43 CST; 14min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 158097 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 4379)
     Memory: 7.2M
        CPU: 967ms
     CGroup: /system.slice/redis-server.service
             └─158097 /usr/bin/redis-server 127.0.0.1:6379

更多 systemctl 命令如下:

systemctl start redis-server   # 启动
systemctl stop redis-server    # 停止
systemctl restart redis-server # 重启

systemctl is-enabled redis-server  # 检查是否已经设置开机自动启动
systemctl enable  redis-server     # 设置为开机自动启动
systemctl disable redis-server     # 禁止开机自动启动

查看 Redis 服务器系统进程

$ ps -ef|grep redis
redis     158097       1  0 21:51 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379
root      160611  148921  0 22:05 pts/0    00:00:00 grep --color=auto redis

检查 Redis 端口状态

$ netstat -nlt | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN
tcp6       0      0 ::1:6379                :::*                    LISTEN

Redis 默认监听 6379 端口。

(5) 使用 redis-cli 客户端工具操作redis

$ redis-cli -h 127.0.0.1 -p 6379

127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> set my-home-page wang123.net
OK
127.0.0.1:6379> get my-home-page
"wang123.net"
127.0.0.1:6379> exit

(6) 配置文件

通过 apt 安装的 Redis 的默认配置路径在: /etc/redis/redis.conf

我可以修改 redis.conf 配置文件,找到 port 行,将原默认端口号 6379 改成 6001:

#port 6379
prot 6001

重启并重新连接 redis 服务:

# 重启
$ systemctl restart redis-server

# 默认连接 6379 端口失败
$ redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit

# 指定端口号 6001
$ redis-cli -h 127.0.0.1 -p 6001
127.0.0.1:6001> ping
PONG
127.0.0.1:6001> dbsize
(integer) 1
127.0.0.1:6001> get my-home-page
"wang123.net"
127.0.0.1:6001> exit

卸载 Redis 服务

直接使用 apt 卸载:

$ apt purge --auto-remove redis-server

或者手动删除Redis命令与配置文件等:

$ whereis redis-server
redis-server: /usr/bin/redis-server /usr/share/man/man1/redis-server.1.gz
$ whereis redis-cli
redis-cli: /usr/bin/redis-cli /usr/share/man/man1/redis-cli.1.gz

$ rm /usr/bin/redis-*
$ rm -rf /etc/redis/
$ rm -rf /var/log/redis/
$ rm -rf /var/log/redis/
$ rm -rf /var/lib/redis/
$ rm /etc/init.d/redis-server

参考链接

https://redis.io/

https://www.cnblogs.com/architectforest/p/15737927.html

Thanks for reading.