日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
CentOS6.5下Redis安裝部署配置指南

CentOS6.5下redis安裝部署配置指南、常用命令、主從同步集群、redis-php學(xué)習(xí)資料整合詳解。

1、Redis的介紹、安裝、配置、啟動(dòng)流程
1.1、Redis 特征簡介
Redis是Remote Dictionary Server的縮寫。他本質(zhì)上一個(gè)Key/Value數(shù)據(jù)庫,與Memcached類似的NoSQL型數(shù)據(jù)庫,但是他的數(shù)據(jù)可以持久化的保存在磁盤上,解決了服務(wù)重啟后數(shù)據(jù)不丟失的問題,他的值可以是string(字符串)、list(列表)、sets(集合)或者是ordered sets(被排序的集合),所有的數(shù)據(jù)類型都具有push/pop、add/remove、執(zhí)行服務(wù)端的并集、交集、兩個(gè)sets集中的差別等等操作,這些操作都是具有原子性的,Redis還支持各種不同的排序能力。

Redis 支持絕大部分主流的開發(fā)語言, 如:  PHP、 Java、 C#、 Perl、 Python、 Ruby 等等
通常,Redis將數(shù)據(jù)存儲(chǔ)于內(nèi)存中,或被配置為使用虛擬內(nèi)存。通過兩種方式可以實(shí)現(xiàn)數(shù)據(jù)持久化:使用截圖的方式,將內(nèi)存中的數(shù)據(jù)不斷寫入磁盤;或使用類似 MySQL 的日志方式,記錄每次更新的日志。前者性能較高,但是可能會(huì)引起一定程度的數(shù)據(jù)丟失;后者相反。

Redis 支持將數(shù)據(jù)同步到多臺(tái)從庫上,這種特性對提高讀取性能非常有(在物理機(jī)真實(shí)環(huán)境中每秒高并發(fā)讀取速度能達(dá)到十萬多次)。

1.2、redis下載
[root@mysqldb1 ~]# wget http://download.redis.io/releases/redis-3.0.5.tar.gz

1.3、解壓
[root@mysqldb1 ~]# tar xf redis-3.0.5.tar.gz

這樣就在當(dāng)前目錄下新建了一個(gè)包含發(fā)行版源代碼的目錄,必須cd進(jìn)入這個(gè)目錄以繼續(xù)服務(wù)器的編譯。

1.4、編譯及安裝

進(jìn)入redis解壓目錄,執(zhí)行如下命令編譯Redis:
[root@mysqldb1 ~]# cd redis-3.0.5
[root@mysqldb1 redis-3.0.5]# make && make install

也可以指定目錄安裝:
make prefix=/path/to/installdir install
安裝tcmalloc包需指定參數(shù),如make USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes
因?yàn)閷σ粋€(gè)基本的配置的編譯,一般需要1分鐘左右的時(shí)間,實(shí)際需要的時(shí)間因你的硬件和選擇的模塊數(shù)量會(huì)有很大不同。

1.5、配置

接著,復(fù)制redis.conf到/etc/下,修改配置文件,來配置Redis服務(wù)器。
1 [root@mysqldb1 redis-3.0.5]# cp redis.conf /etc/

1.6、參數(shù)參看
1234567891011121314 [root@mysqldb1 redis-3.0.5]# redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
  ./redis-server - (read config from stdin)
  ./redis-server -v or --version
  ./redis-server -h or --help
  ./redis-server --test-memory
Examples:
  ./redis-server (run the server with default conf)
  ./redis-server /etc/redis/6379.conf
  ./redis-server --port 7777
  ./redis-server --port 7777 --slaveof 127.0.0.1 8888
  ./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
  ./redis-server /etc/sentinel.conf --sentinel

1.7、版本參看 
[root@mysqldb1 redis-3.0.5]# redis-server -v
Redis server v=3.0.5 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=ee8d4e51452e5879

1.8、啟動(dòng)Redis服務(wù)器
[root@mysqldb1 redis-3.0.5]# redis-server /etc/redis.conf

注:此命令僅有一個(gè)啟動(dòng)參數(shù),指定/path/to/redis.conf目錄下的配置文件,不加參數(shù)執(zhí)行默認(rèn)配置。
[root@mysqldb1 ~]# redis-cli ping 
PONG

測試啟動(dòng) redis-cli ping 返回PONG,啟動(dòng)成功。
[root@mysqldb1 ~]# netstat -tulnp | grep 6379
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                  LISTEN      11731/redis-server 
tcp        0      0 :::6379                    :::*                        LISTEN      11731/redis-server

 

1.9、停止Redis

關(guān)閉服務(wù)

[root@mysqldb1 ~]# redis-cli shutdown
[root@mysqldb1 ~]# netstat -tulnp | grep 6379
[root@mysqldb1 ~]# redis-cli ping 
Could not connect to Redis at 127.0.0.1:6379: Connection refused

注:可指定端口:redis-cli -p shutdown

1.10、連接Redis
兩種鏈接redis的方法:
方法一、
[root@mysqldb1 ~]# redis-cli      #也可以指定ip,端口號(hào)啟動(dòng)redis(redis-cli -h 192.168.1.2 -p 6379)
127.0.0.1:6379> 
127.0.0.1:6379> quit

方法二、
[root@mysqldb1 ~]# telnet 192.168.1.2 6379
Trying 192.168.1.2...
Connected to 192.168.1.2.
Escape character is '^]'.
quit
+OK
Connection closed by foreign host

.

2、redis常用命令詳解
2.1、redis編譯安裝命令查看
[root@mysqldb1 redis-3.0.5]#cd /usr/local/bin
[root@mysqldb1 bin]# ll | grep redis
-rwxr-xr-x. 1 root root 4587299 Nov  2 01:26 redis-benchmark
-rwxr-xr-x. 1 root root  22177 Nov  2 01:26 redis-check-aof
-rwxr-xr-x. 1 root root  45387 Nov  2 01:26 redis-check-dump
-rwxr-xr-x. 1 root root 4691450 Nov  2 01:26 redis-cli
lrwxrwxrwx. 1 root root      12 Nov  2 01:26 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6464789 Nov  2 01:26 redis-server

2.2、redis-benchmark是Redis性能測試工具,測試Redis在你的系統(tǒng)及你的配置下的讀寫性能,redis的基準(zhǔn)信息和性能檢測。
redis-benchmark參數(shù):
[root@mysqldb1 ~]# redis-benchmark --help
Usage: redis-benchmark [-h ] [-p ] [-c ] [-n [-k ]
 -h       Server hostname (default 127.0.0.1)
設(shè)置檢測主機(jī)IP地址,默認(rèn)為127.0.0.1
 -p           Server port (default 6379)
設(shè)置檢測主機(jī)的端口號(hào),默認(rèn)為6379
 -s         Server socket (overrides host and port)
服務(wù)器套接字(壓到主機(jī)和端口)
 -a       Password for Redis Auth
 -c       Number of parallel connections (default 50)
客戶端并發(fā)連接數(shù)
 -n       Total number of requests (default 100000)
客戶端總共發(fā)出的請求數(shù)
 -d           Data size of SET/GET value in bytes (default 2)
測試使用的數(shù)據(jù)集的大小/字節(jié)的值(默認(rèn)2字節(jié))
 -dbnum         SELECT the specified db number (default 0)
 -k       1=keep alive 0=reconnect (default 1)
1:表示保持連接(默認(rèn)值)0:重新連接
 -r   Use random keys for SET/GET/INCR, random values for SADD
  Using this option the benchmark will expand the string __rand_int__
  inside an argument with a 12 digits number in the specified range
  from 0 to keyspacelen-1. The substitution changes every time a command
  is executed. Default tests use this to hit random keys in the
  specified range.
SET/GET/INCR方法使用隨機(jī)數(shù)插入數(shù)值,如果設(shè)置為100則插入值為rand:000000000000 - rand:000000000099
 -P         Pipeline requests. Default 1 (no pipeline).
默認(rèn)為1(無管道),當(dāng)網(wǎng)絡(luò)延遲過長時(shí),使用管道方式通信(請求和響應(yīng)打包發(fā)送接收)
 -q                Quiet. Just show query/sec values
簡約信息模式,只顯示查詢和秒值等基本信息。
 --csv              Output in CSV format
以CSV格式輸出信息
 -l                Loop. Run the tests forever
無線循環(huán)插入測試數(shù)據(jù),ctrl+c停止
 -t         Only run the comma separated list of tests. The test
names are the same as the ones produced as output.
只運(yùn)行測試逗號(hào)分隔的列表命令,如:-t ping,set,get
 -I                Idle mode. Just open N idle connections and wait.
空閑模式。立即打開50個(gè)空閑連接和等待。
Examples:
 Run the benchmark with the default configuration against 127.0.0.1:6379:
  $ redis-benchmark
 Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
  $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20
 Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
  $ redis-benchmark -t set -n 1000000 -r 100000000
 Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
  $ redis-benchmark -t ping,set,get -n 100000 --csv
 Benchmark a specific command line:
  $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0
 Fill a list with 10000 random elements:
  $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__
 On user specified command lines __rand_int__ is replaced with a random integer
 with a range of values selected by the -r option.

2.3、redis-cli 的使用說明
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 root@mysqldb1 ~]# redis-cli --help
redis-cli 3.0.5
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h       Server hostname (default: 127.0.0.1).
  -p           Server port (default: 6379).
  -s         Server socket (overrides hostname and port).
  -a       Password to use when connecting to the server.
  -r         Execute specified command N times.
  -i       When -r is used, waits seconds per command.
 It is possible to specify sub-second times like -i 0.1.
  -n             Database number.
  -x                Read last argument from STDIN.
  -d     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
 not a tty).
  --no-raw          Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat            Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
 Default time interval is 15 sec. Change it using -i.
  --latency-dist    Shows latency as a spectrum, requires xterm 256 colors.
 Default time interval is 1 sec. Change it using -i.
  --lru-test   Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb   Transfer an RDB dump from remote server to local file.
  --pipe            Transfer raw Redis protocol from stdin to server.
  --pipe-timeout In --pipe mode, abort with error if after sending all data.
 no reply is received within seconds.
 Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan            List all keys using the SCAN command.
  --pattern     Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency Run a test to measure intrinsic system latency.
 The test will run for the specified amount of seconds.
  --eval       Send an EVAL command using the Lua script at .
  --help            Output this help and exit.
  --version          Output version and exit.
Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'
  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.

2.4、redis-check-aof

更新日志檢查 ,加--fix參數(shù)為修復(fù)log文件
redis-check-aof appendonly.aof

2.5、 redis-check-dump

檢查本地?cái)?shù)據(jù)庫文件
redis-check-dump  dump.rdb

2.6、獲取服務(wù)器的信息和統(tǒng)計(jì)
[root@mysqldb1 bin]# redis-cli -p 6379 info 
# Server
redis_version:3.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:ee8d4e51452e5879
redis_mode:standalone
os:Linux 2.6.32-431.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:11760
run_id:51f2b7336fae3bf3e1e4a21d76aa71b02f1e9608
tcp_port:6379
uptime_in_seconds:10033
uptime_in_days:0
hz:10
lru_clock:3570034
config_file:/etc/redis.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:188575024
used_memory_human:179.84M
used_memory_rss:241483776
used_memory_peak:228770552
used_memory_peak_human:218.17M
used_memory_lua:36864
mem_fragmentation_ratio:1.28
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1446405599
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:2
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:71398
total_commands_processed:22036268
instantaneous_ops_per_sec:0
total_net_input_bytes:820805789
total_net_output_bytes:15461673777
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:6295156
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:3258
migrate_cached_sockets:0
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:388.55
used_cpu_user:250.86
used_cpu_sys_children:1.06
used_cpu_user_children:3.15
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=994967,expires=0,avg_ttl=0

3、redis主從模式實(shí)戰(zhàn)

3.1、Redis的主從配置簡介:   
在Redis中配置Master-Slave模式其實(shí)非常的簡單。相信在閱讀完下面的文章您可以輕松愉快做到完成redis的Master-Slave同步。這里我先給大家列出一些理論性的知識(shí),后面給出實(shí)際操作的案例。
下面的列表清楚的解釋了Redis Replication的特點(diǎn)和優(yōu)勢。
1、同一個(gè)Master可以同步多個(gè)Slaves。    2、Slave同樣可以接受其它Slaves的連接和同步請求,這樣可以有效的分載Master的同步壓力。因此我們可以將Redis的Replication架構(gòu)視為圖結(jié)構(gòu)。
3、Master Server是以非阻塞的方式為Slaves提供服務(wù)。所以在Master-Slave同步期間,客戶端仍然可以提交查詢或修改請求。
4、Slave Server同樣是以非阻塞的方式完成數(shù)據(jù)同步。在同步期間,如果有客戶端提交查詢請求,Redis則返回同步之前的數(shù)據(jù)。5、為了分擔(dān)Master的讀操作壓力,Slave服務(wù)器可以為客戶端提供只讀操作的服務(wù),寫服務(wù)仍然必須由Master來完成。即便如此,系統(tǒng)的伸縮性還是得到了很大的提高。
6、Master可以將數(shù)據(jù)保存操作交給Slaves完成,從而避免了在Master中要有獨(dú)立的進(jìn)程來完成此操作。

3.2、redis主從的工作原理: 
在Slave啟動(dòng)并連接到Master之后,它將主動(dòng)發(fā)送一個(gè)SYNC命令。此后Master將啟動(dòng)后臺(tái)存盤進(jìn)程,同時(shí)收集所有接收到的用于修改數(shù)據(jù)集的命令,在后臺(tái)進(jìn)程執(zhí)行完畢后,Master將傳送整個(gè)數(shù)據(jù)庫文件到Slave,以完成一次完全同步。而Slave服務(wù)器在接收到數(shù)據(jù)庫文件數(shù)據(jù)之后將其存盤并加載到內(nèi)存中。此后,Master繼續(xù)將所有已經(jīng)收集到的修改命令,和新的修改命令依次傳送給Slaves,Slave將在本次執(zhí)行這些數(shù)據(jù)修改命令,從而達(dá)到最終的數(shù)據(jù)同步。
如果Master和Slave之間的鏈接出現(xiàn)斷連現(xiàn)象,Slave可以自動(dòng)重連Master,但是在連接成功之后,一次完全同步將被自動(dòng)執(zhí)行。
 
3.3、配置步驟說明:
Redis 的 master/slave 數(shù)據(jù)復(fù)制方式可以是一主一從或者是一主多從的方式,Redis 在master 是非阻塞模式,也就是說在 slave 執(zhí)行數(shù)據(jù)同步的時(shí)候,master 是可以接受客戶端的請求的,并不影響同步數(shù)據(jù)的一致性,然而在 slave 端是阻塞模式的,slave 在同步 master數(shù)據(jù)時(shí),并不能夠響應(yīng)客戶端的查詢Redis 的 master/slave 模式下,master 提供數(shù)據(jù)讀寫服務(wù),而 slave 只提供讀服務(wù)Redis的 master/slave 的配置方式是在 slave 主機(jī)的 Redis目錄下的 redis.conf 配置文件中
配置方式可以有2種:
1234567 1、一主一從 
    master -> slave
2、一主多從
    master -> slave -> slave -> slave
                ├--> slave -> slave
                ├─-> slave -> slave
                └─-> slave -> slave

一個(gè)集群可以包含最多4096個(gè)節(jié)點(diǎn)(主節(jié)點(diǎn)master和從節(jié)點(diǎn)slave),建議最多設(shè)置幾百個(gè)節(jié)點(diǎn),其配置特簡單,就是把從服務(wù)器的redis.conf配置文件中的slaveof參數(shù)指向主服務(wù)器的ip 及 端口。

3.4、創(chuàng)建主從實(shí)現(xiàn)案例(本案例是通過一主機(jī)多端口的方式實(shí)現(xiàn)的):

3.4.1、創(chuàng)建redis主從目錄
[root@mysqldb1 redis]# mkdir /redis/{master_6379,slave_6380}/{conf,log,data} -p
[root@mysqldb1 redis]# ls
master_6379  slave_6380

3.4.2、分別修改redis主從服務(wù)器的配置文件
[root@mysqldb1 redis-3.0.5]# cp /root/redis-3.0.5/redis.conf master_6379/conf/master.conf
[root@mysqldb1 redis-3.0.5]# cp /root/redis-3.0.5/redis.conf slave_6380/conf/slave.conf
[root@mysqldb1 redis]# vim master_6379/conf/master.conf#修改主服務(wù)器的配置文件
 47 pidfile /redis/master_6379/redis_master.pid
 51 port 
110 logfile "/redis/master_6379/log/master.log"
193 dir /redis/master_6379/data/
515 appendfilename "appendonly.aof"
[root@mysqldb1 redis]# vim /redis/slave_6380/conf/slave.conf #修改從服務(wù)器的配置文件
 46 pidfile /redis/slave_6380/redis_slave.pid
 50 port 6380
109 logfile "/redis/slave_6380/log/slave.log"
192 dir /redis/slave_6380/data/
212 slaveof 127.0.0.1 6379
514 appendfilename "appendonly.aof"

3.4.3、分別啟動(dòng)主、從服務(wù)器
[root@mysqldb1 master_6379]# redis-server /redis/master_6379/conf/master.conf
[root@mysqldb1 slave_6380]# redis-server /redis/slave_6380/conf/slave.conf

3.4.4、分別查看主、從服務(wù)器的日志文件
[root@mysqldb1 redis]# cat /redis/master_6379/log/master.log  #主服務(wù)器的日志文件
...  ...
14363:M 02 Nov 12:42:57.805 * DB loaded from disk: 0.000 seconds
14363:M 02 Nov 12:42:57.805 * The server is now ready to accept connections on port 6379#主服務(wù)器的啟動(dòng)端口
14363:M 02 Nov 12:43:36.361 * Slave 127.0.0.1:6380 asks for synchronization#要求同步的從服務(wù)器信息
14363:M 02 Nov 12:43:36.361 * Full resync requested by slave 127.0.0.1:6380
14363:M 02 Nov 12:43:36.361 * Starting BGSAVE for SYNC with target: disk
14363:M 02 Nov 12:43:36.496 * Background saving started by pid 14371
14371:C 02 Nov 12:43:36.528 * DB saved on disk
14371:C 02 Nov 12:43:36.528 * RDB: 4 MB of memory used by copy-on-write
14363:M 02 Nov 12:43:36.597 * Background saving terminated with success
14363:M 02 Nov 12:43:36.597 * Synchronization with slave 127.0.0.1:6380 succeeded
[root@mysqldb1 redis]# cat /redis/slave_6380/log/slave.log #從服務(wù)器的日志文件
...  ...
14368:S 02 Nov 12:43:35.424 * DB loaded from disk: 0.000 seconds
14368:S 02 Nov 12:43:35.424 * The server is now ready to accept connections on port 6380#從服務(wù)器的啟動(dòng)端口
14368:S 02 Nov 12:43:36.360 * Connecting to MASTER 127.0.0.1:6379#正在連接主服務(wù)器
14368:S 02 Nov 12:43:36.361 * MASTER <-> SLAVE sync started#主從同步已經(jīng)開始
14368:S 02 Nov 12:43:36.361 * Non blocking connect for SYNC fired the event.
14368:S 02 Nov 12:43:36.361 * Master replied to PING, replication can continue...
14368:S 02 Nov 12:43:36.361 * Partial resynchronization not possible (no cached master)
14368:S 02 Nov 12:43:36.497 * Full resync from master: dd7a9d178eb3434494fecd4c97cc05e8d6bc1a69:1
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: receiving 55 bytes from master
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Flushing old data
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Loading DB in memory
14368:S 02 Nov 12:43:36.598 * MASTER <-> SLAVE sync: Finished with success

3.4.5、查看redis主、從數(shù)據(jù)文件md5指紋信息
[root@mysqldb1 redis]# find /redis/ -name *.rdb | xargs md5sum
81646a7364950775039f694b1ddd6c8a  /redis/slave_6380/data/slave_dump.rdb
81646a7364950775039f694b1ddd6c8a  /redis/master_6379/data/master_dump.rdb

通過指紋信息可以得到redis主、從服務(wù)器的數(shù)據(jù)是一致的

3.4.6、向主服務(wù)器添加數(shù)據(jù)
[root@mysqldb1 ~]# redis-cli -p 6379
127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> set key2 liangge
OK
127.0.0.1:6379> set key3 OK
OK
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key3"
127.0.0.1:6379> quit

3.4.7、在從服務(wù)器上查看數(shù)據(jù)信息是否已同步
[root@mysqldb1 ~]# redis-cli -p 6380
127.0.0.1:6380> get key1
"hello"
127.0.0.1:6380> get key2
"liangge"
127.0.0.1:6380> get key3
"OK"
127.0.0.1:6380> keys *
1) "key1"
2) "key2"
3) "key3"
127.0.0.1:6380> quit

3.4.8、再次查看主、從服務(wù)器的md5數(shù)據(jù)指紋
[root@mysqldb1 ~]# find /redis/ -name *.rdb |xargs md5sum
81646a7364950775039f694b1ddd6c8a  /redis/slave_6380/data/slave_dump.rdb
81646a7364950775039f694b1ddd6c8a  /redis/master_6379/data/master_dump.rdb

3.4.9、查看redis主從服務(wù)器工作目錄
[root@mysqldb1 redis]# tree /redis
/redis
├── master_6379
│  ├── conf
│  │  └── master.conf
│  ├── data
│  │  └── master_dump.rdb
│  └── log
│      └── master.log
└── slave_6380
├── conf
│  └── slave.conf
├── data
│  └── slave_dump.rdb
└── log
└── slave.log
8 directories, 6 files

4、PHP中安裝redis插件

4.1、下載php_redis擴(kuò)展模塊
phpredis下載地址: https://codeload.github.com/owlient/phpredis/zip/master

4.2、安裝php_redis擴(kuò)展模塊
[root@LNMP ~]# unzip phpredis-master.zip 
[root@LNMP ~]# cd phpredis-master
[root@LNMP phpredis-master]# /usr/local/php5.5.30/bin/phpize
[root@LNMP phpredis-master]# ./configure --with-php-config=/usr/local/php5.5.30/bin/php-config 
[root@LNMP phpredis-master]# make && make install
[root@LNMP phpredis-master]# ll /usr/local/php5.5.30/lib/php/extensions/no-debug-non-zts-20121212/
total 1668
-rwxr-xr-x. 1 root root 465457 Nov  2 06:21 memcache.so
-rwxr-xr-x. 1 root root 303946 Oct 14 04:24 opcache.a
-rwxr-xr-x. 1 root root 210011 Oct 14 04:24 opcache.so
-rwxr-xr-x. 1 root root 717996 Nov  2 18:35 redis.so
#說明php_redis模塊已經(jīng)安裝成功

4.3、在php.php文件中配置php_redis模塊
12345 [root@LNMP phpredis-master]# vim /usr/local/php5.5.30/lib/php.ini 
extension_dir="/usr/local/php5.5.30/lib/php/extensions/no-debug-non-zts-20121212/"
extension="memcache.so"
extension="redis.so"
#如果以前配置過php擴(kuò)展模塊現(xiàn)在只需要加上這行即可。

4.4、查看php是否啟動(dòng),如果啟動(dòng)則重啟,如若沒有啟動(dòng)則直接啟動(dòng)。
[root@LNMP phpredis-master]# netstat -tulnp | egrep "php|nginx"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      9922/nginx         
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                  LISTEN      13361/php-fpm 
[root@LNMP phpredis-master]# pkill php
[root@LNMP phpredis-master]# netstat -tulnp | egrep "php|nginx"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      9922/nginx   
[root@LNMP phpredis-master]# /usr/local/php5.5.30/sbin/php-fpm -t
[02-Nov-2015 18:53:33] NOTICE: configuration file /usr/local/php5.5.30/etc/php-fpm.conf test is successful
[root@LNMP phpredis-master]# /usr/local/php5.5.30/sbin/php-fpm -c /usr/local/php5.5.30/lib/php.ini 
[root@LNMP phpredis-master]# netstat -tulnp | egrep "php|nginx"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      9922/nginx         
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                  LISTEN      13400/php-fpm

4.5、在瀏覽器查看php_redis模塊是否成功加載,本機(jī)是用linux命令行工具查看
[root@LNMP phpredis-master]# curl localhost/phpinfo.php|grep redis
  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
 Dload  Upload  Total  Spent    Left  Speed
  0    0    0    0    0    0      0      0 --:--:-- --:--:-- --:--:--    0

redis


Registered save handlers files user memcache redis 
100 67295    0 67295    0    0  4433k      0 --:--:-- --:--:-- --:--:-- 6571k
This program is free software; you can redistribute it and/or modify it under the terms of the PHP License as published by the PHP Group and included in the distribution in the file:  LICENSE

5、編寫php程序測試php_redis模塊
5.1、php連接redis的程序代碼
$redis = new Redis();
$redis->connect('192.168.1.2',6379) or die("Could not connect redis");
$redis->set('mykey1','liangge');
echo "after insert get mykey1:  ".$redis->get('mykey1');
$redis->delete('mykey1');
echo "
after delete get mykey1:    ".$redis->get('mykey1');
?>

5.2、調(diào)試輸出結(jié)果
after insert get mykey1: liangge
after delete get mykey1:


網(wǎng)頁題目:CentOS6.5下Redis安裝部署配置指南
網(wǎng)頁地址:http://m.5511xx.com/article/dpsjjhc.html