redis的優點:支持主從備份,操作指令豐富,支持異步的數據持久化
將 redis 安裝到 /usr/local/webserver/redis
1.下載安裝包
wget http://redis.googlecode.com/files/redis-2.2.15.tar.gz
tar zxvf redis-2.2.15.tar.gz
cd redis-2.2.15
make
mkdir -p /usr/local/webserver/redis/conf
mkdir -p /usr/local/webserver/redis/run
mkdir -p /usr/local/webserver/redis/db
cp redis.conf /usr/local/webserver/redis/conf/
cd src
2.將 src 目錄下所有可執行文件復制到安裝目錄
cp redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server mkreleasehdr.sh
/usr/local/webserver/redis/
3.修改配置文件
vi /usr/local/webserver/redis/conf/redis.conf
daemonize yes --yes啟用守護進程
pidfile /usr/local/webserver/redis/run/redis.pid --Redis以守護進程方式運行時把pid寫入文件
dir /usr/local/webserver/redis/db --數據存放的目錄
4.創建啟動和停止服務腳本
vi /usr/local/webserver/redis/start.sh
#!/bin/bash
/usr/local/webserver/redis/redis-server /usr/local/webserver/redis/conf/redis.conf
vi /usr/local/webserver/redis/stop.sh
#!/bin/bash
kill `cat /usr/local/webserver/redis/run/redis.pid`
chmod a+x /usr/local/webserver/redis/start.sh /usr/local/webserver/redis/stop.sh
5.啟動 redis 服務
/usr/local/webserver/redis/start.sh
驗證證服務是否成功:
netstat -nlpt | grep 6379
6.啟動客戶端驗證
/usr/local/webserver/redis/redis-cli
>set key1 val1
>get key1
二. M/S主從集群配置
1台Master和一台Slave
Master IP:127.0.0.1 PORT:6379
Slave1 IP:127.0.0.1 PORT:63791
2.復制slave目錄,方便管理
cp -r /usr/local/webserver/redis /usr/local/webserver/redis-slave1
3.修改slave的配置文件和服務腳本
vi /usr/local/webserver/redis-slave1/conf/redis.conf --
port 63791
pidfile /usr/local/webserver/redis-slave1/run/redis.pid
dir /usr/local/webserver/redis-slave1/db
slaveof 127.0.0.1 6379
vi /usr/local/webserver/redis-slave1/start.sh
#!/bin/bash
/usr/local/webserver/redis-slave1/redis-server /usr/local/webserver/redis-slave1/conf/redis.conf
vi /usr/local/webserver/redis-slave1/stop.sh
#!/bin/bash
kill `cat /usr/local/webserver/redis-slave1/run/redis.pid`
4.啟動M/S服務
/usr/local/webserver/redis/start.sh
/usr/local/webserver/redis-slave1/start.sh
驗證服務是否正常
netstat -nlpt | grep redis-server
5.驗證redis是否有效
/usr/local/webserver/redis/redis-cli [默認連接端口:6379 的 Master服務]
>set key1 val1
>quit
/usr/local/webserver/redis/redis-cli -p 63791 [連接 Slave1 服務]
>get key1
“val1″ (數據成功同步了)
多個slave同理配置