gearman + mysql方式實現持久化操作示例
本文例項講述了gearman+mysql方式實現持久化操作。分享給大家供大家參考,具體如下:
1、為什麼要持久化?
gearman的job server中的工作佇列儲存在記憶體中,一旦伺服器有未處理的任務時重啟或者宕機,那麼這些任務就會丟失。
持久化儲存佇列可以允許新增後臺任務,並將其儲存在外部的持久型佇列裡(比如MySQL資料庫)。
2、關於gearman的持久化的文章,建議可以看官方文件
http://gearman.org/manual/job_server/#persistent_queues
3、建立用於持久化的資料庫和表
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、建立gearman使用者
> create user 'gearman'@'%' IDENTIFIED BY '123456'; > grant all on gearman.* TO 'gearman'@'%'; > flush privileges;
5、啟動gearmand時指定持久化引數
> 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
如果出現如下問題,說明你在編譯安裝gearman時沒有把libdrizzle裝上
gearmand: unrecognised option '--libdrizzle-host=192.168.1.100'
在如下網址,下載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編譯時可能會找不到相關標頭檔案和連結庫,需要你手動新增軟連結
> ./configure > make && make install
然後我們重新編譯安裝gearman
> tar xf gearmand-1.1.12.tar.gz > cd gearmand-1.1.12
如果configure的有哪些引數不清楚,可以用下面命令檢視
> ./configure --help
這裡需要安裝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
檢視是否安裝上
> gearmand --help
如果出現如下錯誤
gearmand: error while loading shared libraries: libdrizzle.so.9: cannot open shared object file: No such file or directory
請開啟修改/etc/ld.so.conf
> vi /etc/ld.so.conf
加入如下一句話
/usr/local/lib
執行ldconfig
> ldconfig
再次執行上面的gearmand --help,如果出現如下資訊,則安裝成功
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如果出現如下問題
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版本太高的原因,還是其他的原因,如果大家試了實在不行還是換另一個方式,另一方式我測試是成功的。
建立一個後臺job
> gearman -f test -b 123456
檢視資料庫如下:
更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL索引操作技巧彙總》、《MySQL常用函式大彙總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧彙總》、《MySQL儲存過程技巧大全》及《MySQL資料庫鎖相關技巧彙總》
希望本文所述對大家MySQL資料庫計有所幫助。