溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

gearman + mysql方式實(shí)現(xiàn)持久化操作示例

發(fā)布時(shí)間:2020-09-04 03:20:08 來(lái)源:腳本之家 閱讀:150 作者:懷素真 欄目:MySQL數(shù)據(jù)庫(kù)

本文實(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)打開(kāi)修改/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ù)如下:

gearman + mysql方式實(shí)現(xiàn)持久化操作示例

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《MySQL索引操作技巧匯總》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》

希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI