redis配置和使用:

redis配置和使用:

CentOS7 :

因为本人使用的是华为云服务器,安装教程在官网,所以安装部分省略......

启动redis:

redis-server

配置redis文件:

# 首先先查找redis.conf路径
find / -name redis.conf
/etc/redis.conf

vim /etc/redis.conf
> 

命令模式下按 / 关键字  # 快速查找关键字位置

# 配置远程连接
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1
bind 0.0.0.0

# 保护模式打开
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

# 后台运行 
# 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.
daemonize yes


# 设置密码
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass 密码

# 保存退出
esc
Shift + ; 
x
Enter

再次启动redis:

redis-server /etc/redis.conf
redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.

127.0.0.1:6379> auth 密码
OK
127.0.0.1:6379> ping
PONG
任务完成:ojbk --- 允许了远程连接又设置了密码。
接下来去云服务器开启安全组,开放端口。
安全组放通后可以去项目中使用了。

项目数据缓存到redis:

前提是要安装好redis, 配置好redis, 启动好redis.

安装库:
pip install django-redis
pip install django-redis-cache
项目中缓存配置:

setting >

CACHES = {
	'default': {
		'BACKEND': "django_redis.cache.RedisCache", # 找库
                # 数据库+驱动 (数据库和驱动同名时可省略)://用户名:密码@主机:端口/库
                # "redis://password@公网IP:端口/1" 可连接到云服务器
		'LOCATION': "redis://127.0.0.1:6379/1",  # 本机链接
		'OPTIONS': {
                        "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
	}
}

去redis中查询数据:

# 登录
redis-cli
auth 密码
ping

#进入到第一个库
select 1
# 获取所有数据 
keys *
"1"
"2"
"3"
# 查询数据
get "1"
posted @ 2020-10-22 17:49  Morpheus1024  阅读(89)  评论(0编辑  收藏  举报