Quantcast
Viewing all articles
Browse latest Browse all 11

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

Viewing all articles
Browse latest Browse all 11

Trending Articles