1 安装wget命令 yum install wget ,然后执行 mkdir install 且进入 cd install
2 下载安装包:wget http://download.redis.io/releases/redis-5.0.8.tar.gz 进行 解压:tar xf redis-5.0.8.tar.gz
[root@localhost soft]# ll -h
total 1.9M
drwxrwxr-x 6 root root 4.0K Mar 12 08:07 redis-5.0.8
-rw-r--r-- 1 root root 1.9M Jun 6 23:44 redis-5.0.8.tar.gz
3 cd redis-src 看README.md ,执行编译命令: make
如果报错,执行yum install gcc , make distclean ,然后再次执行make
4 cd src 可以看到生成了可执行程序, cd .. 退出来后 make install PREFIX=/opt/soft/redis5
5 vi /etc/profile
export REDIS_HOME=/opt/soft/redis5
export PATH=$PATH:$REDIS_HOME/bin
查看配置的redis环境变量路径
source /etc/profile
echo $PATH
6 cd utils 然后执行 ./install_server.sh
1)一个物理机可以有多个redis实例(进程),通过port区分
2)可执行程序就一份在目录,但是内存中未来的多个实例需要各自的配置文件和持久化目录等资源
3) service redis_6379 start/stop/status
执行 cd /etc/init.d/ **** 可以看到新创建的redis_6379
4)脚本会帮启动redis
7 查看redis是否启动 ps -fe|grep redis
启动另外的实例:cd redis-util , ./install_server.sh
查看启动状态 service redis_6380 status ,
查看进程 pe -fe|grep redis
ps -ef |grep redis
[root@localhost ~]# ps -ef |grep redis
root 8326 1 0 Jun12 ? 00:01:14 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6379
root 8472 1 0 Jun12 ? 00:01:06 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6380
root 8937 1 0 Jun12 ? 00:00:38 /opt/mashibing/redis5/bin/redis-server 127.0.0.1:6381
root 11998 11585 0 03:44 pts/1 00:00:00 grep redis
redis-cli
默认6379端口,也可以指定端口和数据库 redis-cli -p 6380
,切换数据库select 6
;
或者直接输入:redis-cli -p 6380 -n 6
,表示在6380端口切换到6号数据库,redis有16个数据库,默认6379,0号库
[root@localhost ~]# redis-cli -p 6380 -n 6
127.0.0.1:6380[6]>
127.0.0.1:6380[6]> help
redis-cli 5.0.8
To get help about Redis commands type:
"help @" to get a list of commands in <group>
"help " for help on <command>
"help " to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6380[6]>
参照上面可以知道 :
help @se +Tab键补全
,有命令使用说明,或者直接help se+Tab键
同样有补全,eg:
127.0.0.1:6380[6]> help SET
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
127.0.0.1:6380[6]> FLUSHALL -- 清除所有值,正式环境勿用,等同flushdb
OK
127.0.0.1:6380[6]> keys * -- 查询所有key
(empty list or set)
set k1 abc nx
redis分布式锁主要命令以下测试set
里带nx
的 命令,当k1有值时, set k1 abc nx
里加nx表示k1无值时才会将其赋值为abc
,否则如果k1
已经有值,则使用 set...nx
不会赋值成功,redis
分布式锁也用到这个命令实现的。
特点: nx只能创建,xx只能更新。
127.0.0.1:6380[6]> set k1 1
OK
127.0.0.1:6380[6]> set k1 abc nx
(nil)
127.0.0.1:6380[6]> get k1
"1"
127.0.0.1:6380[6]> set k2 abc nx
OK
127.0.0.1:6380[6]> get k2
"abc"
127.0.0.1:6380[6]> clear -- 清屏
127.0.0.1:6380[6]> exit -- 退出客户端会话
字符串同时赋值多个key的值 :
127.0.0.1:6380[6]> mset a 1 b 2 c cc -- mset 多个键值赋值
OK
127.0.0.1:6380[6]> mget a b
1) "1"
2) "2"
127.0.0.1:6380[6]> mget a b c -- mget 多个键值取值
1) "1"
2) "2"
3) "cc"
127.0.0.1:6380[6]> get c
"cc"
输入: help @string
测试追加命令append
127.0.0.1:6380[6]> help @string
APPEND key value
summary: Append a value to a key
since: 2.0.0
............
-------------------------------------
127.0.0.1:6380[6]> set k3 hello
OK
127.0.0.1:6380[6]> append k3 ' world!' -- 追加命令 append
(integer) 12
127.0.0.1:6380[6]> get k3
"hello world!"
按范围获取或覆盖k3的指定位置的值,getrange 【正反向索引】 ,setrange ; eg:
-- 正反向索引
127.0.0.1:6380[6]> getrange k3 6 11
"world!"
127.0.0.1:6380[6]> getrange k3 6 -1
"world!"
127.0.0.1:6380[6]> getrange k3 0 -1
"hello world!"
127.0.0.1:6380[6]> getrange k3 6 11
"world!"
-------------------------------------
127.0.0.1:6380[6]> get k3
"hello world!"
127.0.0.1:6380[6]> SETRANGE k3 6 Q -- setRange 指定位置覆盖原值,从脚标6开始覆盖通过Q
(integer) 12
127.0.0.1:6380[6]> get k3
"hello Qorld!"
127.0.0.1:6380[6]> SETRANGE k3 6 xiaoQ
(integer) 12
127.0.0.1:6380[6]> get k3
"hello xiaoQ!"
获取长度strlen +key
127.0.0.1:6380[6]> strlen k3 -- 获取长度
(integer) 12
redis里没有int类型,因为string类型里也操作数值类型数据,如下
通过object 的encoding能够获取数值类型
127.0.0.1:6380[6]> FLUSHDB
OK
127.0.0.1:6380[6]> set k1 11
OK
127.0.0.1:6380[6]> type k1
string
127.0.0.1:6380[6]> set k2 hello
OK
127.0.0.1:6380[6]> type k2
string
127.0.0.1:6380[6]> help OBJECT
OBJECT subcommand [arguments [arguments ...]]
summary: Inspect the internals of Redis objects
since: 2.2.3
group: generic
127.0.0.1:6380[6]> OBJECT help
1) OBJECT <subcommand> arg arg ... arg. Subcommands are:
2) ENCODING <key> -- Return the kind of internal representation used in order to store the value associated with a key.
3) FREQ <key> -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.
4) IDLETIME <key> -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.
5) REFCOUNT <key> -- Return the number of references of the value associated with the specified key.
127.0.0.1:6380[6]>
127.0.0.1:6380[6]> OBJECT encoding k1 -- 通过encoding获取k1为数值类型
"int"
127.0.0.1:6380[6]> OBJECT encoding k2
"embstr"
incr +key 自增1 ,自增量incrby key increment ,自增浮点数,自减同理help获取方式,DECRBY ,DECR
127.0.0.1:6380[6]> incr k1
(integer) 12
127.0.0.1:6380[6]> get k1
"12"
127.0.0.1:6380[6]> help incr
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
group: string
127.0.0.1:6380[6]> help incrby
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
group: string
127.0.0.1:6380[6]> INCRBY k1 10 -- 增量为10
(integer) 22
127.0.0.1:6380[6]> get k1
"22"
127.0.0.1:6380[6]> INCRBYFLOAT k1 3.5 -- 增量为3.5 浮点数
"25.5"
127.0.0.1:6380[6]> DECRBY K1 11 -- 自减11
(integer) -11
127.0.0.1:6380[6]> GET K1
"-11"
127.0.0.1:6380[6]> DECR K1 -- 自减1
(integer) -12
127.0.0.1:6380[6]> GET K1
"-12"
redis二进制安全是因为数据存取是二进制的数据,存取不会保留数据编码格式。redis中是通过SDS实现字符串对象的工具;redis只取字节流。
当用户使用一个程序读取一个二进制文件时,该文件会被按照这个程序的规则进行解释。如果这个程序所使用的格式编码和文件被写入的格式编码一致,那么这个文件可以被正常读取,否则会使文件损坏,导致数据永久消失,所以重要文件最好放在只读的存储介质中,比如光盘。【hbase也是二进制安全的】
所以多语言开发倾向json,xml形式传输数据,不使用序列化的原因是需要额外增加编码器和解码器,不然容易出错,严重的话还会导致数据丢失。
eg:
读取数据编码格式为gb2312
127.0.0.1:6380[6]> set k1 中 -- 给k1赋值为 “中”
OK
127.0.0.1:6380[6]> strlen k1
(integer) 2
127.0.0.1:6380[6]> get k1
"\xd6\xd0" -- 十六进制存取
-------------------------------------------------
编码格式切换为ISO-8859-1 后给k2赋值
127.0.0.1:6380[6]> set k2 ? -- 给k2赋值为 “中”
OK
127.0.0.1:6380[6]> STRLEN k2 --不同于k1的编码,所以长度不一致
(integer) 1
127.0.0.1:6380[6]> get k2
"?"
127.0.0.1:6380[6]> exit
下面数据跟当前编码格式一致,则转换的就是对的,k2跟当前编码不一致,所以依然获取不到
[root@localhost ~]# redis-cli -p 6380 -n 6 --raw -- 格式化数据编码
127.0.0.1:6380[6]> get k2 -- 编码格式切换回gb2312后
?
127.0.0.1:6380[6]> get k1
中
127.0.0.1:6380[6]> help getset
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0
group: string
127.0.0.1:6380[6]> getset k1 abc
中
127.0.0.1:6380[6]> get k1
abc
给k1,k2赋值,用msetnx给k1,k3赋值失败,原因k1是存在的,执行此命令就失败了,所以整体失败了
nx只管创建,不管更新
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> mset k1 a k2 b
OK
127.0.0.1:6380[6]> mget k1 k2
a
b
127.0.0.1:6380[6]> MSETNX k1 aa k3 cc -- 返回结果不是ok 是0表示执行结果失败,原因nx只管创建,k1是存在的,k1执行失败,所以整体失败,体现该操作为原子性
0
127.0.0.1:6380[6]> mget k1 k2 k3
a
b
1 SETBIT key offset value – offset 是指二进制位的偏移量
一字节8个二进制位。
127.0.0.1:6380[6]> help setbit
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
group: string
127.0.0.1:6380[6]> FLUSHDB
OK
127.0.0.1:6380[6]> get keys
(nil)
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> STRLEN k1
(integer) 1
127.0.0.1:6380[6]> get k1
"@"
127.0.0.1:6380[6]> setbit k1 7 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A"
127.0.0.1:6380[6]> STRLEN k1
(integer) 1
127.0.0.1:6380[6]> setbit k1 9 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A@"
127.0.0.1:6380[6]> strlen k1
(integer) 2
验证ascii码值对应字符值:man ascii
[root@localhost ~]# man ascii
...
Oct Dec Hex Char Oct Dec Hex Char
------------------------------------------------------------------------
000 0 00 NUL '\0' 100 64 40 @
001 1 01 SOH (start of heading) 101 65 41 A
002 2 02 STX (start of text) 102 66 42 B
003 3 03 ETX (end of text) 103 67 43 C
004 4 04 EOT (end of transmission) 104 68
...
标准字符集为ascii,其他一般叫做扩展字符集;
扩展的含义:其他字符集不再对ascii重编码。
ascii码首位必须是0,其他可以操作
2 BITPOS key bit [start] [end]
bit 二进制位的值 start ,end表示字节
含义: 查找bit在字节区间首次出现的位置
127.0.0.1:6380[6]> help bitpos
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
group: string
127.0.0.1:6380[6]> bitpos k1 1 0 0
(integer) 1
127.0.0.1:6380[6]> bitpos k1 1 1 1
(integer) 9
到此,k1的值图文表示如下
3 BITCOUNT key [start end] 二进制位1的个数统计,在start end这个字节区间
127.0.0.1:6380[6]> bitpos k1 1 0 0
(integer) 1
127.0.0.1:6380[6]> bitpos k1 1 1 1
(integer) 9
127.0.0.1:6380[6]> BITCOUNT k1 0 0
(integer) 2
127.0.0.1:6380[6]> BITCOUNT k1 0 1
(integer) 3
127.0.0.1:6380[6]> BITCOUNT k1 1 1
(integer) 1
4 BITOP operation destkey key [key …]
operation与或非,亦或等之类的操作
destkey 目标key:操作的结果放在这里
key…:多个参与运算操作的key
127.0.0.1:6380[6]> help bitop
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
group: string
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k1 6 1
(integer) 0
127.0.0.1:6380[6]> FLUSHALL
OK
127.0.0.1:6380[6]> setbit k1 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k1 7 1
(integer) 0
127.0.0.1:6380[6]> setbit k2 1 1
(integer) 0
127.0.0.1:6380[6]> setbit k2 6 1
(integer) 0
127.0.0.1:6380[6]> get k1
"A"
127.0.0.1:6380[6]> get k2
"B"
127.0.0.1:6380[6]> bitop and targetkey k1 k2
(integer) 1
127.0.0.1:6380[6]> get targetkey
"@"
127.0.0.1:6380[6]> bitop or targetkey2 k1 k2
(integer) 1
127.0.0.1:6380[6]> get targetkey2
"C"
redis中的位图bitmap,很重要
redis:面向网络io ,多路复用的时候,单线程单进程就能处理;
mysql倾向bio ,比如十万百万个连接会到达很多请求,如果每个请求要出发到磁盘io,会导致带宽阻塞,就会等很久,所以mysql是用的bio
诚心欢迎各位交流探讨技术QQ:2402774969