初始
# 安装yum扩展源
[root@localhost ~]# yum install epel* -y
# 安装redis
[root@localhost ~]# yum install redis net-tools -y
# 配置系统参数
[root@localhost ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@localhost ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
[root@localhost ~]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
[root@localhost ~]# echo 2048 > /proc/sys/net/core/somaxconn
[root@localhost ~]# echo "net.core.somaxconn = 2048" >> /etc/sysctl.conf
[root@localhost ~]# echo "* soft nofile 10240" >> /etc/security/limits.conf
[root@localhost ~]# echo "* hard nofile 10240" >> /etc/security/limits.conf
[root@localhost ~]# echo "* soft nproc 10240" >> /etc/security/limits.conf
[root@localhost ~]# echo "* hard nproc 10240" >> /etc/security/limits.conf
[root@localhost ~]# ulimit -n 10240
[root@localhost ~]# ulimit -u 10240
# 修改服务监听地址(默认是: 127.0.0.1 改为 0.0.0.0)
[root@localhost ~]# sed -i 's/^bind\(.*\)/bind 0.0.0.0/g' /etc/redis.conf
# 将redis加入到开机自启动
[root@localhost ~]# systemctl enable redis
# 启动redis
[root@localhost ~]# systemctl start redis
配置文件
默认: /etc/redis.conf
自定义: redis-server /path/to/redis.conf
多数据库
Redis默认支持16个数据库,可以通过配置参数databases来修改这一数据,redis-cli客户端命令工具连接上redis服务器之后默认会自动选择第0个数据库。数据库以数字序列进行编制,如果想切换到第1个数据库只需要select 1即可。
# 连接到redis
[root@localhost ~]# redis-cli
# 查看当前redis最大支持多少个数据库
127.0.0.1:6379> config get databases
1) "databases"
2) "16"
# 默认连接的数据库是0,并且redis没有提供命令来查看当前我们选择的是哪个数据库。
127.0.0.1:6379> set name "zhengtong"
OK
127.0.0.1:6379> set work_at "shanghai"
OK
# 查看当前数据库所有的key。
127.0.0.1:6379> keys *
1) "name"
2) "work_at"
# 切换到数据库1
127.0.0.1:6379> select 1
OK
# 查看数据库1中的所有的key。
127.0.0.1:6379> keys *
(empty list or set)
# 切换回数据库0
127.0.0.1:6379> select 0
OK
# 查看数据库0中的所有的key。
127.0.0.1:6379> keys *
1) "name"
2) "work_at"
数据类型
Strings / 字符串类型命令(24)
|- | - | - | - | -
--- |--- | --- | --- | --- | ---
APPEND | BITCOUNT | BITFIELD | BITOP | BITPOS | DECR
DECRBY | GET | GETBIT | GETRANGE | GETSET | INCR
INCRBY | INCRBYFLOAT | MGET | MSET | MSETNX | PSETEX
SET | SETBIT | SETEX | SETNX | SETRANGE | STRLEN
Lists / 列表类型命令(23)
|- | - | - | - | -
--- |--- | --- | --- | --- | ---
BLPOP | BRPOP | BRPOPLPUSH | LINDEX | LINSERT | LLEN
LPOP | LPUSH | LPUSHX | LRANGE | LREM | LSET
LTRIM | RPOP | RPOPLPUSH | RPUSH | RPUSHX |
Hashes / 散列类型命令(15)
|- | - | - | - | -
--- |--- | --- | --- | --- | ---
HDEL | HEXISTS | HGET | HGETALL | HINCRBY | HINCRBYFLOAT
HKEYS | HLEN | HMGET | HMSET | HSCAN | HSET
HSETNX | HSTRLEN | HVALS |
Sets / 集合类型命令(14)
|- | - | - | -
--- |--- | --- | --- | ---
SADD | SCARD | SDIFF | SDIFFSTORE | SINTER
SINTERSTORE | SISMEMBER | SMEMBERS | SMOVE | SPOP
SRANDMEMBER | SREMSSCAN | SUNION | SUNIONSTORE |
Sorted Sets / 有序集合类型命令(21)
|- | - | -
--- |--- | --- | ---
ZADD | ZCARD | ZCOUNT | ZINCRBY
ZINTERSTORE | ZLEXCOUNT | ZRANGE | ZRANGEBYLEX
ZRANGEBYSCORE | ZRANK | ZREM | ZREMRANGEBYLEX
ZREMRANGEBYRANK | ZREMRANGEBYSCORE | ZSCAN | ZREVRANGEBYLEX
ZREVRANGE | ZREVRANGEBYSCORE | ZSCORE | ZREVRANK
ZUNIONSTORE |
其他
Keys / 关键字命令(24)
|- | - | - | - | -
--- |--- | --- | --- | --- | ---
DEL | DUMP | EXISTS | EXPIRE | EXPIREAT | KEYS
MIGRATE | MOVE | OBJECT | PERSIST | PEXPIRE | PEXPIREAT
PTTL | RANDOMKEY | RENAME | RENAMENX | RESTORE | SCAN
SORT | TOUCH | TTL | TYPE | UNLINK | WAIT
Server / 服务分析工具命令 (31)
|- | - | -
--- |--- | --- | ---
BGREWRITEAOF | CLIENT GETNAME | FLUSHALL | COMMAND
BGSAVE | CLIENT KILL | FLUSHDB | COMMAND COUNT
SAVE | CLIENT LIST | INFO | COMMAND GETKEYS
SHUTDOWN | CLIENT PAUSE | LASTSAVE | COMMAND INFO
SLAVEOF | CLIENT REPLY | MONITOR | CONFIG GET
SLOWLOG | CLIENT SETNAME | DBSIZE | CONFIG RESETSTAT
SYNC | ROLE | DEBUG OBJECT | CONFIG REWRITE
TIME | - | DEBUG SEGFAULT | CONFIG SET
Connection / 连接工具命令 (6)
|- | - | - | - | -
--- |--- | --- | --- | --- | ---
AUTH | ECHO | PING | QUIT | SELECT | SWAPDB
Transactions / 事务处理命令
|- | - | - | -
--- |--- | --- | --- | ---
DISCARD | EXEC | MULTI | UNWATCH | WATCH
参考
- [x] 书籍: Redis 入门指南
- [x] 官网: Strings 命令集