最近公司打算上新项目需要用到Redis.所以就赶快学习学习这个目前很火热的NoSQL.下面简单介绍一下Redis
Redis本身是用C语言编写,支持网络、可持久化的日志型Key Value DB,当然还有一些其它的Key Value,例如大名鼎鼎的MemcacheDB、CouchDB等等.从2013年起Redis开发由VMware主持.
作为Key Value DB自然缺少不了键(Key)、键值(Key Value).除了常规的还提供:
Lists(列表)
Sets(集合)
Sorted Sets(有序集合)
Hashes(哈希表)
键值类型决定该键值的操作,同时,如果键值提供的是普通数字,Redis则提供自增等原子操作.
Redis持久化
一、使用截图将内存中的数据不断写入磁盘.
二、使用类似MySQL日志方式记录每次更新的日志.
前者性能高,但是无法避免数据丢失.后者相反.
Redis主从同步
Redis本身支持将数据同步到多台Slave上.
上面介绍这么多,下面开始重点安装Redis
Redis去官网下载.下面开始编译安装Redis
[mysql@localhost ~]$ cd redis-2.8.11
[mysql@localhost redis-2.8.11]$ ls
00-RELEASENOTES BUGS CONTRIBUTING COPYING deps INSTALL Makefile MANIFESTO README redis.conf runtest runtest-sentinel sentinel.conf src tests utils
[mysql@localhost redis-2.8.11]$ make
cd src && make all
make[1]: Entering directory `/home/mysql/redis-2.8.11/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
(cd ../deps && make distclean)
make[2]: Entering directory `/home/mysql/redis-2.8.11/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[2]: Leaving directory `/home/mysql/redis-2.8.11/deps'
(rm -f .make-*)
echo STD=-std=c99 -pedantic >> .make-settings
echo WARN=-Wall >> .make-settings
echo OPT=-O2 >> .make-settings
.......
.......
.......
.......
CC bio.o
CC rio.o
CC rand.o
CC memtest.o
CC crc64.o
CC bitops.o
CC sentinel.o
CC notify.o
CC setproctitle.o
CC hyperloglog.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
CC redis-check-dump.o
LINK redis-check-dump
CC redis-check-aof.o
LINK redis-check-aof
Hint: To run 'make test' is a good idea ;)
make[1]: Leaving directory `/home/mysql/redis-2.8.11/src'
[mysql@localhost redis-2.8.11]$ sudo yum install tcl -y
[mysql@localhost redis-2.8.11]$ make test
........
........
........
........
121 seconds - unit/type/zset
130 seconds - unit/basic
148 seconds - unit/type/list-3
107 seconds - unit/hyperloglog
175 seconds - integration/replication-3
200 seconds - unit/obuf-limits
364 seconds - unit/memefficiency
\o/ All tests passed without errors!
Cleanup: may take some time... OK
make[1]: Leaving directory `/home/mysql/redis-2.8.11/src'
至此Redis安装完毕了.
那么接下来看看如何配置、启动、关闭Redis
[mysql@localhost redis-2.8.11]$ sudo cp -p -r redis.conf /etc/
[sudo] password for mysql:
[mysql@localhost redis-2.8.11]$
redis.conf需求修改以下参数
daemonize
默认情况下Redis不在后台运行.需要在后台运行修改此参数
pidfile
Redis默认运行的pid文件,如果有多个Redis指定不同的位置
port
默认端口号6379
logfile
配置log文件地址,默认使用标准输出
dir
数据库镜像文件路径
--其它参数见后续博文^_^
启动Redis
如果不指定后台启动默认Redis启动打印如下信息:
[mysql@localhost redis-2.8.11]$ src/redis-server
[5338] 21 Jul 17:19:02.899 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
[5338] 21 Jul 17:19:02.900 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
[5338] 21 Jul 17:19:02.900 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
[5338] 21 Jul 17:19:02.900 # Current maximum open files is 1024. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5338
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
..................
..................
..................
[mysql@localhost redis-2.8.11]$ src/redis-server /etc/redis.conf
[mysql@localhost redis-2.8.11]$ ps -ef | grep redis
mysql 5300 1 0 16:55 ? 00:00:00 src/redis-server *:6379
mysql 5304 2262 0 16:55 pts/1 00:00:00 grep redis
现在我们操作Redis
[mysql@localhost redis-2.8.11]$ telnet 192.168.12.217 6379
Trying 192.168.12.217...
Connected to 192.168.12.217.
Escape character is '^]'.
插入数据
set name hello
+OK
查询数据
get name
hello
验证键值是否存在
exists name
:1
删除键值
del name
:1
exists name
:0
0代表Key不存在,1代表存在.
关闭Redis
[mysql@localhost redis-2.8.11]$ src/redis-cli shutdown
[mysql@localhost redis-2.8.11]$ ps -ef | grep redis
mysql 5307 2262 0 16:56 pts/1 00:00:00 grep redis
在安装过程中注意需要安装Tcl否则会报:
make: *** [test] Error 1
今天只是Redis介绍和入门,Redis才刚刚开始.^_^
阅读(6923) | 评论(0) | 转发(3) |