在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Redis数据库概述
Redis相比于其他数据库的优点
Redis安装部署 [root@redis ~]# yum install -y gcc gcc-c++ make [root@redis opt]# tar xzf redis-5.0.7.tar.gz [root@redis opt]# cd redis-5.0.7/ [root@redis redis-5.0.7]# make [root@redis redis-5.0.7]# make prefix=/usr/local/redis install [root@redis redis-5.0.7]# cd utils/ [root@redis utils]# ./install_server.sh #启动安装脚本 Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server ##这一步需要手动指定位置,其他都回车即可 [root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/ [root@localhost utils]# netstat -antp | grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 23276/redis-server Redis服务控制 /etc/init.d/redis_6379 stop /etc/init.d/redis_6379 start /etc/init.d/redis_6379 restart /etc/init.d/redis_6379 status 修改配置文件 [root@localhost utils]# vim /etc/redis/6379.conf [root@localhost utils]# /etc/init.d/redis_6379 restart #重启服务 Stopping ... Waiting for Redis to shutdown ... Redis stopped Starting Redis server... [root@localhost utils]# netstat -antp | grep redis tcp 0 0 192.168.118.88:6379 0.0.0.0:* LISTEN 23492/redis-server tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 23492/redis-server Redis命令工具redis-server:用于启动Redis的工具 redis-benchmark:用于检测Redis在本机的运行效率 redis-check-aof:修复AOF持久化文件 redis-check-rdb:修复RDB持久化文件 redis-cli:Redis命令行工具 redis-cli -h 远程主机ip -p 服务端口号 -a 指定密码 如果没有设置数据库密码,可以省略-a选项,若不添加任何选项表示,这使用127.0.0.1:6379连接本机上的Redis数据库 Redis测试工具redis-benckmarkredis-benchmark 选项: -h :指定服务器主机名 -P :指定服务器端口 -s :指定服务器socket(套接字) -c :指定并发连接数 -n :指定请求数 -d :以字节的形式指定SET/GET值的数据大小 -k :1=keep alive 0=reconnect -r :SET/GET/INCR 使用随机key,SADD使用随机值 -p :通过管道传输<numreq>请求 -q :强制退出redis -l :生成循环,永久执行测试 -t :仅运行以逗号分割的测试命令列表 -I :Idle模式,仅打开N哥idle连接并等待 [root@localhost utils]# redis-benchmark -h 192.168.118.88 -P 6379 -c 100 -n 10000 ##向主机发送100哥并发连接和10000哥请求测试性能 [root@localhost utils]# redis-benchmark -h 192.168.118.88 -p 6379 -q -d 10 ##测试存取10字节的数据包性能 PING_INLINE: 925925.88 requests per second PING_BULK: 1470588.12 requests per second SET: 900900.88 requests per second GET: 934579.44 requests per second INCR: 1136363.62 requests per second LPUSH: 578034.69 requests per second RPUSH: 793650.75 requests per second LPOP: 584795.31 requests per second RPOP: 806451.62 requests per second SADD: 840336.12 requests per second HSET: 746268.62 requests per second SPOP: 1098901.12 requests per second LPUSH (needed to benchmark LRANGE): 671140.94 requests per second LRANGE_100 (first 100 elements): 26089.22 requests per second LRANGE_300 (first 300 elements): 5534.65 requests per second LRANGE_500 (first 450 elements): 3414.37 requests per second LRANGE_600 (first 600 elements): 2345.11 requests per second MSET (10 keys): 120048.02 requests per second [root@localhost utils]# redis-benchmark -t set,lpush -n 100000 -q ##测试本机Redis服务在进行set和lpush操作时的性能 SET: 166112.95 requests per second LPUSH: 191570.88 requests per second Redis数据库数据类型
String数据类型[root@redis utils]# redis-cli 127.0.0.1:6379> [root@redis utils]# redis-cli #查看所有的键 127.0.0.1:6379> keys * 1) "counter:__rand_int__" 2) "myset:__rand_int__" 3) "mylist" 4) "key:__rand_int__" 127.0.0.1:6379> keys m* #查看以m开头所有键 1) "myset:__rand_int__" 2) "mylist" 创建数据,获取数据 127.0.0.1:6379> set whd zhangsan #创建一个键whd,值为zhangsan OK 127.0.0.1:6379> keys * #查看创建结果 1) "counter:__rand_int__" 2) "myset:__rand_int__" 3) "mylist" 4) "key:__rand_int__" 5) "whd" 127.0.0.1:6379> get whd #获取键值 "zhangsan" 查看键是否存在 127.0.0.1:6379> exists whd #存在返回1,不存在返回为0 (integer) 1 127.0.0.1:6379> exists www (integer) 0 删除键 127.0.0.1:6379> del whd (integer) 1 127.0.0.1:6379> exists whd (integer) 0 查看键中value的数据类型 127.0.0.1:6379> set aaa bbb OK 127.0.0.1:6379> type aaa string 对键重命名 127.0.0.1:6379> rename aaa ccc OK 127.0.0.1:6379> keys * 1) "counter:__rand_int__" 2) "myset:__rand_int__" 3) "mylist" 4) "key:__rand_int__" 5) "ccc" ##rename也可用来覆盖键值 ```bash 127.0.0.1:6379> get ccc "bbb" 127.0.0.1:6379> set ddd fff #新建键ddd,值为fff OK 127.0.0.1:6379> rename ccc ddd #将ccc值覆盖ddd OK 127.0.0.1:6379> get ddd #查看结果 "bbb" ##renamens,可以判断重命名的新键名是否存在,存在则返回0不做操作,不存在则覆盖并重命名 127.0.0.1:6379> set aaa bbb OK 127.0.0.1:6379> renamenx ddd aaa (integer) 0 查看当前数据库key的数目 127.0.0.1:6379> dbsize (integer) 6 设置密码,删除密码 127.0.0.1:6379> config set requirepass 123123 #设置新密码 OK 127.0.0.1:6379> auth 123123 #认证密码 OK 127.0.0.1:6379> config get requirepass #查看密码 1) "requirepass" 2) "123123" [root@redis utils]# redis-cli -h 192.168.118.88 -p 6379 #此时可以登录数据库,但是不能进行操作 192.168.118.88:6379> keys * (error) NOAUTH Authentication required. 192.168.118.88:6379> auth 123123 #进行密码认证后才能操作 OK 192.168.118.88:6379> keys * 1) "counter:__rand_int__" 2) "aaa" 3) "ddd" 4) "myset:__rand_int__" 5) "mylist" 6) "key:__rand_int__" 192.168.118.88:6379> config set requirepass '' #删除密码 OK 追加数据 127.0.0.1:6379> keys * 1) "counter:__rand_int__" 2) "aaa" 3) "ddd" 4) "myset:__rand_int__" 5) "mylist" 6) "key:__rand_int__" 7) "www" 127.0.0.1:6379> get www "10" 127.0.0.1:6379> append www 20 #append,追加数值 (integer) 4 127.0.0.1:6379> get www "1020" 127.0.0.1:6379> append whd 23 #如果键不存在,append命令相当于set创建键,并赋值 (integer) 2 127.0.0.1:6379> get whd "23" 127.0.0.1:6379> strlen whd #strlen判断键值得长度 (integer) 2 key值递增/递减,incr/decr/incrby/decrby 127.0.0.1:6379> get whd "23" 127.0.0.1:6379> incr whd #INCR递增数值(key值必须为整数) (integer) 24 127.0.0.1:6379> incr whd (integer) 25 127.0.0.1:6379> decr whd #DECR递减数值(key值必须为整数) (integer) 24 127.0.0.1:6379> decr whd (integer) 23 127.0.0.1:6379> incrby whd 10 #按照指定数值进行递增 (integer) 33 127.0.0.1:6379> get whd "33" 127.0.0.1:6379> decrby whd 5 #按照指定数值进行递减 (integer) 28 127.0.0.1:6379> getset返回原有值然后给key设置新值 127.0.0.1:6379> getset whd 22 #先返回原有key值50,然后给予key新值22(新的值不会打印显示) "50" 127.0.0.1:6379> get whd #查看key新的值 "22" 127.0.0.1:6379> getset whd 66 "22" 127.0.0.1:6379> get whd "66" setex支持key的过期时间 127.0.0.1:6379> setex ttt 10 66 #指定ttt10秒过期 OK 127.0.0.1:6379> ttl ttt #ttl可查看生命周期 (integer) 3 127.0.0.1:6379> ttl ttt #查出-2表示已经过期,-1表示永久保存 (integer) -2 127.0.0.1:6379> get ttt (nil) 127.0.0.1:6379> ttl whd (integer) -1127.0.0.1:6379> setnx whd 22 (integer) 0 127.0.0.1:6379> setnx whd 22 #setnx可以对键进行判断,有则操作无效,无则创建 (integer) 0 批量创建,批量查看 127.0.0.1:6379> mset aa 10 bb 20 cc 30 OK 127.0.0.1:6379> keys * 1) "counter:__rand_int__" 2) "cc" 3) "myset:__rand_int__" 4) "whd" 5) "aa" 6) "key:__rand_int__" 7) "mylist" 8) "bb" 127.0.0.1:6379> mget aa bb cc 1) "10" 2) "20" 3) "30" 127.0.0.1:6379> msetnx aa 10 jj 10 dd 10 #msetnx可进行判断,只要有一个键存在,则操作失效,当键都不存在才创建key (integer) 0 127.0.0.1:6379> msetnx jj 10 dd 10 (integer) 1 List数据类型127.0.0.1:6379> lpush age a b c d e #lpush由左插入多个键值, (integer) 5 127.0.0.1:6379> lrange age 0 -1 #lrange查看list数据类型的key值,后面要加查看的范围,这里0表示第一个数,-1表示最后一个数(也可以输入key值个数), 1) "e" 2) "d" 3) "c" 4) "b" 5) "a" 127.0.0.1:6379> lrange age 1 2 1) "d" 2) "c" 127.0.0.1:6379> lrange age 3 5 1) "b" 2) "a" 127.0.0.1:6379> lpushx age g #lpushx插入一个元素在一个已存在的key的开头 (integer) 6 127.0.0.1:6379> lrange age 0 -1 1) "g" 2) "e" 3) "d" 4) "c" 5) "b" 6) "a" 127.0.0.1:6379> lpop age #lpop移除首个元素 "g" 127.0.0.1:6379> lrange age 0 -1 1) "e" 2) "d" 3) "c" 4) "b" 5) "a" 127.0.0.1:6379> lpop age "e" 127.0.0.1:6379> lrange age 0 -1 1) "d" 2) "c" 3) "b" 4) "a" 127.0.0.1:6379> llen age #获取key值个数 (integer) 4 LREM/LSET/LINDEX/LTRIM 127.0.0.1:6379> del age (integer) 1 127.0.0.1:6379> lpush age a b c d d a c (integer) 7 127.0.0.1:6379> lrange age 0 -1 1) "c" 2) "a" 3) "d" 4) "d" 5) "c" 6) "b" 7) "a" 127.0.0.1:6379> lrem age 1 a #指定删除从开头开始n个重复值 (integer) 1 127.0.0.1:6379> lrange age 0 -1 1) "c" 2) "d" 3) "d" 4) "c" 5) "b" 6) "a" 127.0.0.1:6379> lrem age 2 c (integer) 2 127.0.0.1:6379> lrange age 0 -1 1) "d" 2) "d" 3) "b" 4) "a" 127.0.0.1:6379> lindex age 1 #获取索引值对应的key值,索引值从上往下从0开始 "d" 127.0.0.1:6379> lindex age 3 "a" 127.0.0.1:6379> lset age 1 a #基于已有的索引值更换key值 OK 127.0.0.1:6379> lset age 3 c OK 127.0.0.1:6379> lrange age 0 -1 1) "d" 2) "a" 3) "b" 4) "c" 127.0.0.1:6379> lrange whd1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "c" 6) "b" 7) "b" 127.0.0.1:6379> ltrim whd1 0 3 #仅保留索引范围的元素 OK 127.0.0.1:6379> lrange whd1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 在指定元素的左/右插入元素 127.0.0.1:6379> linsert whd1 before a a1 #在a的前面插入元素a1 (integer) 5 127.0.0.1:6379> lrange whd1 0 -1 1) "a1" 2) "a" 3) "b" 4) "c" 5) "d" 127.0.0.1:6379> linsert whd1 after a a2 #在a的后面插入元素a2 (integer) 6 127.0.0.1:6379> lrange whd1 0 -1 1) "a1" 2) "a" 3) "a2" 4) "b" 5) "c" 6) "d" 由右插入元素 127.0.0.1:6379> rpush whd2 a b c d e (integer) 5 127.0.0.1:6379> lrange whd2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 127.0.0.1:6379> rpushx whd2 a1 #在最后插入元素 (integer) 6 127.0.0.1:6379> lrange whd2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "a1" 127.0.0.1:6379> rpop whd2 #移除最后一个元素 "a1" 127.0.0.1:6379> lrange whd2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 127.0.0.1:6379> rpoplpush whd2 whd2 #组合使用,右弹出左插入,相当于把原key值最后一个元素插入到目标key值得最前面 "e" 127.0.0.1:6379> lrange whd2 0 -1 1) "e" 2) "a" 3) "b" 4) "c" 5) "d" 127.0.0.1:6379> rpoplpush whd2 whd1 "d" 127.0.0.1:6379> lrange whd1 0 -1 1) "d" 2) "a1" 3) "a" 4) "a2" 5) "b" 6) "c" 7) "d" Hash(散列)数据类型
127.0.0.1:6379> hset caiwu name "lisi" #创建键,设置字段为name,值为lisi (integer) 1 127.0.0.1:6379> hget caiwu name "lisi" 127.0.0.1:6379> hset caiwu name "wangwu" #为该键添加字段name1,值为wangwu (integer) 0 127.0.0.1:6379> hset caiwu name1 "wangwu" (integer) 1 127.0.0.1:6379> hexists caiwu name #判断caiwu键中name字段是否存在 (integer) 1 127.0.0.1:6379> hlen caiwu #查看key值里面右多少个字段 (integer) 2 127.0.0.1:6379> hdel caiwu name #删除字段 (integer) 1 127.0.0.1:6379> hexists caiwu name (integer) 0 127.0.0.1:6379> hsetnx caiwu name "aa" #添加字段 (integer) 1 127.0.0.1:6379> hget caiwu name "aa" 127.0.0.1:6379> hset caiwu name2 2 (integer) 1 127.0.0.1:6379> hincrby caiwu name2 2 #递增字段值 (integer) 4 127.0.0.1:6379> hincrby caiwu name2 -2 #递减字段值 (integer) 2 127.0.0.1:6379> hgetall caiwu #查看key中所有信息 1) "name1" 2) "wangwu" 3) "name" 4) "aa" 5) "name2" 6) "2" 127.0.0.1:6379> hmget caiwu name name1 #查看key的多个字段值 1) "aa" 2) "wangwu" 127.0.0.1:6379> hkeys caiwu #查看键的多个字段 1) "name1" 2) "name" 3) "name2" 127.0.0.1:6379> hvals caiwu #查看键中所有的字段值 1) "wangwu" 2) "aa" 3) "2" set数据类型(无序集合)
127.0.0.1:6379> keys * 1) "counter:__rand_int__" 2) "myset:__rand_int__" 3) "key:__rand_int__" 4) "mylist" 5) "caiwu" 127.0.0.1:6379> sadd whd a b c d #添加键,设置键值 (integer) 4 127.0.0.1:6379> smembers whd #查看键值 1) "a" 2) "c" 3) "b" 4) "d" 127.0.0.1:6379> scard whd #查看key值个数 (integer) 4 127.0.0.1:6379> sismember whd a #判断key值是否存在,存在则返回1,不存在则返回0 (integer) 1 127.0.0.1:6379> sismember whd g (integer) 0 127.0.0.1:6379> srandmember whd #srandmember表示随机返回一个成员 "b" 127.0.0.1:6379> srandmember whd "a" 随机移除键值 127.0.0.1:6379> spop whd #随机移除数值,后面不加参数则默认移除一个,可以添加数值,表示移除该数值个数的元素 "a" 127.0.0.1:6379> smembers whd 1) "c" 2) "b" 3) "d" 127.0.0.1:6379> spop whd 2 1) "d" 2) "b" 127.0.0.1:6379> smembers whd 1) "c" 127.0.0.1:6379> sadd whd a b e f #添加元素,有重复值不会插入 (integer) 4 127.0.0.1:6379> smembers whd 1) "a" 2) "e" 3) "b" 4) "c" 5) "f" 127.0.0.1:6379> srem whd a e b #移除指定元素 (integer) 3 127.0.0.1:6379> smembers whd 1) "f" 2) "c" 127.0.0.1:6379> sadd whd1 a b #设置新键 (integer) 2 127.0.0.1:6379> smove whd whd1 c #讲whd中的c移动到whd1中 (integer) 1 127.0.0.1:6379> smembers whd 1) "f" 127.0.0.1:6379> smembers whd1 1) "a" 2) "c" 3) "b" Sorted Set 数据类型(有序集合)
127.0.0.1:6379> zadd whd 1 "zhansan" #添加一个数值为1的成员zhansan (integer) 1 127.0.0.1:6379> zadd whd 2 "lisi" 3 "wangwu" #添加两个成员,对应数值为2,3 (integer) 2 127.0.0.1:6379> zrange whd 0 -1 #查看whd的各元素值 1) "zhansan" 2) "lisi" 3) "wangwu" 127.0.0.1:6379> zrange whd 0 -1 WITHSCORES #WITHSCORES表示返回的结果中包含每个成员及其分数,否则返回成员 1) "zhansan" 2) "1" 3) "lisi" 4) "2" 5) "wangwu" 6) "3" 127.0.0.1:6379> zrank whd "zhansan" #获取成员对应的位置索引,0代表第一个,nil表示不存在 (integer) 0 127.0.0.1:6379> zrank whd "lisi" (integer) 1 127.0.0.1:6379> zcard whd #查看成员数 (integer) 3 127.0.0.1:6379> zcount whd 2 3 #分数满足表达式2<= score <=3的成员数量 (integer) 2 127.0.0.1:6379> zcount whd 1 2 (integer) 2 127.0.0.1:6379> zrem whd lisi #删除指定成员 (integer) 1 127.0.0.1:6379> zrange whd 0 -1 1) "zhansan" 2) "wangwu" 127.0.0.1:6379> zscore whd zhansan #返回成员所对应的分数 "1" 127.0.0.1:6379> zrange whd 0 -1 WITHSCORES 1) "zhansan" 2) "1" 3) "wangwu" 4) "3" 127.0.0.1:6379> zincrby whd 5 zhansan #为成员的分数增加指定值 "6" 127.0.0.1:6379> zrange whd 0 -1 WITHSCORES 1) "wangwu" 2) "3" 3) "zhansan" 4) "6" 127.0.0.1:6379> zadd whd 2 zhansan #添加已有成员设置分数时,会覆盖原因成员和分数,并返回为0.表示没有增加元素,但不表示没有修改 (integer) 0 127.0.0.1:6379> zrange whd 0 -1 WITHSCORES 1) "zhansan" 2) "2" 3) "wangwu" 4) "3" 127.0.0.1:6379> zrangebyscore whd 1 2 #获取分数满足1<=score<=2的成员 1) "zhansan" 2) "lisi" 127.0.0.1:6379> zrange whd 0 -1 1) "lisi" 2) "zhansan" 3) "xuyi" 4) "wangwu" 127.0.0.1:6379> zrangebyscore whd -inf +inf limit 1 3 #-inf表示第一个成员,+inf表示最后一个成员,这里表示匹配所有成员,然后limit显示索引1后面的三个成员 1) "zhansan" 2) "xuyi" 3) "wangwu" 127.0.0.1:6379> zremrangebyrank whd 0 1 #删除位置索引满足表达式0<=rank<=1的元素 (integer) 2 127.0.0.1:6379> zcard whd (integer) 2 127.0.0.1:6379> zrange whd 0 -1 1) "xuyi" 2) "wangwu" 排序 127.0.0.1:6379> zadd whd1 1 a 2 b 3 d 4 e (integer) 4 127.0.0.1:6379> zrevrange whd 0 -1 withscores 1) "wangwu" 2) "3" 3) "xuyi" 4) "2" 127.0.0.1:6379> zrevrange whd1 0 -1 withscores #以位置索引从高到低显示区间的值 1) "e" 2) "4" 3) "d" 4) "3" 5) "b" 6) "2" 7) "a" 8) "1" 到此这篇关于Redis数据库安装部署及基本操作的文章就介绍到这了,更多相关Redis安装部署内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论