centos7-安装redis

高效的内存数据库
安装单服务版
版本5.0.6


准备工作及安装

  • 准备环境
systemctl stop iptable firewalld
systemctl disable iptable firewalld

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

yum -y install gcc tcl
wget http://download.redis.io/releases/redis-5.0.6.tar.gz -P /tmp
useradd -M -s /sbin/nologin redis
mkdir -p /data/redis/logs /data/redis/data/6379 /opt/redis/conf /var/run/redis 
  • 解压程序并安装
cd /tmp
tar zxvf redis-5.0.6.tar.gz
cd redis-5.0.6/
make MALLOC=libc
make PREFIX=/opt/redis/ install
cp ./src/redis-trib.rb /opt/redis/bin/
  • 创建配置文件
cat << EOF > /opt/redis/conf/redis-6379.conf
daemonize yes
pidfile /var/run/redis/redis-6379.pid
port 6379
tcp-backlog 65535
bind 127.0.0.1
timeout 600
tcp-keepalive 60
loglevel notice
logfile /data/redis/logs/redis-6379.log
databases 16
lua-time-limit 5000
maxclients 3000
dir /data/redis/data/6379/
requirepass password
masterauth password

###慢日志参数###
slowlog-log-slower-than 10000
slowlog-max-len 128

###内存参数###
maxmemory 700M
maxmemory-policy volatile-lru

###RDB持久化参数###
save 900 1
save 300 10
save 60 1000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb

###AOF持久化参数###
no-appendfsync-on-rewrite yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 256mb
aof-load-truncated yes
aof-rewrite-incremental-fsync yes

###客户端Buffer参数###
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 128mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

###其他参数###
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
latency-monitor-threshold 0
hz 10
EOF

当前仅侦听本地'127.0.0.1'IP地址,如需在更多地址上侦听,请按照示例增加IP地址: 'bind 127.0.0.1 10.10.10.10'。
内存配置根据机器以及项目需求进行调整。

  • 修改相关目录宿主
chown -R redis:redis /data/redis /opt/redis /var/run/redis
  • 使用redis用户启动redis
sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis-6379.conf
  • 登录redis并使用检测redis状态
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a password

127.0.0.1:6379> info

# Server
redis_version:5.0.6
......
# Keyspace
  • 关闭redis
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a password shutdown

使用系统systemctl管理redis启动关闭以及异常关闭重启

  • 创建启动文件
cat << EOF > /usr/lib/systemd/system/redis-6379.service
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
User=redis
ExecStart=/opt/redis/bin/redis-server /opt/redis/conf/redis-6379.conf
Restart=on-failure
RestartSec=10s
KillMode=control-group
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
  • 启动并设置开机启动
systemctl start redis-6379.service
systemctl enable redis-6379.service

redis简单使用

  • 写入一个数据
set key name
  • 根据key查询数据
get key
  • 根据key删除数据
del key
  • 查询所有的key值
keys *
  • 清空所有数据
flushall
  • 切换数据库
select 2
  • 查看key值的有效期
ttl key
#or
pttl key

ttl计量单位为秒, pttl计量单位为毫秒

猜你喜欢

转载自www.cnblogs.com/taoyuxuan/p/11887697.html