一、基于lnmp配置gearman实现Redis和MySQL数据同步
我们本篇博客解决的就是:如何将mysql端修改的数据及时更新到rdis端,实现数据同步。
1、在server3上,解压lib_mysqludf_json-master.zip
[root@server3 ~]# yum install unzip -y
[root@server3 ~]# ls
[root@server3 ~]# unzip lib_mysqludf_json-master.zip
2、安装gcc
和mariadb-devel
,mariadb-devel和gcc不能同时安装,同时安装只会安装其中一个。
[root@server3 ~]# cd lib_mysqludf_json-master
[root@server3 lib_mysqludf_json-master]# yum install gcc -y
[root@server3 lib_mysqludf_json-master]# yum install mariadb-devel -y
3、gcc进行编译
[root@server3 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
4、将
lib_mysqludf_json-master/lib_mysqludf_json.so
模块拷贝到/usr/lib64/mysql/plugin/
插件目录下
cp lib_mysqludf_json-master/lib_mysqludf_json.so /usr/lib64/mysql/plugin/
5、登陆数据库,查看插件目录
[root@server3 lib_mysqludf_json-master]# mysql -uroot -predhat
MariaDB [(none)]> show global variables like 'plugin_dir';
6、注册udf函数,并查看。
MariaDB [(none)]> create FUNCTION json_object returns string soname 'lib_mysqludf_json.so';
MariaDB [(none)]> select * from mysql.func;
7、安装libgearman安装包,并安装管理gearman的分布式队列插件,进行编译和安装
[root@server3 ~]# yum install libgearman-* libevent-devel-2.0.21-4.el7.x86_64.rpm -y
[root@server3 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[root@server3 ~]# cd gearman-mysql-udf-0.6
[root@server3 gearman-mysql-udf-0.6]# ls
[root@server3 gearman-mysql-udf-0.6]# ./configure -libdir=/usr/lib64/mysql/plugin/ --with-mysql
[root@server3 gearman-mysql-udf-0.6]# make && make install
8、再次注册两个udf函数
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';
MariaDB [(none)]> select * from mysql.func;
9、指定gearman的服务信息
MariaDB [(none)]> select gman_servers_set('172.25.2.1:4730');
10、编写mysql触发器脚本test.sql,并导入test数据库
[root@server3 ~]# 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');
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 ;
[root@server3 ~]# mysql -predhat < test.sql
[root@server3 ~]# mysql -uroot -predhat
11、查看触发器
MariaDB [(none)]> SHOW TRIGGERS FROM test;
12、在server1上(gman的worker端),开启gearmand服务并查看其端口号4730是否开启
[root@server1 ~]# systemctl start gearmand
[root@server1 ~]# netstat -tnlp
13、编写worker.php
脚本,并放到/usr/local目录下
[root@server1 ~]# vim worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
$redis = new Redis();
$redis->connect('172.25.2.2', 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@server1 ~]# cp worker.php /usr/lcoal/
14、在后台运行worker
[root@server1 ~]# nohup php /usr/local/worker.php &> /dev/null &
15、测试,在server3更新数据并查看(注意:其中id=1的因为是之前更改的,所以不生效)
MariaDB [(none)]> use test;
MariaDB [test]> update test set name='ranran' where id=2;
MariaDB [test]> select * from test;
MariaDB [test]> update test set name='qiuqiu' where id=3;
在server2尝试获取,发生改变
[root@server2 redis-5.0.3]# redis-cli
127.0.0.1:6379> get 2
127.0.0.1:6379> get 3
在浏览器中查看,看到发生改变
此时,就实现了Redis和MySQL数据同步了。