Redis - using Gearmand as mysql cache server

Posted by onedumbcoder on Fri, 13 Mar 2020 07:53:45 +0100

Redis - using Gearmand as mysql cache server

Article directory

1. What is gearland?

Gearmand is a machine used to delegate work to other machines, distributed calls are more suitable for doing a certain work, and
A system that performs a task to load balance multiple calls, or to call functions in other languages.
In short, the client program submits the request to gearmand, which forwards the request to the appropriate worker to process the request, and finally returns the result through gearmand.

Operation process:
Client --> Job --> Worker

  • Client request initiator, client program can be any language: C, PHP, Perl, Python, etc.
  • The Job request dispatcher, the load coordinator forwards the request from the Client to the appropriate Worker.
  • Worker request handler, handle the requests distributed by Job, can be any language

2. Deployment process

Start gearmand in server1:

systemctl start gearmand
systemctl status gearmand


In server3:

yum install unzip -y
unzip lib_mysqludf_json-master.zip 

To install MariaDB devel:

yum install -y mariadb-devel.x86_64

Compile module:

yum install gcc -y
cd lib_mysqludf_json-master
gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c

Put the module in the mysql plug-in Directory:

cp lib_mysqludf_json-master/lib_mysqludf_json.so /usr/lib64/mysql/plugin/

View in database:

MariaDB [(none)]> show global variables like 'plugin_dir';

Register udf function:

MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';


Install the plug-in to manage the distributed queues of gearman:

tar zxf gearman-mysql-udf-0.6.tar.gz 
yum install libevent-devel-2.0.21-4.el7.x86_64.rpm libgearman-* -y
cd gearman-mysql-udf-0.6
./configure --libdir=/usr/lib64/mysql/plugin/ --with-mysql
make && make install

Register udf function:

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';


To view functions:

MariaDB [(none)]> select * from mysql.func;

Specify gman service information:

MariaDB [(none)]> SELECT gman_servers_set('172.25.254.1:4730');


Write mysql trigger:

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 ;

Import data:

mysql -p123 < test.sql

To view triggers:

MariaDB [(none)]> SHOW TRIGGERS FROM test;

In server1:
Write the worker side of gman:

cd /usr/local
vim worker.php 

<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
 
$redis = new Redis();
$redis->connect('172.25.254.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);
}
?>

Run worker in the background:

nohup php /usr/local/worker.php &> /dev/null &

3. test

Modify database contents:

MariaDB [test]> update test set name='westos' where id=2;


View on redis:

redis-cli
127.0.0.1:6379> get 2
"westos"


In page view, data synchronization:

222 original articles published, 31 praised, 10000 visitors+
Private letter follow

Topics: MySQL MariaDB Redis PHP