安裝redis
sudo apt-get install -y redis-server
設(shè)置密碼
sudo vi /etc/redis/redis.conf 密碼在 500行左右
服務(wù)端重啟
service redis restart 開始 start 停止 stop
客戶端連接
redis-cli -h IP地址 -p 6379 -a 密碼
允許遠(yuǎn)程連接
1、注釋掉本地IP地址綁定
69行: # bind 127.0.0.1 ::1
2、關(guān)閉保護(hù)模式(把yes改為no)
88行: protected-mode no
3、重啟服務(wù)
sudo /etc/init.d/redis-server restart

image.png

image.png
原文鏈接:https://blog.csdn.net/m0_67403013/article/details/124090499
2 與Django連接配置
首先安裝 django-redis
pip install django-redis redis
然后在settings中配置:
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'CONNECTION_POOL_KWARGS': {'max_connections': 200}
}
}
}
連接方式一
from django_redis import get_redis_connection
cache = get_redis_connection('default')
連接方式二
import redis
cache = redis.Redis(host='localhost', port=6379)
cache
Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0
通用命令
# 切換庫(number的值在0-15之間,db0 ~ db15)
select 0
# 查看鍵
keys 表達(dá)式 # keys *
# 數(shù)據(jù)類型
type key
# 鍵是否存在
exists key
# 刪除鍵
del key
# 鍵重命名
rename key newkey
# 清除當(dāng)前庫中所有數(shù)據(jù)(慎用)
flushdb
# 清除所有庫中所有數(shù)據(jù)(慎用)
flushall
python交互redis
sudo pip3 install redis
import redis
# 創(chuàng)建數(shù)據(jù)庫連接對(duì)象
r = redis.Redis(host='127.0.0.1',port=6379,db=0,password='123456')