Redis Learning Note Four synchronization practices for Mysql and Redis

Source: Internet
Author: User

First, test environment in Ubuntu Kylin14.0464bit has installed MySQL, Redis, PHP, lib_mysqludf_json.so, Gearman. Click here to view the test database and table reference this article also has some basic operations, which are described in the previous article. 1, installation and installation Gearman-mysql-udfapt-GetInstall libgearman-devwget https://launchpad.net/gearman-mysql-udf/trunk/0.6/+download/gearman-mysql-udf-0.6.tar.gzTar-xzf gearman-mysql-udf-0.6. TAR.GZCD Gearman-mysql-udf-0.6./configure--with-mysql=/usr/bin/mysql_config--libdir=/usr/lib/mysql/plugin/makesudo make install register UDF function: CREATE function gman_do_background RETURNS STRING SONAME'libgearman_mysql_udf.so'; CREATE FUNCTION gman_servers_set RETURNS STRING SONAME'libgearman_mysql_udf.so'; CREATE FUNCTION gman_do RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_high RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_low RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_background RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_high_background RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_do_low_background RETURNS STRING SONAME"libgearman_mysql_udf.so"; CREATE AGGREGATE FUNCTION gman_sum RETURNS INTEGER SONAME"libgearman_mysql_udf.so"; CREATE FUNCTION gman_servers_set RETURNS STRING SONAME"libgearman_mysql_udf.so"; Specify the information for the Gearman server: SELECT gman_servers_set ('127.0.0.1:4730'); Use example: Refer to http://blog.csdn.net/xundh/article/details/46287681 set up a reverse.php worker.<?Php$worker=NewGearmanworker (); $worker-addserver (); $worker->addfunction ("Reverse","my_reverse_function"); while($workerWork ()); function My_reverse_function ($job) {returnStrrev ($jobworkload ());}?>Enter the command PHP reverse.php to run. Into MySQL, enter: SELECT Gman_do ("Reverse",'abcdef') as test from Users; ---From users can also be taken without. Write a picture here describing select Gman_do ("Reverse", password) As test from Users, you can see the output, where the password column has been processed by reverse's worker, and MySQL acts as client side: Here you can also enter the following command to test: SELECT Gman_do_high ("Reverse", password) as test from Users; --High priority writes here the picture description of the Select Gman_do_background ("Reverse", password) as test from Users; --background Low priority, returns the host and job number. Here write the picture description create Synctoredis job stop front of reverse worker, build a synctoredis.php<?php Ini_set ('Default_socket_timeout', -1);//very important, otherwise it may be reported redisexception with message read error on connection ...$worker =NewGearmanworker (); $worker-Addserver (); $worker->addfunction ('Synctoredis','Synctoredis'); $redis=NewRedis (); $redis->connect ('127.0.0.1',6379); Echo ("begin:\n");  while($workerWork ()); function Synctoredis ($job) {Global$redis; $workString= $jobworkload (); $work=Json_decode ($workString); Echo ('Get Value:');            Echo ($workString); Echo ("\ n"); Echo ('Json_decode:');            Var_dump ($work); Echo ("\ n"); if(!isset ($workuser_id)) {                    return false; } $redis-Set($workuser_id, $workString); }123456789Ten One A - - the - - - + - + A at - - - -test it in MySQL: SELECT gman_do ("Synctoredis", Json_object (user_id asUser_id,password aspassword)) As test from Users; if Redis monitoring is turned on, you can see that Redis has received data: Here's a picture describing the Redis query results here to write a picture description2, set up trigger DELIMITER $$ create TRIGGER Datatoredis after UPDATE on the Users for each ROW BEGIN SET @ret =gman_do_background ('Synctoredis', Json_object (new.user_id as' user_id ', new.email as' Email ', new.display_name as' Display_name ', New.password as' password ')); end$ $DELIMITER; Execute SQL statement test: INSERT INTO Users values ('8','New','3','Hello'); Update UsersSetEmail='[email protected]' whereUser_id=8here, write a picture describing the normal use, you can use the worker&Set as background task: nohup PHP synctoreids.php&other issues Sometimes, the command line executes PHP statements that may prompt the following error, [[email protected] redis]# php synctoredis.php php Fatal error:class ' Redis ' not fou ndinch/home/wwwroot/default/youai/php/redis/synctoredis.php on line6processing method: Running PHP command and Apache or Ngnix may use different php.ini, even if you see the Redis in php.ini, you also need to add the Redis module in the PHP command line using PHP.ini. View the php.ini location used by Ngnix

Redis Learning Note Four synchronization practices for Mysql and Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.