MySQL Connector/Python 0.1.0-devel: first development snapshot

Today we’re releasing MySQL Connector/Python 0.1.0-devel. This is the first in a serie of development snapshots aimed to get more people trying it out and reporting problems. This is not a production-ready release, but it should work on all Unices and Windows which has Python 2.4 or later installed. (No, no Py3K, yet!)

You can download it from Launchpad: there is a .tar.gz and .zip

There will be documentation and tutorial, but installation is as easy as pie (pun intended):


shell> tar xzf mysql-connector-python-0.1.0-devel.tar.gz
shell> cd mysql-connector-python-0.1.0-devel
shell> python ./setup.py install

If you want to run the unittests, you’ll need a MySQL server running locally.

Running MySQL Cluster on Mac: working around a ndb_mgmd bug

A week ago we found a workaround for a bug in MySQL Cluster making it impossible to run a management node on MacOS X. Until the bug is fixed, you should use the --nodaemon option for the ndb_mgmd executable. Both MySQL Cluster v6.3 and v7.0 are affected.

Currently, I’m starting the management node like this:


(
cd /opt/mysql/mysql ;
./libexec/ndb_mgmd -f /opt/mysql/config.ini \
--nodaemon 2>/dev/null 1>&2 </dev/null &
)

Obviously, you’ll want to change the paths.

Eventually, the bug will get fixed, but until then you got no excuse to not try MySQL Cluster on Mac!

A simpler startup script for MySQL on MacOS X

What you do when you’re fed up with a script? Right, you write your own.You’ll have to excuse me for the long shell script you’ll find here below, but I’m not going to bother putting it on some download website.

It’s a shell script which starts and stops the MySQL server. Indeed, a replacement for the init.d script found in the MySQL distributions. I’m using it personally on my Macs and it’s not supported in any way.

But why? Well, I’m playing with MySQL Workbench, Server Administration. The MySQL init.d script didn’t work right away (oh, various reasons for that), so I used mine. So I figured it might be useful for others and it’s not complicated or shocking-new-stuff. If you want to use it, you’ll have to edit the 2 variables at the top. It’s only going to work on MacOS X.

#!/bin/bash
# Author: Geert Vanderkelen <geert@kemuri.org>

BASEDIR="/opt/mysql/mysql"
CNF="/opt/mysql/my51.cnf"

MODE="$1"
_RETURN="" # for returning from functions

pidof() {
    local CMD=$1
    local PID=`ps cax -o "pid,command" | awk -v cmd="$CMD" '{ if (\$2 == cmd) printf("%s",\$1) }'`
    if [ "x$PID" == "x" ]; then
        _RETURN=""
        return 1
    else
        _RETURN=$PID
        return 0
    fi
}

checkprocess() {
    local CMD=$1
    _RETURN=""
    pidof $CMD
    if [ $? -eq 0 ];
    then
        local PID=${_RETURN}
        kill -n 0 $PID 2>/dev/null 1>&2
        if [ $? -ne 0 ]; then
            return 2
        fi
        _RETURN=$PID
        return 0
    fi
    
    return 1
}

waituntildown() {
    local CMD=$1
    pidof $CMD
    while [ $? -eq 0 ]; do
        echo -n '.'
        sleep 1
        pidof $CMD
    done
}

exec_mysqld_safe() {
    (
        cd $BASEDIR
        ./bin/mysqld_safe --defaults-file=$CNF 2>/dev/null 1>&2 </dev/null &
    )
}

status() {
    checkprocess mysqld
    RET=$?
    if [ $RET -ne 0 ]; then
        if [ $RET -eq 1 ]; then
            echo "MySQL is not running."
        elif [ $RET -eq 2 ]; then
            echo "You have no permission to stop MySQL."
        fi
        return 1
    fi
    return 0
}

start() {
    checkprocess mysqld
    if [ $? -eq 0 ]; then
        echo "MySQL is running."
        exit 1
    fi
    echo -n "Starting MySQL.. "
    exec_mysqld_safe
    sleep 2
    checkprocess mysqld
    if [ $? -ne 0 ]; then
        echo " Failed!"
        exit 1
    fi
    echo " OK"

}

stop() {
    status
    RET=$?
    if [ $RET -ne 0 ]; then
        exit 1
    fi
    PID=${_RETURN}
    echo -n "Stopping MySQL.. "
    kill $PID 2>/dev/null 1>&2
    if [ $? -eq 0 ]; then
        waituntildown mysqld
        echo " OK"
    else
        echo " Failed!"
    fi
}

case "$MODE" in
    "start")
        start
    ;;
    
    "stop")
        stop
    ;;
    
    "restart")
        stop
        start
    ;;
    
    "status")
        status
        if [ $? -eq 0 ]; then
            echo "MySQL is running."
            exit 0
        else
            exit 1
        fi
    ;;
    
    "getpid")
        pidof mysqld
        if [ $? -eq 0 ]; then
            echo ${_RETURN}
        fi
    ;;
    
    *)
        echo "Usage: $0 {start|stop|restart|status|getpid}"
        exit 1
    ;;
esac

Mounting a MacOS X Disk Image at login

Work says that we need to store sensitive data like email and customer files on some encrypted media. This is a good thing. My laptop has my home directory secured, but I don’t want to encrypt everything on my desktop. The solution to this is to create an encrypted Disk Image (using Disk Utility) and make Mail.app store my email there. This all works great!

The problem surfaced this week when I decided it would be good to shutdown my desktop to save energy (which I usually did, but I got sloppy). What happened? When Mac OS X shuts down, and there is still some application doing something with your opened Disk Image, it will not remove the mount-point, e.g. in my case /Volumes/FileVaultBlack. Later, if you open it again, Mac OS will create the directory /Volumes/FileVaultBlack 1 and use that as mount-point. The issue here is that I configured the AccountPath in Mail.app to a location in /Volumes/FileVaultBlock.. In mortals speech: “Email is broken”.

The solution is to mount the Disk Image when logging in using a predefined mounting point. This involves making a directory and creating an AppleScript application. So lets get busy!

First, eject the Disk Image using the Finder application. It will complain when applications still need it, so you have to make sure that nothing is accessing it.

Create the mountpoint. The idea is to create a directory in /Volumes/ (it doesn’t really matter where you do it, but I like to keep them in one place). I suggest opening Terminal for this and do the following:


shell> mkdir /Volumes/FileVaultBlack

You want to name it differently. I’m using the name of the Disk Image, just like MacOS would do when you double click the .dmg-file.

Open AppleScript Editor and save the following script into a file called, for example, FileVaultBlack_mountatlogin.


do shell script "hdiutil attach /Users/geert/FileVaultBlack.dmg -mountpoint /Volumes/FileVaultBlack"

The above script will execute the hdiutil command-line utility telling it to mount the given Disk Image (.dmg-file) on the give mount-point (the one we created earlier).

Test this script by pushing the Run-button in the AppleScript Editor and see whether your Disk Image is visible in Finder. If not, check the locations of both the Disk Image and the mount-point you gave in the script.

When you saved it, save it again as an application. Still within the AppleScript Editor do the following:

  1. Choose Save As from the File menu
  2. Save As: save it using slighly different name, for example MountFileVaultBlack
  3. File Format: Application
  4. You don’t have to check Run Only

We save it 2 times, once as a script, once as an application. This way, you can easily edit the script later, and save it again as an application. (I’m using different names because it’s less confusing when writing this post.)

Lets now add it to our Login Items:

  1. Open System Preferences and go to Accounts
  2. Make sure you choose My Account, i.e. your account, and go to Login Items
  3. Add an item to the list, clicking the +-sign
  4. Locate the application you created above, in our exampled named MountFileVaultBlack, and press Add.
  5. Check also the Hide option next to the new item.

That should be it. Log out and in and check if the Disk Image is mounting. There should be an icon happily jumping in the Dock while the mounting is on-going. Open Finder and see if your files are accessible.

Want to compile a MySQL Cluster MGM API application?

Here is a quick way to compile a simple MGM API application. The example will get the state of all nodes in MySQL Cluster and print whether they are connected or not.

All this without a Makefile, we just want to have some simple example on Linux to see how it works. It’s basic, maybe, but sometimes useful to just have a peek.

Requirements! We assume that:

  1. you installed MySQL Cluster 6.3 or higher, preferably under /usr/local/mysql,
  2. your cluster is up and shiny,
  3. and ndb_mgmd runs on the same machine you are compiling the MGM API test application on.

The code, save it in a file called mgmapi_test.cc (from the MySQL manual):

#include <stdlib.h>
#include <mgmapi/mgmapi.h>

int main()
{
  NdbMgmHandle handle = ndb_mgm_create_handle();
  ndb_mgm_set_connectstring(handle,"localhost");
  ndb_mgm_connect(handle,0,0,0);
  struct ndb_mgm_cluster_state *state = ndb_mgm_get_status(handle);
  for(int i=0; i < state->no_of_nodes; i++)
  {
    struct ndb_mgm_node_state *node_state= &state->node_states[i];
    printf("node with ID=%d ", node_state->node_id);

    if(node_state->version != 0)
      printf("connected\n");
    else
      printf("not connected\n");
  }
  free((void*)state);
  ndb_mgm_destroy_handle(&handle);
}

Lets compile it! Note that it goes all on one line, but you could also paste the following lines in a shell script:

(
 MYBASE="/usr/local/mysql";
 PATH="$MYBASE/bin:$PATH";
 g++ `mysql_config --libs` `mysql_config --cflags` -lndbclient \
  -I$MYBASE/include/storage/ndb \
  -o mgmapi_test mgmapi_test.cc
)

No errors? Lets run it! Setting LD_LIBRARY_PATH doesn’t hurt:

(
  export LD_LIBRARY_PATH="/usr/local/mysql/lib";
  ./mgmapi_test
)

The result should be something like this:

node with ID=1 connected
node with ID=2 connected
node with ID=3 connected
node with ID=4 connected
node with ID=10 connected
node with ID=11 not connected
node with ID=12 not connected
node with ID=13 not connected
..

FAQ: Wouldn’t it be better to have a Makefile? Absolutely. Will there be problems doing it above? Sure, but the error message should help; start by checking if the locations are OK.