Gearman Queue error: mysql_stmt_execute failed: Unknown prepared statement handler (1)

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, February 20, 2013

Follow me on Twitter as @mattiasgeniar

If you’re running Gearman from a self-compiled version (or self-packaged version), you can run into the following error. This is the same on Ubuntu, Debian or CentOS/RHEL clones.

[  proc ] mysql_stmt_execute failed: Unknown prepared statement handler (1) given to mysqld_stmt_execute -> libgearman-server/plugins/queue/mysql/queue.cc:356
 [  proc ] gearman_server_job_add gearman_server_run_command(QUEUE_ERROR) -> libgearman-server/server.cc:284

The above is visible from the /var/log/gearmand/gearmand.log logs, that which calls the queue will fail with an error message as such:

Warning: GearmanClient::doBackground(): _client_run_tasks(GEARMAN_SERVER_ERROR) queue_insert_error:QUEUE_ERROR -> libgearman/client.cc:1522 in /path/to/script.php on line xxx

Option #1: missing UUID package

The solution is relatively simply if this is your problem: you may not have the uuid package installed. The package is meant to generate unique IDs that Gearman would use to assign to each job, without the package the unique ID’s can not be generated. If you’re running a MySQL persistent backend for your Gearman, inserts would fail without such a unique ID because of primairy key violations.

As such, install it:

$ apt-get install uuid
$ yum install uuid

Depending on your OS, use either apt/yum.

MySQL ‘s wait_timeout is set too low

You’re using MySQL as a backend for Gearmand. Gearman’s MySQL driver (or libdrizzle) does not seem to have good reconnect support. It connects to the database the moment the gearmand service starts, but it does not reconnect to the database if it loses connection. If your mysql wait_timeout is set too low, MySQL will disconnect the idling Gearman client, and Gearman won’t reconnect. The fix is then to restart Gearman to connect to MySQL again.

A long-term fix could be to increase your MySQL’s wait_timeout to the maximum value (don’t forget to also increase your interactive_timeout in the same run, and you may want to check max_user_connections to avoid having too many idling connections per user). This also works for MySQL forks such as MariaDB.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.