In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly tells you how gearman implements redis cache mysql. You can check the relevant technical terms on the Internet or find some related books to supplement them. We will not dabble here, so let's go straight to the topic. I hope this article on how gearman implements redis cache mysql can bring you some practical help.
Introduction to Gearman:
Gearman is a distributed task distribution framework. The design is simple and has been widely supported. A typical Gearman application includes the following parts:
1. Gearman Job Server:Gearman core program, which runs in the background in the form of daemon
2. Gearman Client: it can be understood as the recipient of a task. For example, if I want to execute an email task in the background, I can call a Gearman Client in the program and pass in the email information, and then the execution result can be displayed to the user immediately, and the task itself will run slowly in the background.
3. Gearman Worker: the real executor of the task generally needs to write the specific logic and run it through the daemon. 4. After receiving the task content transmitted by Gearman Client, Gearman Worker will process it sequentially.
Design ideas:
First of all, we use mysql UDF (realized by the combination of lib_mysqludf_json and gearman-mysql-udf) to trigger the trigger to transfer the data into Gearman when the data in mysql is changed. at this time, mysql is equivalent to the clinet of Gearman. Then run the php program written by yourself as worker to transfer the data from Gearman to Redis, which is equivalent to the consumer of Gearman.
Implementation process:
1. Configure the YUM source and install gearmand
Http://down.51cto.com/data/2274661 # # YUM package file configuration, and packages required by GEARMAND
1. Yum install-y php-pecl-gearman libgearman libgearman-devel gearmand nc
2. Start the service / etc/init.d/gearmand start
3. Check whether the status is OK netstat-alnutp | grep gearman
# the default port used by gearman is 4370
2. Install database and php-MySQL plug-ins
1. Database installation (brief)
2. Plug-in installation yum-yinstall php-mysql # installation of this plug-in is mainly to use PHP to collect and write data
3. Start MySQL
3. Install lib_mysqludf_json on the data cloud server
1 、 unzip master.zip
2 、 cd lib_mysqludf_json-master
3. Rm-rf lib_mysqludf_json.so
4. Gcc $(mysql_config-- cflags)-shared-fPIC-olib_mysqludf_ json.solib_mysqludf_json.c
# # copy the recompiled lib_mysqludf_json.so to the / usr/local/mysql/lib/plugin directory
4. Install gearman-mysql-udf on the data cloud server
1. Extract the package tar xf gearman-mysql-udf-0.6.tar.gz-C. /
2 、 Cd gearman-mysql-udf-0.6
3. Compile. / configure--with-mysql=/usr/local/mysql/bin/mysql_config-- libdir=/usr/local/mysql/lib/plugin/
# Note: this is the directory of the data installation package defined by yourself. If you install it with the YUM package, use it.
. / configure-with-mysql=/usr/bin/mysql_config-libdir=/usr/lib64/plugin/
Compilation error: configure: error:At least version 0.33 of libgearman is required for gearman-mysql-udf
Installation: yum-y installlibgearman-devel # due to the low version of the original package, just reinstall it.
5. Install the make&& make install # # plug-in and install it into the plug-in directory of MySQL.
6. Enter the database, create the corresponding function,trigger and set the gearmanserver information
1. Createfunction json_object returns string soname 'lib_mysqludf_json.so'
2. Create functiongman_do_background returns string soname 'libgearman_mysql_udf.so'
3. Createfunction gman_servers_set returns string soname 'libgearman_mysql_udf.so'
7. It is recommended to install the 3.0.7 version of redis# and install the driver connecting redis with php, and install redis with yum in the test environment.
1. Yum install php-pecl-redis redis-y
2. Start the redis service servie redis start
3. Log in to redis for simple verification
Root@nod1 gearman-mysql-udf-0.6] # redis-cli
Redis 127.0.0.1 6379 > keys *
1) "counter:__rand_int__"
2) "key:__rand_int__"
3) "save"
4) "mylist"
8. Log in to the database, add a trigger to a table under a certain database according to the business, and set the letter of gearman server
1. / usr/local/mysql/bin/mysql-S / tmp/mysql3306.sock-p
2 、 create database wy
3. Create table test (idint primary key,name char (20))
4. Setting gearman server information # # is very important. If it is not executed, the data cannot be collected into the REDIS.
MariaDB [wy] > SELECTgman_servers_set ('127.0.0.1pur4730')
+-+
| | gman_servers_set ('127.0.0.1pur4730') | |
+-+
| | 127.0.0.1pur4730 |
+-+
9. Add triggers
DELIMITER $$
CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
SET@ret=gman_do_background ('syncToRedis', json_object (NEW.id AS `id`, NEW.name as `name`))
END$$
DELIMITER
10. Write a worker program that is responsible for transferring the data from gearman to redis:
File name: redis_workder.php
1. Write:
2. Start phpredis_worker.php
Startup error report: PHPWarning: GearmanWorker::work (): send_packet (GEARMAN_ERRNO) Failed to send server-options packet-> libgearman/connection.cc:485 in / home/redis_worker.php on line 8
Solution: / usr/sbin/gearmand-d-L 127.0.0.1-p 4730
Then at startup: nohup php redis_worker.php & keep running in the background
Startup gearmand error / var/log/mearmond.log error not resolved
ERROR 2016-12-28 07 socket 46 ERROR 38.000000 [main] socket () (Address family not supported by protocol)-> libgearman-server/gearmand.cc:468
ERROR 2016-12-28 07 gearmand_sockfd_close 46 gearmand_sockfd_close 38.000000 [main] called with aninvalid socket-> libgearman-server/io.cc:933
Summary:
Test by writing data in the MYSQL database, and then observe whether the REDIS has data, if so, the efficiency of this method is very low, in 100W data, the database write performance is greatly reduced.
Gearman is how to achieve redis cache mysql to tell you here, for other related issues you want to know can continue to pay attention to our industry information. Our section will capture some industry news and professional knowledge to share with you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 302
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.