目录前言一、前端搭建1、Nginx部署2、PHP部署安装以及模块安装二、后端部署1.Mysql部署2.Redis作为缓存的部署以及安装三、germand分布式缓存位置部署1、gearman原理2、C端部署3、S端部署总结
安装部署Nginx
yum install unzip openssl-devel pcre-devel GCc make -y
tar -zxf nginx-1.22.0.tar.gz
./configure --with-Http_stub_status_module --with-http_ssl_module --with-file-aio --add-module=/root/nginx-Goodies-nginx-sticky-module-ng-08a395c66e42/
make -j 2 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
vim nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
php安装同时部署相应的兼容模块方式
yum install php php-fpm -y
yum install php-pecl-gearman-1.1.2-1.el7.x86_64.rpm php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm php-pecl-redis-2.2.8-1.el7.x86_64.rpm php-fpm-5.4.16-46.el7.x86_64.rpm -y
systemctl start php-fpm.serrvice
将编写好的php放置到Nginx的默认发布目录位置
vim test.php
<?php
$redis = new Redis();
$redis->connect('192.168.63.102',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('192.168.63.109','redis','Dockerps-A1');
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 <font color=#FF0000>$key</font>";
echo "<br>";
echo "name is <font color=#FF0000>$data[$key]</font>";
echo "<br>";
}
?>
1.建立用户设定远程登录
yum install mariadb-server -y
systemctl start mariadb.service
部署后在MySQL端进行创建一个用户给与远程登录权限,使得Redis作为缓存可以用来同步数据使用
建立test表保证有可以查看的数据
MariaDB [test]> CREATE USER redis@'%' IDENTIFIED BY 'dockerps-A1' ;
MariaDB [test]> CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MariaDB [test]> INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');
MariaDB [test]> grant all on test.* to redis@'%';
2.注册UDF函数
yum install mariadb-devel -y #下载对应的开发包
unzip lib_mysqludf_JSON-master.zip
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
#利用udf函数使得其生成UDF函数
MariaDB [(none)]> show global variables like 'plugin_dir';
#查看函数的存储目录
cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
make -j 2 && make install
./utils/install_server.sh
这里C端为MySQL的UDF函数节点通过已经注册的UDF函数和触发器结合使数据发生改变后将数据的更改通知到前端gearmand节点使其从Redis节点从新同步发生改变的数据
S端为前端worker节点即前端gearmand,同样需要在php环境中兼容这个插件的模块
./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/
make -j 2 && make install
MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so';
MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';
#注册Gearman的UDF函数使其可以正常调用数据库的内容
MariaDB [(none)]> SELECT gman_servers_set('192.168.63.101:4730');
#IP为S端IP以及端口为gearmand的运行端口
vim trigger.sql
use test;
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 ;
yum install gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm -y
systemctl start gearmand.service
编写worker文件
vim worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('192.168.63.102', 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);
}
?>
到此这篇关于MySQL+Redis缓存+Gearman共同构建数据库缓存的文章就介绍到这了,更多相关Redis Gearman数据库缓存内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
--结束END--
本文标题: MySQL+Redis缓存+Gearman共同构建数据库缓存的方法
本文链接: https://lsjlt.com/news/44940.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0