Fixing MySQL Bug#2: now MySQL makes toast!

MySQL makes toast

Historical MySQL Bug#2, opened 12 Sep 2002, states that MySQL Connector/J doesn’t make toast. It hasn’t been fixed for more than 14 years. I’ve finally created a patch for it.

First of all: why only fix this for MySQL Connector/J? We should make sure the server can do this for any implementation! With this fix, now MySQL server (starting with version 5.1) can make toast.

There are few dependences though (see assembled setup picture):

  1. Raspberry Pi + PiFace shield
  2. Power switch relay (I’ve used an IoT Power Relay)
  3. Toaster oven (any cheap mechanical model will work)


  1. Make_toast binary, which is run on the Raspberry Pi and PiFace interface (you’ll need to install the PiFace library):
    import sys
    from time import sleep
    if len(sys.argv) == 2 and sys.argv[1].isdigit():
            toast_time = sys.argv[1]
            toast_time = 10
    print "Toasting for " + str(toast_time) + " seconds..."
    import pifacedigitalio as p
    except (KeyboardInterrupt, SystemExit):
            print "Exiting and turning off heat..."
    print "Your toast is ready! Enjoy! "
  2. MySQL UDF, based on lib_mysqludf_sys, which calls the make_toast binary:
    char* make_toast(
            UDF_INIT *initid
    ,       UDF_ARGS *args
    ,       char* result
    ,       unsigned long* length
    ,       char *is_null
    ,       char *error
            FILE *pipe;
            char line[1024];
            unsigned long outlen, linelen;
            char buf[40];
            result = malloc(1);
            outlen = 0;
            sprintf(buf, "make_toast %s", args->args[0]);
            pipe = popen(buf, "r");
            while (fgets(line, sizeof(line), pipe) != NULL) {
                    linelen = strlen(line);
                    result = realloc(result, outlen + linelen);
                    strncpy(result + outlen, line, linelen);
                    outlen = outlen + linelen;
            if (!(*result) || result == NULL) {
                    *is_null = 1;
            } else {
                    result[outlen] = 0x00;
                    *length = strlen(result);
            return result;


mysql> call make_toast(300)

Demo picture (thanks to my colleague Fernando Laudares Camargos), actual video will follow:

MySQL makes toast

Implementation details:

MySQL makes toastHardware/wiring

The relay switch powers on the toaster oven, and no modifications are needed to the oven itself. Make sure the timer is set to 30 min initially, the Raspberry Pi/MySQL UDF will now control how long you toast the bread.

The setup wiring is super easy (but may be counterintuitive if you are used to working with Arduino): use the output pins (image) and connect 5v on the PiFace to the “+” sign on the relay switch, and one of the pins to the “-” sign on the relay switch.

Software install

  1. Install PiFace software and Python bindings
  2. Test the make_toast python script
  3. Add user “mysql” to the spi and gpio groups so it can manipulate pins:
    # gpasswd -a mysql gpio
    # gpasswd -a mysql spi
  4. Download the make toast UDF code and run install.sh.
mysql> call make_toast(300);

Enjoy your toast when it is hot!

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com