Redis 是一个Key-Value存储的系统;它支持存储的value类型有string(字符串),list(列表),set(集合),zset(有序集合);为了保证效率;数据都缓存在内存中;它也周期性将内存数据写入磁盘或者把读写操做写入记录文件。Redis使用单线程模型;还有其余特性:例如健过时,发布/订阅,事务功能等等。
相比其余个Key-Value存储的系统;有这些优点:redis
Redis下载地址sql
tar -zxvf redis-5.0.0.tar.gz cd redis-5.0.0 make cd src make install PREFIX=/usr/local/redis mkdir -p /usr/local/redis/etc mv ../redis.conf /usr/local/redis/etc
习惯了使用非root用户来管理数据库;前提咱们须要建立redis用户和相关目录数据库
useradd redis mkdir -p /data/redis/{logs,conf,pid,data} chown -R redis:redis /data/redis
配置redis;在/data/redis/conf目录下建立redis-6379.conf文件;内容以下缓存
###基本参数### daemonize yes pidfile /data/redis/pid/redis-6379.pid port 6379 tcp-backlog 65535 bind 0.0.0.0 timeout 0 tcp-keepalive 0 loglevel notice logfile /data/redis/logs/redis-6379.log databases 16 lua-time-limit 5000 maxclients 10000 protected-mode yes dir /data/redis/data/ ###慢日志参数### slowlog-log-slower-than 10000 slowlog-max-len 128 ###内存参数### #maxmemory 1G #maxmemory-policy volatile-lru ###RDB持久化参数### save 3600 1 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump-6379.rdb" ###AOF持久化参数### no-appendfsync-on-rewrite yes appendonly yes appendfilename "appendonly-6379.aof" appendfsync no auto-aof-rewrite-min-size 512mb auto-aof-rewrite-percentage 100 aof-load-truncated yes aof-rewrite-incremental-fsync yes ###客户端Buffer参数### client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 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 ###安全参数### requirepass li0924 # masterauth 123456789 rename-command KEYS REDIS_KEYS rename-command FLUSHDB REDIS_FLUSHDB rename-command FLUSHALL REDIS_FLUSHALL ###复制参数(从库)### ###集群参数###
相关配置参数:安全
配置用户环境变量架构
export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin alias rds_start="redis-server /data/redis/conf/redis-6379.conf" alias rds_stop="redis-cli -a li0924 shutdown &> /dev/null" alias rsql="redis-cli"
redis-server /data/redis/conf/redis-6379.conf # 或者 rds_start
redis-cli shutdown
# 或者
rds_stop
固然还有一种更直接,更粗暴的方式 kill进程 。不建议这样操做;由于这样不会作持久化操做;app
pkill redis-server
redis-cli
# 或者
rsql
redis-benchmark:redis性能测试工具 redis-check-aof:检查aof日志的工具 redis-check-dump:检查rdb日志的工具 redis-cli:链接用的客户端 redis-server:redis服务进程
固然这也许须要根据日志如何提醒;作对应的修改。tcp
# 在/etc/sysctl.conf文件添加 vm.overcommit_memory = 1 net.core.somaxconn = 65536 # 在/etc/rc.local文件添加 echo never > /sys/kernel/mm/transparent_hugepage/enabled
如今能够愉快玩耍redis分布式
# 1. 启动redis数据库 [redis@Postgres ~]$ rds_start # 2. 链接redis数据库 [redis@Postgres ~]$ rsql # 3. 密码安全认证 127.0.0.1:6379> AUTH li0924 OK # 4. 查看全部健;原命令是KEYS *; 127.0.0.1:6379> redis_keys * 1) "name" # 5. 设置K-V 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> redis_keys * 1) "hello" 2) "name" # 6. 获取健hello的值 127.0.0.1:6379> get hello "world" # 7. 退出redis客户端 127.0.0.1:6379> exit # 8. 关闭redis数据库 [redis@Postgres ~]$ rds_stop [redis@Postgres ~]$ rsql Could not connect to Redis at 127.0.0.1:6379: Connection refused