Redis的使用

本文最后更新于:2022年7月16日 晚上

概览:Redis使用

预警:本文仅作为Redis的简单了解。

Ubuntu中启动、重启、停止

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ubuntu@VM-20-16-ubuntu:/etc/redis$ ps -aux | grep redis
ubuntu 2134 0.0 0.0 14828 1112 pts/0 S+ 19:17 0:00 grep --color=autoredis
redis 32478 0.0 0.1 51852 3684 ? Ssl 19:07 0:00 /usr/bin/redis-server 0.0.0.0:6379

## 停止 redis
ubuntu@VM-20-16-ubuntu:/etc/redis$ sudo systemctl stop redis
ubuntu@VM-20-16-ubuntu:/etc/redis$ ps -aux | grep redis
ubuntu 2215 0.0 0.0 14828 1128 pts/0 S+ 19:17 0:00 grep --color=autoredis
ubuntu@VM-20-16-ubuntu:/etc/redis$

## 启动redis
ubuntu@VM-20-16-ubuntu:/etc/redis$ sudo systemctl start redis
ubuntu@VM-20-16-ubuntu:/etc/redis$ ps -aux | grep redis
redis 2452 0.0 0.1 51852 3608 ? Ssl 19:18 0:00 /usr/bin/redis-server 0.0.0.0:6379
ubuntu 2463 0.0 0.0 14828 1060 pts/0 S+ 19:18 0:00 grep --color=autoredis
ubuntu@VM-20-16-ubuntu:/etc/redis$

## 重启
ubuntu@VM-20-16-ubuntu:/etc/redis$ sudo systemctl restart redis

配置修改 —— 访问ip、密码

修改redis.conf配置文件

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
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ 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 into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 ::1


# 4321就是访问密码
requirepass 4321

权限访问:

1
2
3
4
5
6
7
ubuntu@VM-20-16-ubuntu:/etc/redis$ redis-cli
127.0.0.1:6379> get key
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 4321
OK
127.0.0.1:6379> get key
(nil)

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!