它的大概框架有點像這樣: php
主機環境 rhel6.5 selinx and iptales disabled
# tar zxf redis-3.0.2.tar.gz
# yum install -y gcc
# cd redis-3.0.2
make
make install
安裝完後會在/usr/local/bin下產生以下文件
# ls /usr/local/bin/
redis-benchmark redis-check-dump redis-sentinel
redis-check-aof redis-cli redis-server
這些可執行文件的做用以下:
redis-server: Redis 服務主程序。
redis-cli: Redis 客戶端命令行工具,也能夠用 telnet 來操做。
redis-benchmark: Redis 性能測試工具,用於測試讀寫性能。
redis-check-aof:檢查 redis aof 文件完整性,aof 文件持久化記錄服務器執行的全部寫操做命令,用於還原數據。
redis-check-dump:檢查 redis rdb 文件的完整性,rdb 就是快照存儲,即按照必定的策略週期性的將數據保存到磁盤,是默認的持久化方式。
redis-sentinel: redis-sentinel 是集羣管理工具,主要負責主從切換。
2. 配置並啓動服務
# /root/redis/redis-3.0.2/utils/install_server.sh
redis服務配置及安裝,能夠直接回車,採起默認方式
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
查看網絡狀態會發現已經自動開啓了redis的6379端口
# netstat -antulp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 4197/redis-server *
tcp 0 0 :::6379 :::* LISTEN 4197/redis-server *
# ls /etc/redis/
6379.conf #這個就是它的配置文件
Redis 客戶端使用:
127.0.0.1:6379> config get *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
當須要作redis擴展時,咱們能夠這樣作
# vim /etc/redis/6379.conf
# slaveof <masterip> <masterport>
slaveof masterip 6379
便可實現redis的高可用
Redis 做 mysql 的緩存服務器
1. 安裝 lnmp 環境,安裝如下軟件包:
這一步可能會出現版本不兼容問題,建議安裝最新穩定版rpm包,若是曾經安裝過能夠進行更新
# yum localinstall nginx-1.8.0-1.el6.ngx.x86_64.rpm
php-5.3.3-38.el6.x86_64.rpm
php-cli-5.3.3-38.el6.x86_64.rpm
php-common-5.3.3-38.el6.x86_64.rpm
php-devel-5.3.3-38.el6.x86_64.rpm
php-fpm-5.3.3-38.el6.x86_64.rpm
php-gd-5.3.3-38.el6.x86_64.rpm
php-mbstring-5.3.3-38.el6.x86_64.rpm
php-mysql-5.3.3-38.el6.x86_64.rpm
php-pdo-5.3.3-38.el6.x86_64.rpm
# yum install mysql-server -y
2. 簡單配置 nginx
# vim /etc/nginx/conf.d/default.conf
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
# nginx -t #檢測是否出現語法問題
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# service nginx start
3:php-fpm簡單配置
# vim /etc/php-fpm.d/www.conf
修改其用戶和用戶組爲nginx
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
# vim /etc/php.ini #修改其時區爲亞洲上海
date.timezone = Asia/Shanghai
# service php-fpm start
# vim /usr/share/nginx/html/index.php
<?php
phpinfo()
?>
# nginx -s reload
4:安裝 php 的 redis 擴展
# unzip phpredis-master.zip
# cd phpredis-master
[root@server1 phpredis-master]# phpize
# ./configure --with-php-config=/usr/bin/php-config
# make && make install
; Enable mysql extension module
# cp mysql.ini redis.ini
# vim redis.ini
extension=redis.so
# vim /etc/php.ini
extension=redis.so #加載 redis 模塊
5.配置 mysql
mysql> grant all on test.* to redis@localhost identified by 'redis';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
編輯測試sql語句腳本
# vim test.sql
use test;
CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');
# mysql < test.sql # 導入mysql數據庫
# mysql #查看導入數據
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
mysql> select * from test;
+----+-------+
| id | name |
+----+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
| 5 | test5 |
| 6 | test6 |
| 7 | test7 |
| 8 | test8 |
| 9 | test9 |
+----+-------+
9 rows in set (0.00 sec)
mysql> desc test;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(7) | NO | PRI | NULL | auto_increment |
| name | char(8) | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
6. 建立 php 測試頁面
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379) or die ("could net connect redis server");
# $query = "select * from test limit 9";
$query = "select * from test";
for ($key = 1; $key < 10; $key++)
{
if (!$redis->get($key))
{
$connect = mysql_connect('127.0.0.1','redis','westos');
mysql_select_db(test);
$result = mysql_query($query);
//若是沒有找到$key,就將該查詢sql的結果緩存到redis
while ($row = mysql_fetch_assoc($result))
{
$redis->set($row['id'],$row['name']);
}
$myserver = 'mysql';
break;
}
else
{
$myserver = "redis";
$data[$key] = $redis->get($key);
}
}
echo $myserver;
echo "<br>";
for ($key = 1; $key < 10; $key++)
{
echo "number is <b><font color=#FF0000>$key</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
echo "<br>";
}
?>
# cp test.php /usr/share/nginx/html/
測試結果:
第一次揮發現其數據來自mysql,當再次刷新後其數據即是來自redis存儲了
到這裏,咱們已經實現了 redis 做爲 mysql 的緩存服務器,可是若是更新了 mysql,redis中仍然會有對應的 KEY,數據就不會更新,此時就會出現 mysql 和 redis 數據不一致的情
況。因此接下來就要經過 mysql 觸發器將改變的數據同步到 redis 中
配置 gearman 實現數據同步
Gearman 是一個支持分佈式的任務分發框架:
html