server5 lnmp架构 172.25.11.5
server6 redis 172.25.11.6
server7 mysql端 172.25.11.7
1.安装nginx和php
cd redis/
yum install -y 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
2.配置php
vim /etc/php.ini # 更改它的时区
946 date.timezone =Asia/Shanghai
id nginx # 查看nginx用户是否建立
cd /etc/php-fpm.d/
vim www.conf # 将39行和41行改为nginx
39 user = nginx
40 ; RPM: Keep a group allowed to write in log dir.
41 group = nginx
/etc/init.d/php-fpm start
netstat -antlp
cd /etc/nginx/conf.d/
vim default.conf 编辑配置文件,要更改的行已列出
8 location / {
9 root /usr/share/nginx/html;
10 index index.php index.html index.htm;
11 }
30 location ~ \.php$ {
31 root html;
32 fastcgi_pass 127.0.0.1:9000;
33 fastcgi_index index.php;
34 fastcgi_param SCRIPT_FILENAME //usr/share/nginx/html$fastcgi_scrip t_name;
35 include fastcgi_params;
36 }
nginx -s reload # 重新加载nginx
/etc/init.d/nginx start # 打开nginx
cd /usr/share/nginx/html/
vim index.php # 写一个php测试页面
验证:在浏览器访问看能否看到php页面,172.25.11.5
cd /usr/share/nginx/html/
cd redis/ # 这里面有一个之前写好的test.php
cp test.php /usr/share/nginx/html/index.php # 将其复制到nginx的默认发布目录里
vim /usr/share/nginx/html/index.php
$redis = new Redis();
$redis->connect('172.25.11.6',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('172.25.11.7','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 "
";
for ($key = 1; $key < 10; $key++)
{
echo "number is $key";
echo "
";
echo "name is $data[$key]";
echo "
";
}
?>
cd # 切到root下
yum install unzip -y
unzip phpredis-master.zip
cd phpredis-master
phpize # 创建新的预编译
./configure ##编译
make
make install # 安装
修改php配置:
cd /etc/php.d/
cp mysql.ini redis.ini # 复制一个redis.ini文件出来
vim redis.ini # 编辑redis.ini
更改内容为:
extension=redis.so
/etc/init.d/php-fpm reload
php -m | grep redis # 查看php扩展
cd /root/redis/
scp test.sql server3: # 将写好的test.sql发到server3中,再到3中导入数据库
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');
#DELIMITER $$
#CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
# SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
# END$$
#DELIMITER ;
将reids配置文件里,之前的slaveof 172.25.11.5 6379 行注释掉
让server2的redis成为master,可以用redis-cli登陆,info查看是否变为master
yum install -y mysql-server # 安装系统自带的mysql
/etc/init.d/mysqld start # 打开数据库
[root@server7 ~]# mysql # 登陆
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
mysql> grant all on test.* to redis@'%' identified by 'westos';
# 进行远程登录用户授权
[root@server7 ~]# vim test.sql ##查看下内容
[root@server7 ~]# mysql < test.sql ##导入数据库
[root@server7 ~]# mysql
mysql> use test;
mysql> select * from test; ##发现导入成功
Gearman 是一个支持分布式的任务分发框架:
Gearman Job Server: Gearman 核心程序,
需要编译安装并以守护进程形式运行在后台。
Gearman Client:可以理解为任务的请求者。
Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过
守护进程方式运行,Gearman Worker 接收到 Gearman Client
传递的任务内容后,会按顺序处理。
大致流程:下面要编写的 mysql 触发器,就相当于 Gearman 的客户端。
修改表,插入表就相当于直接下发任务。
然后通过 lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式,
然后在通过 gearman-mysql-udf 插件将任务加入到
Gearman 的任务队列中,
最后通过redis_worker.php,
也就是 Gearman 的 worker 端来完成 redis 数据库的更新。
[root@server5 ~]# cd redis/
[root@server5 redis]#
yum install -y gearmand-1.1.8-2.el6.x86_64.rpm
libgearman-devel-1.1.8-2.el6.x86_64.rpm
libgearman-1.1.8-2.el6.x86_64.rpm
libevent-1.4.13-4.el6.x86_64.rpm
libevent-devel-1.4.13-4.el6.x86_64.rpm
libevent-doc-1.4.13-4.el6.noarch.rpm
libevent-headers-1.4.13-4.el6.noarch.rpm
[root@server5 local]# /etc/init.d/gearmand start
Starting gearmand: [ OK ]
[root@server5 redis]# scp gearman-mysql-udf-0.6.tar.gz server3:
[root@server5 redis]# scp lib_mysqludf_json-master.zip server3:
[root@server5 redis]# scp libgearman-* server3:
[root@server5 redis]# scp libevent-* server3:
[root@server5 redis]# cp worker.php /usr/local/
[root@server5 redis]# tar zxf gearman-1.1.2.tgz
[root@server5 redis]# cd gearman-1.1.2
[root@server5 gearman-1.1.2]# phpize
[root@server5 gearman-1.1.2]# make && make install
[root@server5 gearman-1.1.2]# cd /etc/php.d/
[root@server5 php.d]# cp redis.ini gearman.ini
[root@server5 php.d]# vim gearman.ini
写入: extension=gearman.so
[root@server5 php.d]# /etc/init.d/php-fpm reload # 重新加载php配置
[root@server5 php.d]# php -m | grep gearman # 查看php扩展
gearman
[root@server5 php.d]# cd /usr/local/
[root@server5 local]# vim worker.php
php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('172.25.11.6', 6379);
while($worker->work());
function syncToRedis($job)
{
global $redis;
$workString = $job->workload();
$work = json_decode($workString);
if(!isset($work->id)){
return false;
}
$redis->set($work->id, $work->name);
}
?>
[root@server5 local]# nohup php /usr/local/worker.php &
###在后台运行worker.php
[root@server5 local]# netstat -antlp ###查看端口是否打开
[root@server7 ~]# yum install -y unzip
[root@server7 ~]# unzip lib_mysqludf_json-master.zip
[root@server7 ~]# cd lib_mysqludf_json-master
ls ##会看见一些的文件,最主要要有lib_mysqludf_json.so
yum install -y gcc mysql-devel
要执下面这个命令,首先解决gcc,mysql-devel 这两个软件依赖性
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。
通常,数据库中的数据映
射为 JSON 格式,是通过程序来转换的。
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
拷贝 lib_mysqludf_json.so 模块
cd /usr/lib64/mysql/plugin
[root@server7 plugin]# mysql 登陆数据库
mysql> show global variables like 'plugin_dir';
查看 mysql 的模块目录:
+---------------+-------------------------+
| Variable_name | Value |
+---------------+-------------------------+
| plugin_dir | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
注册 UDF 函数:
mysql> CREATE FUNCTION json_object RETURNS STRING SONAME
-> 'lib_mysqludf_json.so';
cd /root
[root@server7 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
yum install -y libgearman-* libevent-*
[root@server7 ~]# cd gearman-mysql-udf-0.6
./configure --libdir=/usr/lib64/mysql/plugin/
###进行源码编译
make && make install ###安装
[root@server7 ~]# mysql
注册 UDF 函数:
mysql> CREATE FUNCTION gman_do_background RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';
mysql> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';
mysql> select * from mysql.func;
####查看函数
mysql> SELECT gman_servers_set('172.25.11.5:4730');
###指定 gearman 的服务信息
mysql < test.sql
这里test.sql要做点改变,将2,3行注释,5,6,7,8,9注释去掉
mysql> SHOW TRIGGERS FROM test; ###查看触发器
mysql> use test
Database changed
mysql> update test set name='westos'; ###将名字更新为westos