redis安装及使用
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
酌情安装依赖:
yum install -y epel-release
yum install -y gcc
启动:
src/redis-server
设置启动服务脚本
cat > /usr/lib/systemd/system/redis.service <<-EOF
[Unit]
Description=Redis 6379
After=syslog.target network.target
[Service]
Type=forking
PrivateTmp=yes
Restart=always
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 -a jcon shutdown
User=root
Group=root
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000
[Install]
WantedBy=multi-user.target
EOF
# 使服务自动运行
systemctl daemon-reload
systemctl enable redis
# 启动服务
systemctl restart redis
systemctl status redis
#选择数据库,默认0,一般0-15共16个库
select 0
#key大小
dbsize
#查看所有key,*为通配符,也可以为n*即n打头的所有key
keys *
#当前redis的状态,会列出所有使用的库的状态
info
#设置键名key1=zhangsan
set key1 zhangsan
#获取键值
get 'key1'
#删除键值
del 'key1'
#把键key1移动到1库
move key1 1
#是否存在key1
exists key1
#设置key1的有效期为20秒
expire key1 20
#查看多久后key1失效
ttl key1
#设置为永不过期
persist key1
#清空当前库
flushdb
#删除所有库
flushall
6.0的安装命令
wget http://download.redis.io/releases/redis-7.0.0.tar.gz
或curl -O http://download.redis.io/releases/redis-7.0.gz
tar xzf redis-7.0.0.tar.gz
cd redis-7.0.0
make
make install
nano /etc/systemd/system/redis.service
写入以下内容:
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
PIDFile=/var/run/redis_6379.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
groupadd redis
useradd -r -s /sbin/nologin -g redis redis
sudo mkdir /etc/redis
sudo cp /usr/local/bin/redis.conf /etc/redis/6379.conf # 根据实际端口号修改配置文件名和路径
sudo nano /etc/redis/6379.conf # 编辑配置文件以符合你的需求,例如设置正确的日志文件路径等。
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service
systemctl status redis.service
********************************************************************************************************
安得广厦千万间,大庇天下寒士俱欢颜!
********************************************************************************************************