Linux运维 --- Redis的安装和配置
Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库存储系统。
安装Redis
[][][][][]
编译安装
[][][][]0[][][][][][][]Redis server v=4.0.11 sha=00000000:0 malloc=libc bits=64 build=e35fe61bb778256c[]redis-cli 4.0.11[]
优化Redis配置文件
[root@redis redis]# mkdir /data/redis -p[root@redis redis]# cd /usr/local/redis/conf[root@redis conf]# lsredis.conf sentinel.conf[root@redis conf]# cp redis.conf{,.bak}[root@redis conf]# egrep -v "^$|^#" redis.conf.bak > redis.conf[root@redis conf]# vim redis.conf1 bind 0.0.0.02 protected-mode yes3 port 63794 tcp-backlog 10245 timeout 06 tcp-keepalive 3007 daemonize yes8 supervised no9 pidfile /data/redis/redis.pid10 loglevel notice11 logfile "/data/redis/redis.log"12 databases 1613 always-show-logo yes14 save 900 115 save 300 1016 save 60 1000017 stop-writes-on-bgsave-error yes18 rdbcompression yes19 rdbchecksum yes20 dbfilename dump.rdb21 dir /data/redis/22 slave-serve-stale-data yes23 slave-read-only yes24 repl-diskless-sync no25 repl-diskless-sync-delay 5
配置环境变量
[root@redis conf]# vim /etc/profileexport PATH="$PATH:/"/usr/src/redis/bin"
启动redis
[][]tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 21282/redis-server[][]COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEredis-ser 21282 root 6u IPv4 99104 0t0 TCP *:6379 (LISTEN)[]
redis实例
[root@redis conf]# redis-cli# 使用SET命令将值“chd” 存储在键"server:name"中127.0.0.1:6379> SET server:name "chd"OK# 询问秘钥服务器:名称中存储的值是多少? Redis会回复"chd"127.0.0.1:6379> GET server:name"chd"# 测试给定密钥是否存在127.0.0.1:6379> EXISTS server:name => 1(integer) 1127.0.0.1:6379> EXISTS server:chuid => 0(integer) 0127.0.0.1:6379># 箭头(=>)之后的文本显示了预期的输出
参数优化调整
[][]net.core.somaxconn = 10240[][]net.core.somaxconn = 10240vm.overcommit_memory = 1[][]
redis.conf配置文件参数说明
[root@redis conf]# vim redis.confbind 0.0.0.0 # 绑定的主机IPprotected-mode yesport 6379 # 指定 Redis 监听端口tcp-backlog 1024timeout 0 # 客户端闲置多长时间关闭连接 (0 表示关闭该功能)tcp-keepalive 300daemonize yes # 启用守护进程(默认是 no)supervised nopidfile /data/redis/redis.pidloglevel notice # 指定日志记录级别 (总共有4个级别:debug、verbose[默认]、notice、warning)logfile "/data/redis/redis.log" # 指定存放日志文件路径databases 16 # 设置数据库数量always-show-logo yes# 指定在多长时间内有多少次更新save 900 1 # 900秒内有一个更改save 300 10 # 300秒内有10个更改save 60 10000 # 60秒内有10000个更改stop-writes-on-bgsave-error yesrdbcompression yes #指定存储到本地数据库时是否压缩数据(默认 yes)dbfilename dump.rdb # 指定本地数据库文件名dir /data/redis/ # 指定本地数据库存放目录requirepass foobared # 设置Redis连接密码,默认关闭maxclients 128 # 设置同一时间最大客户端连接数maxmemory <bytes> # 指定Redis最大内存限制appendonly no # 指定是否在每次更新操作后进行日志记录appendfilename "appendonly.aof" # 指定更新日志文件名(默认)# 指定更新日志条件# no:等操作系统进行数据缓存同步到磁盘(快)appendfsync everysec # 表示每秒同步一次# 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法hash-max-ziplist-entries 512hash-max-ziplist-value 64# 指定是否激活重置哈希,默认为开启activerehashing yes
关闭和开启redis
[][][]
查看redis.log日志文件
[]27749:C 23 Sep 15:19:33.36727749:C 23 Sep 15:19:33.36727750:M 23 Sep 15:19:33.372 * Increased maximum number of open files to 10032 (it was originally set to 1024)._.__.-``__ ''-.__.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit.-`` .-```. ```\/ _.,_ ''-._( ' , .-` | `, ) Running in standalone mode|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379| `-._ `._ / _.-' | PID: 27750`-._ `-._ `-./ _.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'|| `-._`-._ _.-'_.-' | http://redis.io`-._ `-._`-.__.-'_.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'|| `-._`-._ _.-'_.-' |`-._ `-._`-.__.-'_.-' _.-'`-._ `-.__.-' _.-'`-._ _.-'`-.__.-'27750:M 23 Sep 15:19:33.37627750:M 23 Sep 15:19:33.377 * DB loaded from disk: 0.000 seconds27750:M 23 Sep 15:19:33.377 * Ready to accept connections
登录redis
[]localhost:6379> quit[]127.0.0.1:6379> quit[]
交互式和非交互式
# 交互式[root@redis conf]# redis-cli -h localhost -p 6379localhost:6379> select 1 # 使用select 切换表(总共有16个表)OKlocalhost:6379[1]> set name chuid # 写入name值为chuidOKlocalhost:6379[1]> get name # 获取name的值"chuid"localhost:6379[1]> set name chd # 修改name的值为chdOKlocalhost:6379[1]> get name # 获取name的值"chd"localhost:6379[1]> del name # 删除name(integer) 1localhost:6379[1]># 非交互式[root@redis conf]# redis-cli set name chuidOK[root@redis conf]# redis-cli127.0.0.1:6379> get name"chuid"127.0.0.1:6379> quit[root@redis conf]#
列表简单操作
[root@redis conf]# redis-cli# 建一个列表,录入规则是从左边开始127.0.0.1:6379> lpush names chd1(integer) 1127.0.0.1:6379> lpush names chd2(integer) 2127.0.0.1:6379> lpush names chd3(integer) 3127.0.0.1:6379> lrange names 0 31) "chd3"2) "chd2"3) "chd1"# 从右边录入使用rpush命令127.0.0.1:6379> rpush names chd5(integer) 2# 查看列表的值127.0.0.1:6379> lrange names 0 31) "chd3"2) "chd2"3) "chd1"# 从左边删除所有的chd元素127.0.0.1:6379> lrem names 0 chd(integer) 0# 删除最左边的chd元素127.0.0.1:6379> lrem names 1 chd(integer) 0# 删除列表最左边的元素127.0.0.1:6379> lpop names"chd3"# 删除最右边的元素127.0.0.1:6379> rpop names"chd1"# 修改左边起第一个元素为chd127.0.0.1:6379> lset names 0 chdOK127.0.0.1:6379>
集合录入
# 向集合中添加元素127.0.0.1:6379> sadd ages 26(integer) 1127.0.0.1:6379> sadd ages 27(integer) 1# 查看集合里的元素127.0.0.1:6379> smembers ages1) "26"2) "27"# 随机移除集合里的一个元素127.0.0.1:6379> spop ages"26"# 查找集合里是否有27的元素127.0.0.1:6379> sismember ages 27(integer) 1127.0.0.1:6379># 移除合集中的27元素127.0.0.1:6379> srem ages 27(integer) 1127.0.0.1:6379> sismember ages 27(integer) 0127.0.0.1:6379>
Redis的hash简单操作
# redis的hash操作# 增加一个hash,增加一个键值,key为name、value为chuid127.0.0.1:6379> hset info name 'chuid'(integer) 1127.0.0.1:6379> hset info age 27(integer) 1# 查看hash所有键值信息127.0.0.1:6379> hgetall info1) "name"2) "chuid"3) "age"4) "27"# 删除hash中所有键值127.0.0.1:6379> del info(integer) 1127.0.0.1:6379># 可以同时增加几个键值对127.0.0.1:6379> hmset info name 'chd' age 27OK127.0.0.1:6379> hgetall info1) "name"2) "chd"3) "age"4) "27"# 删除单个或多个键值信息,通过指定key127.0.0.1:6379> hdel info name age(integer) 2127.0.0.1:6379> del info(integer) 0
