redis部署手冊

1、redis介紹php

1、redis簡介html

    REmote DIctionary Server(Redis)是一個基於key-value鍵值對的持久化數據庫存儲系統。redis和memcached緩存服務很像,可是redis支持的數據存儲類型更豐富,包括string(字符串)、list(鏈表)、set(集合)和zset(有序集合)等;git

    這些數據類型都支持push/pop、add/remove及取交集、並集和差集及更豐富的操做,並且這些操做都是原子性的;在此基礎上,redis支持各類不一樣方式的排序,與memcached緩存服務同樣,爲了保證效率,數據都是緩存在內存中提供服務;和MC不一樣的是,redis持久化緩存服務還會週期性的把更新的數據寫入到磁盤以及把修改的操做記錄追加到文件裏記錄下來,比MC更有優點的是,redis還支持master-slave(主從)同步,這點很相似關係型數據庫MySQL。github

    redis是一種非關係型數據存儲工具,這區別於傳統的關係型數據庫,相似於memcache,而且其內部集成了對list(鏈表)、set(集合)的操做,能夠很方便快速的處理數據(像插入、刪除list取交集 並集 差集等),這極大的減輕了底層數據庫的壓力,而且給用戶更快的響應速度。web

    redis的出現,在必定程度上彌補了MC這類key-value內存緩存服務的不足,在部分場合能夠對關係數據庫起到很好的補充做用;redis提供了Python,Ruby,Erlang,PHP客戶端,使用很方便;redis官方文檔以下:redis

http://www.redis.io/documentationsql

http://www.redis.cn數據庫

2、redis的優勢vim

  • 性能很高: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,由於寫內存基本上沒有問題;

 

2、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]# le***EADME     -->    查看安裝等信息的詳細手冊
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

wKioL1WwTPLhKpreAAFL_cXfzn0373.jpg

補充 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)修改完後,瀏覽器訪問

wKiom1WwS2XzimICAAH3eWdBQd8296.jpg

測試:

a)在redis服務器上,建立一個key以下:

[root@cache~]# redis-cli

127.0.0.1:6379>set t1 oldboy01

OK

127.0.0.1:6379>get t1

"oldboy01"

客戶端查看信息:

wKiom1WwS4fjg8gmAAHgKgSeHq8176.jpg

b)在瀏覽器添加key t2 value liubaolong

wKiom1WwS7XiR0lFAAH1DKnIhAI636.jpg

tedis服務端查看信息:
[root@cache~]# redis-cli
127.0.0.1:6379>get t2
"liubaolong"
相關文章
相關標籤/搜索