Last active
August 29, 2015 14:05
-
-
Save jasonwbarnett/cdfcde49d8bc19f934f4 to your computer and use it in GitHub Desktop.
This can be used to install MySQL Server from the bin distribution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Jason Barnett <J@sonBarnett.com> | |
# | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2014 Jason Barnett | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# Helpful resources: | |
# | |
# - http://dev.mysql.com/doc/refman/5.1/en/binary-installation.html | |
# - http://dev.mysql.com/doc/refman/5.1/en/mysql-install-db.html | |
# - http://dev.mysql.com/doc/refman/5.1/en/environment-variables.html | |
# - http://dev.mysql.com/doc/refman/5.1/en/mysqld.html | |
# - http://dev.mysql.com/doc/refman/5.1/en/mysql-secure-installation.html | |
# | |
TIP_HOME=/opt/app/workload/tip | |
MYSQL_USER=tipdbin | |
MYSQL_TAR=${TIP_HOME}/mysql-5.1.73-linux-x86_64-glibc23.tar.gz | |
MYSQL_HOME=${TIP_HOME}/mysql | |
MYSQL_LOG=${MYSQL_HOME}/log | |
MYSQL_DATA=${MYSQL_HOME}/data | |
MYSQL_SCRIPTS=${MYSQL_HOME}/scripts | |
MYSQL_BIN=${MYSQL_HOME}/bin | |
MYSQL_MYCNF=${HOME}/.my.cnf | |
TMP_F_SEROPTS=${HOME}/.tmp.server_opts | |
TMP_F_SOCK=${HOME}/.tmp.socket | |
TMP_F_SECURE=${HOME}/.tmp.secure_patch | |
export PATH=${MYSQL_BIN}:$PATH | |
if [ "$(whoami)" != "${MYSQL_USER}" ]; then | |
echo "This script must be run as the MySQL User: ${MYSQL_USER}" 1>&2 | |
exit 1 | |
fi | |
## Add to user's profile | |
echo "PATH=${MYSQL_BIN}:\$PATH" >> ${HOME}/.bashrc | |
trap "interrupt" 1 2 3 6 15 | |
interrupt() { | |
echo | |
echo "Aborting!" | |
echo | |
cleanup | |
stty echo | |
exit 1 | |
} | |
cleanup() { | |
echo "Cleaning up..." | |
rm -f ${TMP_F_SEROPTS} ${TMP_F_SOCK} ${TMP_F_SECURE} | |
} | |
cd ${TIP_HOME} | |
## Make sure the needed directories get created | |
[[ -d ${MYSQL_HOME} ]] || mkdir ${MYSQL_HOME} | |
[[ -d ${MYSQL_LOG} ]] || mkdir ${MYSQL_LOG} | |
[[ -d ${MYSQL_DATA} ]] || mkdir ${MYSQL_DATA} | |
## Extract mysql bin install | |
tar -zxvf ${MYSQL_TAR} -C ${MYSQL_HOME} --strip-components 1 | |
## Run mysql_install_db | |
${MYSQL_SCRIPTS}/mysql_install_db --user=${MYSQL_USER} \ | |
--basedir=${MYSQL_HOME} \ | |
--datadir=${MYSQL_DATA} | |
## Move the my.cnf in place | |
cp ${MYSQL_HOME}/support-files/my-innodb-heavy-4G.cnf ${MYSQL_MYCNF} | |
## Update my.cnf with the right defaults | |
### Setup templates for inserting with sed | |
cat > ${TMP_F_SOCK} <<EOF | |
socket = ${MYSQL_HOME}/mysql.sock | |
EOF | |
cat > ${TMP_F_SEROPTS} <<EOF | |
basedir = ${MYSQL_HOME} | |
datadir = ${MYSQL_DATA} | |
pid-file = ${MYSQL_HOME}/mysqld.pid | |
log-error = ${MYSQL_LOG}/mysqld.log | |
EOF | |
cat > ${TMP_F_SECURE} <<EOF | |
echo "socket=${MYSQL_HOME}/mysql.sock" >>\$config | |
EOF | |
### Remove known attributes that will be redifined later | |
sed -r -i '/^(basedir|datadir|pid-file|log-error|socket)/d' ${MYSQL_MYCNF} | |
### Insert the templates using sed | |
mysqld_lineno=$(egrep -n '\[mysqld\]' ${MYSQL_MYCNF} | awk -F: '{print $1}') | |
sed -i "${mysqld_lineno}r ${TMP_F_SEROPTS}" ${MYSQL_MYCNF} | |
sed -i "${mysqld_lineno}r ${TMP_F_SOCK}" ${MYSQL_MYCNF} | |
client_lineno=$(egrep -n '\[client\]' ${MYSQL_MYCNF} | awk -F: '{print $1}') | |
sed -i "${client_lineno}r ${TMP_F_SOCK}" ${MYSQL_MYCNF} | |
### Change default storage engine to innodb | |
sed -i 's|default-storage-engine = MYISAM|default-storage-engine = innodb|g' ${MYSQL_MYCNF} | |
## Run mysql_secure_installation | |
### First we need to patch the bin so it works with our custom install | |
bottom_lineno=$(egrep -n 'make_config\(\) {|^}' ${MYSQL_BIN}/mysql_secure_installation | egrep -A1 'make_config\(\)' | tail -n1 | awk -F: '{print $1}') | |
bottom_lineno=$((bottom_lineno - 1)) | |
sed -i "${bottom_lineno}r ${TMP_F_SECURE}" ${MYSQL_BIN}/mysql_secure_installation | |
### Start MySQL | |
cd "${MYSQL_HOME}" | |
${MYSQL_BIN}/mysqld_safe --defaults-file=${MYSQL_MYCNF} \ | |
--datadir=${MYSQL_DATA} --socket=${MYSQL_HOME}/mysql.sock \ | |
--pid-file=${MYSQL_HOME}/mysqld.pid --basedir=${MYSQL_HOME} \ | |
--user=${MYSQL_USER} >/dev/null 2>&1 & | |
START_CMD=$(cat <<EOF | |
cd "${MYSQL_HOME}" | |
${MYSQL_BIN}/mysqld_safe --defaults-file=${MYSQL_MYCNF} \\ | |
--datadir=${MYSQL_DATA} \\ | |
--socket=${MYSQL_HOME}/mysql.sock \\ | |
--pid-file=${MYSQL_HOME}/mysqld.pid \\ | |
--basedir=${MYSQL_HOME} \\ | |
--user=${MYSQL_USER} >/dev/null 2>&1 & | |
EOF) | |
echo "Waiting 15 seconds for MySQL to start... " | |
sleep 15 | |
### Now we run the actual mysql_secure_installation | |
random_pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1) | |
${MYSQL_BIN}/mysql_secure_installation <<EOF | |
y | |
${random_pass} | |
${random_pass} | |
y | |
y | |
y | |
y | |
EOF | |
cat >> ${MYSQL_MYCNF} <<EOF | |
[client] | |
user=root | |
password=${random_pass} | |
EOF | |
cleanup | |
echo | |
echo | |
echo | |
echo "All done! MySQL Server is now installed AND running." | |
echo | |
echo "auto generated MySQL root password: ${random_pass}" | |
echo | |
echo "If you need to start MySQL later, you can do so with the following command:" | |
echo | |
echo "${START_CMD}" | |
echo | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment