本文實例講述了gearman+mysql方式實現(xiàn)持久化操作。分享給大家供大家參考,具體如下:
1、為什么要持久化?
gearman的job server中的工作隊列存儲在內(nèi)存中,一旦服務(wù)器有未處理的任務(wù)時重啟或者宕機(jī),那么這些任務(wù)就會丟失。
持久化存儲隊列可以允許添加后臺任務(wù),并將其存儲在外部的持久型隊列里(比如MySQL數(shù)據(jù)庫)。
2、關(guān)于gearman的持久化的文章,建議可以看官方文檔
http://gearman.org/manual/job_server/#persistent_queues
3、創(chuàng)建用于持久化的數(shù)據(jù)庫和表
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、啟動gearmand時指定持久化參數(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)如下問題,說明你在編譯安裝gearman時沒有把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,因為你指定了其它目錄,下面gearman編譯時可能會找不到相關(guān)頭文件和鏈接庫,需要你手動添加軟鏈接
> ./configure
> make make install
然后我們重新編譯安裝gearman
> tar xf gearmand-1.1.12.tar.gz
> cd gearmand-1.1.12
如果configure的有哪些參數(shù)不清楚,可以用下面命令查看
這里需要安裝mysql-devel,以便gearman支持mysql的持久化
> yum install mysql-server mysql-devel
因為我早先裝過gearman,沒有指定--prefix,所以這里也沒有指定,有需要的可以自行指定
> ./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
查看是否安裝上
如果出現(xiàn)如下錯誤
gearmand: error while loading shared libraries: libdrizzle.so.9: cannot open shared object file: No such file or directory
請打開修改/etc/ld.so.conf
加入如下一句話
運行l(wèi)dconfig
再次運行上面的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.
通過libdrizzle啟動gearmand如果出現(xià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版本太高的原因,還是其他的原因,如果大家試了實在不行還是換另一個方式,另一方式我測試是成功的。
創(chuàng)建一個后臺job
> gearman -f test -b 123456
查看數(shù)據(jù)庫如下:

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
您可能感興趣的文章:- 詳解使用Docker部署MySQL(數(shù)據(jù)持久化)
- Java emoji持久化mysql過程詳解
- MySQL8新特性:持久化全局變量的修改方法
- MySQL8新特性:自增主鍵的持久化詳解
- MySQL 8.0統(tǒng)計信息不準(zhǔn)確的原因
- 概述MySQL統(tǒng)計信息
- 詳解mysql持久化統(tǒng)計信息