一、redis介绍
1、redis简介
REmote DIctionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统。redis和memcached缓存服务很像,但是redis支持的数据存储类型更丰富,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)等;
这些数据类型都支持push/pop、add/remove及取交集、并集和差集及更丰富的操作,而且这些操作都是原子性的;在此基础上,redis支持各种不同方式的排序,与memcached缓存服务一样,为了保证效率,数据都是缓存在内存中提供服务;和MC不同的是,redis持久化缓存服务还会周期性的把更新的数据写入到磁盘以及把修改的操作记录追加到文件里记录下来,比MC更有优势的是,redis还支持master-slave(主从)同步,这点很类似关系型数据库MySQL。
redis是一种非关系型数据存储工具,这区别于传统的关系型数据库,类似于memcache,并且其内部集成了对list(链表)、set(集合)的操作,可以很方便快速的处理数据(像插入、删除list取交集 并集 差集等),这极大的减轻了底层数据库的压力,并且给用户更快的响应速度。
redis的出现,在一定程度上弥补了MC这类key-value内存缓存服务的不足,在部分场合可以对关系数据库起到很好的补充作用;redis提供了Python,Ruby,Erlang,PHP客户端,使用很方便;redis官方文档如下:
http://www.redis.io/documentation
http://www.redis.cn
2、redis的优点
性能很高:redis能支持超过100K+每秒的读写频率(10万+)
丰富的数据类型:redis支持二进制的Strings,Lists,Hashes,Sets及Orderd Sets等数据类型操作
原子:redis的所有操作都是原子性的,同时redis还支持对几个操作全并后的原子性执行
丰富的特性:redis还支持publish/subscribe,通知key过期等等特性
redis支持异步机制主从复制
3、redis的应用场景
传统的MySQL + Memcached的网站架构遇到的问题:
MySQL数据库实际上是适合进行海量数据存储的,加上通过MC将热点数据存放到内存cache里,达到加速数据访问的目的;绝大部分公司都曾经使用过这样的架构,但随着业务数据量的不断增加,和访问量的持续增长,很多问题就会暴漏出来:
1. 需要不断的对MySQL进行拆库拆表,MC也需要不断跟着扩容,扩容和维护工作占据大量开发运维时间;
2. MC与MySQL数据库数据一致性问题是个老大难题;
3. MC数据命中率低或down机,会导致大量访问直接穿透到数据库,导致MySQL无法支持访问;
4. 跨机房cache同步一致性问题
redis的最佳应用场景:
1. redis最佳适用场景是全部数据in-memory
2. redis更多场景是作为MC的替代品来使用
3. 支持持久化
4. 当需要除key/value之外的更多数据类型支持时,使用redis更合适
5. 需要负载均衡的场景(redis主从同步)
更多Redis作者谈Redis应用场景见:http://blog.nosqlfan.com/html/2235.html
对Redis数据库小结:
1. 提高了DB的可扩展性,只需要将新加的数据放到新加的服务器上就可以了;
2. 提高了DB的可用性,只影响到需要访问的shard服务器上的数据的用户;
3. 提高了DB的可维护性,对系统的升级和配置可以按shard一个个来搞,对服务产就是灾难;
4. 当Redis物理内存使用超过内存总容量的3/5时,就会开始比较危险了,就开始做swap,内存碎片大;
5. 当达到最大内存时,会清空带有过期时间的key,即使key未到过期时间;
6. redis与DB同步写的问题,先写DB,后写redis,因为写内存基本上没有问题;
二、redis部署
1、下载redis源码包
[root@cache tools]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz --2015-06-04 04:03:10-- http://download.redis.io/releases/redis-2.8.9.tar.gz Resolving download.redis.io...109.74.203.151 Connecting todownload.redis.io|109.74.203.151|:80... connected. HTTP request sent, awaiting response...200 OK Length: 1097369 (1.0M)[application/x-gzip] Saving to: “redis-2.8.9.tar.gz” 100%[=======================================================================================>]1,097,369 4.52K/s in 2m 45s 2015-06-04 04:06:05 (6.49 KB/s) -“redis-2.8.9.tar.gz” saved [1097369/1097369]
2、解压并编译安装
# tar fxzredis-2.8.9.tar.gz [root@cache tools]# cdredis-2.8.9 [root@cache redis-2.8.9]# lessREADME --> 查看安装等信息的详细手册 Where to find complete Redisdocumentation? ------------------------------------------- This README is just a fast"quick start" document. You can find more detailed documentation athttp://redis.io Building Redis -------------- Redis can be compiled and usedon Linux, OSX, OpenBSD, NetBSD, FreeBSD. We support big endian andlittle endian architectures. It may compile on Solarisderived systems (for instance SmartOS) but our support for this platform is"best effort" and Redis is not guaranteed to work as well as in Linux, OSX,and *BSD there. It is as simple as: % make You can run a 32 bit Redisbinary using: % make 32bit After building Redis is a goodidea to test it, using: % make test Fixing problems building 32bit binaries : [root@cache redis-2.8.9]#make MALLOC=jemalloc # 开始编译 cd src && make all make[1]: Entering directory `/application/tools/redis-2.8.9/src‘ rm -rf redis-serverredis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o*.gcda *.gcno *.gcov redis.info lcov-html (cd ../deps && makedistclean) make[2]: Entering directory`/application/tools/redis-2.8.9/deps‘ (cd hiredis && makeclean) > /dev/null || true (cd linenoise && makeclean) > /dev/null || true (cd lua && make clean)> /dev/null || true (cd jemalloc && [ -fMakefile ] && make distclean) > /dev/null || true (rm -f .make-*) make[2]: Leaving directory`/application/tools/redis-2.8.9/deps‘ (rm -f .make-*) echo STD=-std=c99 -pedantic>> .make-settings ... 中间忽略 ... Hint: To run ‘make test‘ is agood idea ;) make[1]: Leaving directory`/application/tools/redis-2.8.9/src‘ [root@cache redis-2.8.9]# makePREFIX=/application/redis-2.8.9 install # 安装到/application/目录 cd src && make install make[1]: Entering directory`/application/tools/redis-2.8.9/src‘ Hint: To run ‘make test‘ is agood idea ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory`/application/tools/redis-2.8.9/src‘ # 创建软连接,并查看 [root@cache redis-2.8.9]# ln-s /application/redis-2.8.9/ /application/redis [root@cache redis-2.8.9]# ll/application/redis lrwxrwxrwx. 1 root root 25Jun 4 20:05 /application/redis ->/application/redis-2.8.9/ # 查看redis的目录结构,并简单说明: [root@cache redis-2.8.9]# LANG=EN [root@cache redis-2.8.9]# tree/application/redis/bin/ /application/redis/bin/ |-- redis-benchmark # redis性能测试工具,测试redis在你的系统及你的配置下的读写性能 |-- redis-check-aof # 更新日志检查 |-- redis-check-dump # 用于本地数据库检查 |-- redis-cli # redis命令行操作工具;当然,也可以用telnet根据其纯文本协议来操作 └-- redis-server # redis服务器的daemon启动程序 0 directories, 5 files
3、配置并启动redis服务
a) 配置redis环境变量 [root@cache ~]# exportPATH=/application/redis/bin/:$PATH # 该行只是临时生效redis命令的环境变量 [root@cache ~]#redis-server # 在命令行下测试该命令,如果能tab键出来,表示为成功; [root@cache ~]# echo"PATH=/application/redis/bin/:$PATH" >>/etc/profile # 写入到配置文件,使其永久生效 [root@cache ~]# tail -1/etc/profile # 查看是否添加正确 PATH=/application/redis/bin/:/application/redis/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@cache redis-2.8.9]# ./etc/profile # 执行后,使配置立即生效 [root@cache redis-2.8.9]# whichredis-server # 查看是否有redis-server命令及位置 /application/redis/bin/redis-server b)查看命令帮助 [root@cache redis-2.8.9]# redis-server--help Usage: ./redis-server[/path/to/redis.conf] [options] ./redis-server - (read config fromstdin) ./redis-server -v or --version ./redis-server -h or --help ./redis-server --test-memory<megabytes> Examples: ./redis-server (run the server withdefault conf) ./redis-server /etc/redis/6379.conf ./redis-server --port 7777 ./redis-server --port 7777 --slaveof127.0.0.1 8888 ./redis-server /etc/myredis.conf--loglevel verbose Sentinel mode: ./redis-server /etc/sentinel.conf--sentinel c)启动redis服务 [root@cache ~]# mkdir/application/redis/conf # 1)创建redis的配置文件目录 [root@cache ~]# cp/application/tools/redis-2.8.9/redis.conf /application/redis/conf/ # 2) 拷贝源码包目录下的redis.conf文件 [root@cache ~]# sysctlvm.overcommit_memory=1 # 3) 执行sysctl命令,分配内存大小 vm.overcommit_memory = 1 [root@cache ~]# redis-server/application/redis/conf/redis.conf & # 启动服务 [1] 15623 [root@cache ~]# [15623] 04 Jun20:35:02.093 * Increased maximum number of open files to 10032 (it wasoriginally set to 1024). _._ _.-``__ ‘‘-._ _.-`` `. `_. ‘‘-._ Redis 2.8.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ‘‘-._ ( ‘ , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379 | `-._ `._ / _.-‘ | PID: 15623 `-._ `-._ `-./ _.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | http://redis.io `-._ `-._`-.__.-‘_.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | `-._ `-._`-.__.-‘_.-‘ _.-‘ `-._ `-.__.-‘ _.-‘ `-._ _.-‘ `-.__.-‘ [15623] 04 Jun 20:35:02.097 #Server started, Redis version 2.8.9 [15623] 04 Jun 20:35:02.097 *The server is now ready to accept connections on port 6379 [root@cache ~]# lsof-i:6379 #查看redis是否启动 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODENAME redis-ser 15623 root 4u IPv6 31395 0t0 TCP *:6379 (LISTEN) redis-ser 15623 root 5u IPv4 31397 0t0 TCP*:6379 (LISTEN)
如果不执行 sysctl vm.overcommit_memory=1 回提示如下错误信息:
[15558] 04 Jun 20:16:37.324 #Server started, Redis version 2.8.9 [15558] 04 Jun 20:16:37.325 #WARNING overcommit_memory is set to 0! Background save may fail under lowmemory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf andthen reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect.
关闭redis服务
[root@cache ~]# redis-clishutdown [15623] 04 Jun 20:45:24.974 #User requested shutdown... [15623] 04 Jun 20:45:24.974 *Saving the final RDB snapshot before exiting. [15623] 04 Jun 20:45:24.979 *DB saved on disk [15623] 04 Jun 20:45:24.979 #Redis is now ready to exit, bye bye... [1]+ Done redis-server/application/redis/conf/redis.conf
4、php扩展redis客户端服务
1)下载redis客户端程序 [root@web1 tools]# wgethttp://github.com/nicolasff/phpredis/archive/master.zip --2015-06-03 06:06:53-- http://github.com/nicolasff/phpredis/archive/master.zip Connecting tocodeload.github.com|192.30.252.144|:443... connected. HTTP request sent, awaitingresponse... 200 OK Length: unspecified[application/zip] Saving to: “master.zip” [ <=> ] 149,906 18.2K/s in 8.1s 2015-06-03 06:07:06 (18.2KB/s) - “master.zip” saved [149906] 2) 查看并解压缩 [root@web1 tools]# lsmaster.zip master.zip [root@web1 tools]# unzipmaster.zip 3) 生成phpize文件 [root@web1 tools]# cdphpredis-master/ [root@web1 phpredis-master]# /application/php/bin/phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 4)编译安装 [root@web1 phpredis-master]# ./configure--with-php-config=/application/php/bin/php-config ... 中间省略 ... creating libtool appending configuration tag"CXX" to libtool configure: creating./config.status config.status: creatingconfig.h # 出现上面3条信息,表示安装成功;接下来进行编译 [root@web1 phpredis-master]#make && make install # 提示下面信息,表示编译安装成功,并记录该信息;后面做php.ini配置文件所使用 Installing sharedextensions: /application/php-5.3.27/lib/php/extensions/no-debug-non-zts-20090626/ 5) 修改php.ini配置文件,内容如下: [root@web1 ~]# ll/application/php/lib/php/extensions/no-debug-non-zts-20090626/ total 1156 -rwxr-xr-x. 1 root root 246672Jun 2 01:10memcache.so -rwxr-xr-x. 1 root root 931913Jun 3 06:18redis.so [root@web1 ~]# echo"extension = redis.so" >>/application/php/lib/php.ini [root@web1 ~]# tail -1/application/php/lib/php.ini extension = redis.so 6) 重启php-fpm,并刷新浏览器查看 [root@web1 ~]# /application/php/sbin/php-fpm
补充 php扩展查看被加载:
[root@web1 ~]#/application/php/bin/php -m [PHP Modules] ... memcache redis session ... [ZendModules]
5、安装redis管理工具phpRedisAdmin
1)下载phpredisadmin客户端 [root@web1tools]# git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git Initializedempty Git repository in /application/tools/phpRedisAdmin/.git/ remote:Counting objects: 527, done. remote: Total527 (delta 0), reused 0 (delta 0), pack-reused 527 Receivingobjects: 100% (527/527), 183.89 KiB | 60 KiB/s, done. Resolvingdeltas: 100% (259/259), done. 2) cd到phpRdisAadmin目录,下载predis.git文件 [root@web1tools]# cd phpRedisAdmin [root@web1 phpRedisAdmin]#git clone https://github.com/nrk/predis.git vendor ============================================================================================================ 查看一下README.markdown说明 [root@web1 phpRedisAdmin]#cat README.markdown [root@web1 phpRedisAdmin]#git submodule init [root@web1 phpRedisAdmin]#git submodule update 3)把phpRedisAdmin目录移到站点目录下【/data/bbs/目录内】 [root@web1tools]# mv phpRedisAdmin /data/bbs/ 4)修改redis客户端的config配置文件 [root@web1 ~]#vim /data/bbs/phpRedisAdmin/includes/config.sample.inc.php <?php //Copy this file to config.inc.php and make changes to that file tocustomize your configuration. $config = array( ‘servers‘ => array( array( ‘name‘ => ‘172.16.1.30‘, // Optional name. ‘host‘ => ‘172.16.1.30‘, // redis服务器的主机IP ‘port‘ => 6379, ‘filter‘ => ‘*‘, ), ... 下面全部省略 ... 5)修改完后,浏览器访问
测试:
a)在redis服务器上,创建一个key如下:
[root@cache~]# redis-cli
127.0.0.1:6379>set t1 oldboy01
OK
127.0.0.1:6379>get t1
"oldboy01"
客户端查看信息:
b)在浏览器添加key t2 value liubaolong
tedis服务端查看信息: [root@cache~]# redis-cli 127.0.0.1:6379>get t2 "liubaolong"
本文出自 “靠谱儿” 博客,请务必保留此出处http://liubao0312.blog.51cto.com/2213529/1677415
原文地址:http://liubao0312.blog.51cto.com/2213529/1677415