gearman + mysql方式實(shí)現(xiàn)持久化操作示例
本文實(shí)例講述了gearman+mysql方式實(shí)現(xiàn)持久化操作。分享給大家供大家參考,具體如下:
1、為什么要持久化?
gearman的job server中的工作隊(duì)列存儲(chǔ)在內(nèi)存中,一旦服務(wù)器有未處理的任務(wù)時(shí)重啟或者宕機(jī),那么這些任務(wù)就會(huì)丟失。持久化存儲(chǔ)隊(duì)列可以允許添加后臺(tái)任務(wù),并將其存儲(chǔ)在外部的持久型隊(duì)列里(比如MySQL數(shù)據(jù)庫(kù))。
2、關(guān)于gearman的持久化的文章,建議可以看官方文檔
http://gearman.org/manual/job_server/#persistent_queues
3、創(chuàng)建用于持久化的數(shù)據(jù)庫(kù)和表
CREATE DATABASE gearman;CREATE TABLE `gearman_queue` (`unique_key` varchar(64) NOT NULL,`function_name` varchar(255) NOT NULL,`priority` int(11) NOT NULL,`data` longblob NOT NULL,`when_to_run` int(11),PRIMARY KEY (`unique_key`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4、創(chuàng)建gearman用戶
> create user ’gearman’@’%’ IDENTIFIED BY ’123456’;> grant all on gearman.* TO ’gearman’@’%’;> flush privileges;
5、啟動(dòng)gearmand時(shí)指定持久化參數(shù)
> gearmand -q libdrizzle --libdrizzle-host=192.168.1.100 --libdrizzle-port=3306 --libdrizzle-user=gearman --libdrizzle-password=123456 --libdrizzle-db=gearman --libdrizzle-table=gearman_queue --libdrizzle-mysql
或者使用如下
> gearmand -q mysql --mysql-host=192.168.1.100 --mysql-port=3306 --mysql-user=gearman --mysql-password=123456 --mysql-db=gearman --mysql-table=gearman_queue
如果出現(xiàn)如下問(wèn)題,說(shuō)明你在編譯安裝gearman時(shí)沒(méi)有把libdrizzle裝上
gearmand: unrecognised option ’--libdrizzle-host=192.168.1.100’
在如下網(wǎng)址,下載libdrizzle
https://launchpad.net/libdrizzle/+download
如:libdrizzle-5.1.4.tar.gz
安裝libdrizzle
> tar xf libdrizzle-5.1.4.tar.gz> cd libdrizzle-5.1.4
這里最好不要指定--prefix,因?yàn)槟阒付似渌夸洠旅鎔earman編譯時(shí)可能會(huì)找不到相關(guān)頭文件和鏈接庫(kù),需要你手動(dòng)添加軟鏈接
> ./configure> make && make install
然后我們重新編譯安裝gearman
> tar xf gearmand-1.1.12.tar.gz > cd gearmand-1.1.12
如果configure的有哪些參數(shù)不清楚,可以用下面命令查看
> ./configure --help
這里需要安裝mysql-devel,以便gearman支持mysql的持久化
> yum install mysql-server mysql-devel
因?yàn)槲以缦妊b過(guò)gearman,沒(méi)有指定--prefix,所以這里也沒(méi)有指定,有需要的可以自行指定
> ./configure> make && make install
configure完成最后顯示的一段信息
* LIBS: * LDFLAGS Flags: * Assertions enabled: no* Debug enabled: no* Warnings as failure: no* Building with libsqlite3 no* Building with libdrizzle yes* Building with libmemcached not found* Building with libpq no* Building with tokyocabinet no* Building with libmysql yes* SSL enabled: no* cyassl found: no* openssl found: yes* make -j: 2* VCS checkout: no* sphinx-build: :
最后可以看到libdrizzle和libmysql那地方顯示yes
查看是否安裝上
> gearmand --help
如果出現(xiàn)如下錯(cuò)誤
gearmand: error while loading shared libraries: libdrizzle.so.9: cannot open shared object file: No such file or directory
請(qǐng)打開修改/etc/ld.so.conf
> vi /etc/ld.so.conf
加入如下一句話
/usr/local/lib
運(yùn)行l(wèi)dconfig
> ldconfig
再次運(yùn)行上面的gearmand --help,如果出現(xiàn)如下信息,則安裝成功
builtin:libdrizzle:--libdrizzle-host arg (=localhost) Host of server.--libdrizzle-port arg (=3306) Port of server. (by default Drizzle)--libdrizzle-uds arg Unix domain socket for server.--libdrizzle-user arg (=root) User name for authentication.--libdrizzle-password arg Password for authentication.--libdrizzle-db arg (=gearman) Database to use.--libdrizzle-table arg (=queue) Table to use.--libdrizzle-mysql Use MySQL protocol.MySQL:--mysql-host arg (=localhost) MySQL host.--mysql-port arg (=3306) Port of server. (by default 3306)--mysql-user arg MySQL user.--mysql-password arg MySQL user password.--mysql-db arg MySQL database.--mysql-table arg (=gearman_queue) MySQL table name.
通過(guò)libdrizzle啟動(dòng)gearmand如果出現(xiàn)如下問(wèn)題
gearmand: Error while initializing the queue : libdrizzle
并且日志里面的記錄是這樣的
ERROR 2017-02-22 07:51:02.536574 [ main ] Failed to initialize libdrizzle: initialize(QUEUE_ERROR) -> libgearman-server/queue.cc:246
不知道是不是mysql版本太高的原因,還是其他的原因,如果大家試了實(shí)在不行還是換另一個(gè)方式,另一方式我測(cè)試是成功的。
創(chuàng)建一個(gè)后臺(tái)job
> gearman -f test -b 123456
查看數(shù)據(jù)庫(kù)如下:
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
相關(guān)文章:
1. SQL Server 2000數(shù)據(jù)庫(kù)崩潰后的補(bǔ)救措施2. MySQL 性能、監(jiān)控與災(zāi)難恢復(fù)3. 在RHEL4U4上安裝Oracle10GS24. MySQL 語(yǔ)句大全:創(chuàng)建、授權(quán)、查詢、修改5. ORACLE中常用的幾種正則表達(dá)式小結(jié)6. 詳解MySQL批量入庫(kù)的幾種方式7. 實(shí)例講解如何使用Oracle數(shù)據(jù)庫(kù)to_date()8. 掌握SQL Server實(shí)戰(zhàn)教程之SQL Server的安裝指南9. PyCharm MySQL可視化Database配置過(guò)程圖解10. Mybatis常見注解有哪些(總結(jié))
