setup.mysql.bash 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/env bash

function usage() {
    echo "setup.mysql.bash"
    echo "--mysqlbuild=$mysqlbuild --shutdown=$shutdown --install=$install --startup=$startup"
}

mysqlbuild=
shutdown=1
install=1
startup=1
s3bucket=tokutek-mysql-build
13 14
sleeptime=60
builtins="mysqlbuild shutdown install startup s3bucket sleeptime"
15
mysqld_args="--user=mysql --core-file --core-file-size=unlimited"
16
sudo=/usr/bin/sudo
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
defaultsfile=""
if [ -f /etc/$(whoami).my.cnf ] ; then
    defaultsfile=/etc/$(whoami).my.cnf
fi

function is_builtin() {
    local v=$1; shift
    local x
    for x in $* ; do 
        if [ $v = $x ] ; then echo 1; return; fi
    done
    echo 0
}

while [ $# -gt 0 ] ; do
    arg=$1; shift
    if [ $arg = "--help" -o $arg = "-h" -o $arg = "-?" ] ; then
        usage; exit 1
    elif [[ $arg =~ --(.*)=(.*) ]] ; then
        r=$(is_builtin ${BASH_REMATCH[1]} $builtins)
        if [ $r = 1 ] ; then
            eval ${BASH_REMATCH[1]}=${BASH_REMATCH[2]}
        else
            mysqld_args="$mysqld_args $arg"
        fi
    else
        mysqld_args="$mysqld_args $arg"
    fi
done

47 48
if [ -d /data/mysql/tmp ] ; then mysqld_args="$mysqld_args --tmpdir=/data/mysql/tmp"; fi

49 50 51 52 53 54
if [[ $mysqlbuild =~ (.*)-(tokudb\-.*)-(linux)-(x86_64) ]] ; then
    mysql=${BASH_REMATCH[1]}
    tokudb=${BASH_REMATCH[2]}
    system=${BASH_REMATCH[3]}
    arch=${BASH_REMATCH[4]}
else
55
    echo $mysqlbuild is not a tokudb build
56 57 58 59 60 61 62 63 64
fi

if [ ! -d downloads ] ; then mkdir downloads; fi

pushd downloads
if [ $? != 0 ] ; then exit 1; fi

basedir=$PWD

65 66 67 68 69 70 71 72 73 74
mysqltarball=$mysqlbuild.tar.gz

if [ -f $mysqlbuild.tar.gz ] ; then
    compression=-z
    mysqltarball=$mysqlbuild.tar.gz
elif [ -f $mysqlbuild.tar.bz2 ] ; then
    compression=-j
    mysqltarball=$mysqlbuild.tar.bz2
fi

75 76 77 78 79 80 81 82 83 84
# get the release
if [ ! -f $mysqltarball ] ; then
    s3get $s3bucket $mysqltarball $mysqltarball 
    if [ $? -ne 0 ] ; then exit 1; fi
fi
if [ ! -f $mysqltarball.md5 ] ; then
    s3get $s3bucket $mysqltarball.md5 $mysqltarball.md5
    if [ $? -ne 0 ] ; then exit 1; fi
fi

85
# check the md5 sum
86
md5sum --check $mysqltarball.md5
87
if [ $? -ne 0 ] ; then
88
    # support jacksum md5 output which is almost the same as md5sum
89 90 91
    diff -b <(cat $mysqltarball.md5) <(md5sum $mysqltarball)
    if [ $? -ne 0 ] ; then exit 1; fi
fi
92

93 94 95
# set ldpath
ldpath=""
if [ -d /usr/local/gcc-4.7/lib64 ] ; then
96
    echo skip ldpath="export LD_LIBRARY_PATH=/usr/local/gcc-4.7/lib64:\$LD_LIBRARY_PATH;"
97 98
fi

99 100 101
# shutdown mysql
if [ $shutdown -ne 0 ] ; then
    if [ -x /etc/init.d/mysql ] ; then
102
        $sudo setsid /etc/init.d/mysql stop
103 104 105
    else
        /usr/local/mysql/bin/mysqladmin shutdown
    fi
106
    sleep $sleeptime
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
fi

pushd /usr/local
if [ $? = 0 ] ; then 
    rm mysql
    popd
fi

# install the release
pushd /usr/local/mysqls 2>/dev/null
if [ $? = 0 ] ; then
    mysqldir=mysqls/$mysqlbuild
else
    pushd /usr/local
    if [ $? -ne 0 ] ; then exit 1; fi
    mysqldir=$mysqlbuild
fi

if [ ! -d $mysqlbuild ] || [ $install -ne 0 ] ; then
    rm mysql
127
    if [ -d $mysqlbuild ] ; then $sudo rm -rf $mysqlbuild; fi
128

129
    tar -x $compression -f $basedir/$mysqltarball
130 131
    if [ $? -ne 0 ] ; then exit 1; fi
    ln -s $mysqldir /usr/local/mysql
132 133 134
    if [ $? -ne 0 ] ; then exit 1; fi
    ln -s $mysqldir /usr/local/$mysqlbuild
    if [ $? -ne 0 ] ; then exit 1; fi
135 136 137 138 139 140

    installdb=$mysqlbuild/bin/mysql_install_db
    if [ ! -f $installdb ] ; then
        installdb=$mysqlbuild/scripts/mysql_install_db
    fi

141 142
    $sudo chown -R mysql $mysqlbuild/data
    $sudo chgrp -R mysql $mysqlbuild/data
143 144 145 146 147 148

    # 5.6 debug build needs this 
    if [ ! -f $mysqlbuild/bin/mysqld ] && [ -f $mysqlbuild/bin/mysqld-debug ] ; then
	ln $mysqlbuild/bin/mysqld-debug $mysqlbuild/bin/mysqld
    fi

149 150
    if [ -z "$defaultsfile" ] ; then 
        default_arg=""
151
    else
152
        default_arg="--defaults-file=$defaultsfile"
153
    fi
154
    $sudo bash -c "$ldpath $installdb $default_arg --user=mysql --basedir=$PWD/$mysqlbuild --datadir=$PWD/$mysqlbuild/data"
155 156 157
    if [ $? -ne 0 ] ; then exit 1; fi
else
    # create link
158
    rm /usr/local/mysql
159 160
    ln -s $mysqldir /usr/local/mysql
    if [ $? -ne 0 ] ; then exit 1; fi
161
    rm /usr/local/$mysqlbuild
162 163
    ln -s $mysqldir /usr/local/$mysqlbuild
    if [ $? -ne 0 ] ; then exit 1; fi
164 165 166 167 168 169 170 171 172 173 174 175
fi
popd

# start mysql
if [ $startup -ne 0 ] ; then 
    ulimit -a
    # increase the open file limit
    ulimit -n 10240
    exitcode=$?
    echo ulimit -n 10240 exitcode $exitcode

    if [ -x /etc/init.d/mysql ] ; then
176
        $sudo setsid /etc/init.d/mysql start
177
    else
178 179 180 181 182
        if [ -z "$defaultsfile" ] ; then
            default_arg=""
        else
            default_arg="--defaults-file=$defaultsfile"
        fi
183
        $sudo -b bash -c "$ldpath /usr/local/mysql/bin/mysqld_safe $default_arg $mysqld_args" >/dev/null 2>&1 &
184
    fi
185
    sleep $sleeptime
186 187 188 189 190 191 192 193 194

    # add mysql grants
    /usr/local/mysql/bin/mysql -u root -e "grant all on *.* to tokubuild@localhost"
    /usr/local/mysql/bin/mysql -u root -e "grant all on *.* to 'ec2-user'@localhost"
fi

popd

exit 0