主机分布
172.25.33.1 httpd+php
172.25.33.2 redis
172.25.33.3 Mariadb
172.25.33.1安装httpd php php-mysql
yum -y install httpd php php-mysql
获取测试文件test.php
connect('172.25.33.2',6379) or die ("could net connect redis server"); #redis主机IP
# $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.33.3','redis','redhat'); # 连接数据库
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 "
";
}
?>
添加redis模块
查看php的版本
上网下载安装同版本php-devel
yum install php-devel-5.4.16-42.el7.x86_64.rpm
获取phpredis-master压缩包并解压
进入目录phpredis-master,使用phpize生成configure文件
phpize
./configure
make && make install
进入目录/usr/lib64/php/modules/下,查看编译后存在的模块文件
进入/etc/php.d下
cp mysql.ini redis.ini
修改redis.ini
修改/etc/php.ini
重启httpd查看已经加载的模块
172.24.33.2安装redis
修改配置文件/etc/redis/6379.conf
重启redis
/etc/init.d/redis_6379 restart
172.25.33.3安装mariadb-server
yum install mariadb-server -y
systemctl start mariadb
导入测试用数据库文件
mysql < 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');
grant all on test.* to redis@'%' identified by 'redhat';
此时尝试对数据库进行更新
update test name='redhat' where id='1';
解决问题:
数据库端:(172.25.33.3)
配置添加用户自定义函数(可以将mysql关系型数据转换为json格式的UDF工具)
获取压缩包并解压
lib_mysqludf_json-master.zip
进入该目录下,编译
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
拷贝新生成的模块至mysql的模块插件目录下
cp lib_mysqludf_json-master/lib_mysqludf_json.so /usr/lib64/mysql/plugin/
show global variables like 'plugin_dir';
CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
查询
select * from mysql.func;
yum install -y \
gearmand-1.1.12-18.el7.x86_64.rpm \
libevent-devel-2.0.21-4.el7.x86_64.rpm \
libgearman-1.1.12-18.el7.x86_64.rpm \
libgearman-devel-1.1.12-18.el7.x86_64.rpm
编译安装mysql的mysqludf的json扩展
进入该目录
./configure --with-mysql --libdir=/usr/lib64/mysql/plugin
make && make install
CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so';
CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';
SELECT gman_servers_set('172.25.33.1:4730');
mysql
内容如下
[root@server3 ~]# cat 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');
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 ;
登录数据库查看触发器
show triggers from test;
./configure && make && make install
进入 /etc/php.d/
新建文件gearman.ini
; Enable mysql extension module
extension=gearman.so
yum install -y \
gearmand-1.1.12-18.el7.x86_64.rpm \
libevent-devel-2.0.21-4.el7.x86_64.rpm \
libgearman-1.1.12-18.el7.x86_64.rpm \
libgearman-devel-1.1.12-18.el7.x86_64.rpm
systemctl start gearmand
systemctl enable gearmand
systemctl restart httpd
编写worker.php文件放入/usr/local/下
[root@server1 php.d]# cat /usr/local/worker.php
addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('172.25.33.2', 6379); # redis主机的IP
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);
}
?>
后台运行worker.php
nohup php /usr/local/worker.php &
在mysql上尝试更新数据
update test set name='234' where id='4';