Redis
Redis简介
Remote DIctionary Server(Redis)是一个由Salvatore Sanfilippo写的键值存储系统
Remote DIctionary Server(Redis)是一个由Salvatore Sanfilippo写的键值存储系统
开源的BSD许可,存储的值和建。作为缓存服务器,和数据库服务器。默认值16个库。提供了丰富的数据类型。String、 list,set等。做数据存储。已达到高效果。
Redis安装:...
Linux安装redis===================
1,安装Redis
先创建好安装路径:/data1/apps/redis2.6.14
1.1,下载及安装
wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
tar -zxvf redis-2.6.14.tar.gz
cd redis-2.6.14
make PREFIX=/data1/apps/redis2.6.14 install
mkdir /data1/apps/redis2.6.14/etc
1.5,可能会出现的错误提示
>>提示1:
make[3]: gcc:命令未找到
>>解决
yum -y install gcc-c++
>>提示2:
在包含自 adlist.c:34 的文件中:
zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
>>解决
make的时候加上 MALLOC=libc 参数
make PREFIX=/data/apps/redis2.6.14 MALLOC=libc install
cp redis.conf /etc/
这个文件是redis启动的配置文件
由于redis安装在/data1/apps/redis2.6.14/下,所以
cd /data1/apps/redis2.6.14/bin/进入这个目下,执行
cp redis-benchmark redis-cli redis-server /usr/bin/
这样就不用再执行时加上./了,而且可以在任何地方执行
echo 1 > /proc/sys/vm/overcommit_memory
设置内存分配策略(可选,根据服务器的实际情况进行设置) /proc/sys/vm/overcommit_memory
可选值:0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent 占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
开启redis端口,修改防火墙配置文件
vi /etc/sysconfig/iptables
加入端口配置
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
重新加载规则
service iptables restart
启动redis service
redis-server /etc/redis.conf
[3862] 19 Feb 23:10:56.339 * Max number of open files set to 10032
[3862] 19 Feb 23:10:56.347 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.14 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 3862
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[3862] 19 Feb 23:10:56.347 # Server started, Redis version 2.6.14
[3862] 19 Feb 23:10:56.347 * The server is now ready to accept connections on port 6379
查看进程,确定redis启动成功:
ps -ef | grep redis
[root@mtycentos ~]# ps -ef | grep redis
root 3862 3666 0 23:10 pts/1 00:00:00 redis-server /etc/redis.conf
root 3886 3866 0 23:11 pts/0 00:00:00 grep redis
如果启动失败,大部分情况下是因为redis.conf的配置有问题。
设置redis为后台守护进程:
vi /etc/redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
#daemonize no
daemonize yes
把no 改为 yes
修改完成后关闭redis : redis-cli shutdown
查看是否关闭:ps -ef | grep redis
再次启动为守护进程:redis-server /etc/redis.conf
[root@mtycentos ~]# ps -ef | grep redis
root 3913 1 0 23:16 ? 00:00:00 redis-server /etc/redis.conf
root 3917 3866 0 23:16 pts/0 00:00:00 grep redis
OK,启动成功
测试一把:
进入redis客户端命令行:
redis-cli 回车
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> set name hanlu
OK
redis 127.0.0.1:6379> get name
"hanlu"
说明成功了。
退出当前命令行:quit
关闭redis:
redis-cli shutdown
redis关闭后,缓存数据会自动保存到硬盘上,硬盘地址为redis.conf中的配置项dgfilename dump.rdb
强制备份数据到磁盘:
redis-cli save or redis-cli -p 6380 save (要制定端口进行备份)
开启redis.config:
redis-server /etc/redis.config
redis-cli常见命令:
set home 。。
keys *
keys *a* 模糊匹配
del home 删除
rename home yss 重命(只改键,为改值)--->flushdb
move key 值 *+-9
1.renamenx key newkey
作用: 把key改名为newkey
返回: 发生修改返回1,未发生修改返回0
注: nx--> not exists, 即, newkey不存在时,作改名动作
2.move key db
将key-value转移到其他db
3.exists key
判断key是否存在,返回1/0
4.type key
返回key存储的值的类型
有string,link,set,order set, hash
ttl key
作用: 查询key的生命周期
返回: 秒数
注:对于不存在的key或已过期的key/不过期的key,都返回-1
Redis2.8中,对于不存在的key,返回-2
获取一部分字符串:
getrange key start stop
作用: 是获取字符串中 [start, stop]范围的值
注意: 对于字符串的下标,左数从0开始,右数从-1开始
1.1,下载及安装
tar -zxvf redis-2.6.14.tar.gz
cd redis-2.6.14
make PREFIX=/data1/apps/redis2.6.14 install
mkdir /data1/apps/redis2.6.14/etc
>>提示1:
make[3]: gcc:命令未找到
>>解决
yum -y install gcc-c++
在包含自 adlist.c:34 的文件中:
zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
>>解决
make的时候加上 MALLOC=libc 参数
make PREFIX=/data/apps/redis2.6.14 MALLOC=libc install
cp redis.conf /etc/
这个文件是redis启动的配置文件
cd /data1/apps/redis2.6.14/bin/进入这个目下,执行
cp redis-benchmark redis-cli redis-server /usr/bin/
这样就不用再执行时加上./了,而且可以在任何地方执行
echo 1 > /proc/sys/vm/overcommit_memory
可选值:0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent 占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)
vi /etc/sysconfig/iptables
加入端口配置
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
service iptables restart
redis-server /etc/redis.conf
[3862] 19 Feb 23:10:56.347 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.14 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 3862
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[3862] 19 Feb 23:10:56.347 * The server is now ready to accept connections on port 6379
ps -ef | grep redis
[root@mtycentos ~]# ps -ef | grep redis
root 3862 3666 0 23:10 pts/1 00:00:00 redis-server /etc/redis.conf
root 3886 3866 0 23:11 pts/0 00:00:00 grep redis
设置redis为后台守护进程:
vi /etc/redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
#daemonize no
daemonize yes
把no 改为 yes
查看是否关闭:ps -ef | grep redis
[root@mtycentos ~]# ps -ef | grep redis
root 3913 1 0 23:16 ? 00:00:00 redis-server /etc/redis.conf
root 3917 3866 0 23:16 pts/0 00:00:00 grep redis
进入redis客户端命令行:
redis-cli 回车
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> set name hanlu
OK
redis 127.0.0.1:6379> get name
"hanlu"
关闭redis:
redis-cli shutdown
redis关闭后,缓存数据会自动保存到硬盘上,硬盘地址为redis.conf中的配置项dgfilename dump.rdb
强制备份数据到磁盘:
redis-cli save or redis-cli -p 6380 save (要制定端口进行备份)
set home 。。
keys *
keys *a* 模糊匹配
del home 删除
rename home yss 重命(只改键,为改值)--->flushdb
move key 值 *+-9
1.renamenx key newkey
作用: 把key改名为newkey
返回: 发生修改返回1,未发生修改返回0
注: nx--> not exists, 即, newkey不存在时,作改名动作
2.move key db
将key-value转移到其他db
3.exists key
判断key是否存在,返回1/0
4.type key
返回key存储的值的类型
有string,link,set,order set, hash
ttl key
作用: 查询key的生命周期
返回: 秒数
作用: 查询key的生命周期
返回: 秒数
注:对于不存在的key或已过期的key/不过期的key,都返回-1
Redis2.8中,对于不存在的key,返回-2
Redis2.8中,对于不存在的key,返回-2
获取一部分字符串:
getrange key start stop
作用: 是获取字符串中 [start, stop]范围的值
注意: 对于字符串的下标,左数从0开始,右数从-1开始
作用: 是获取字符串中 [start, stop]范围的值
注意: 对于字符串的下标,左数从0开始,右数从-1开始