Skip to content

Instantly share code, notes, and snippets.

@fjakobs
Last active December 4, 2018 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjakobs/5502604 to your computer and use it in GitHub Desktop.
Save fjakobs/5502604 to your computer and use it in GitHub Desktop.
#!/bin/bash
if ! [ $# -eq 1 ]
then
echo "Usage: $0 [start|restart|stop|status|cli|install]"
exit 1
fi
export STOPTIMEOUT=10
MYSQL_DIR="$HOME/lib/mysql"
MYSQL_IP=$IP
isrunning() {
if [ -f $MYSQL_DIR/pid/mysql.pid ]; then
mysql_pid=`cat $MYSQL_DIR/pid/mysql.pid 2> /dev/null`
myid=`id -u`
if `ps --pid $mysql_pid > /dev/null 2>&1` || `pgrep -x mysqld_safe -u $myid > /dev/null 2>&1`
then
return 0
fi
fi
return 1
}
isinstalled() {
if [ -f $MYSQL_DIR/etc/my.cnf ]; then
return 0
fi
return 1
}
start() {
if ! isinstalled
then
install
fi
if ! isrunning
then
/usr/bin/mysqld_safe --defaults-file=$MYSQL_DIR/etc/my.cnf >/dev/null 2>&1 &
else
echo "MySQL already running" 1>&2
fi
}
stop() {
if [ -f $MYSQL_DIR/pid/mysql.pid ]; then
pid=$( /bin/cat $MYSQL_DIR/pid/mysql.pid )
/bin/kill $pid
ret=$?
if [ $ret -eq 0 ]; then
TIMEOUT="$STOPTIMEOUT"
while [ $TIMEOUT -gt 0 ] && [ -f "$MYSQL_DIR/pid/mysql.pid" ]; do
/bin/kill -0 "$pid" >/dev/null 2>&1 || break
sleep 1
let TIMEOUT=${TIMEOUT}-1
done
fi
else
if `pgrep -x mysqld_safe > /dev/null 2>&1`
then
echo "Warning: MySQL process exists without a pid file. Use force-stop to kill." 1>&2
else
echo "MySQL already stopped" 1>&2
fi
fi
}
function wait_to_start_db {
i=0
while (( ! echo ";" | mysql -u root -S "${MYSQL_DIR}/socket/mysql.sock" > /dev/null 2>&1) || [ ! -f ${MYSQL_DIR}/pid/mysql.pid ]) && [ $i -lt 30 ]
do
sleep 1
i=$(($i + 1))
done
}
create_mysql_dir() {
rm -rf $MYSQL_DIR
mkdir -p $MYSQL_DIR
cd $MYSQL_DIR
mkdir -p log etc data socket pid run
}
write_config() {
# generate config file
(
cat <<EOF
[mysqld]
datadir=$MYSQL_DIR/data/
socket=$MYSQL_DIR/socket/mysql.sock
bind-address=$MYSQL_IP
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
key_buffer_size = 16K
max_allowed_packet = 200M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 256K
#INNODB
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqld_safe]
log-error=$MYSQL_DIR/log/mysql_error.log
pid-file=$MYSQL_DIR/pid/mysql.pid
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout
EOF
) > $MYSQL_DIR/etc/my.cnf
}
install() {
stop
create_mysql_dir
write_config
mysql_install_db --defaults-file=$MYSQL_DIR/etc/my.cnf
/usr/bin/mysqld_safe --defaults-file=$MYSQL_DIR/etc/my.cnf >/dev/null 2>&1 &
wait_to_start_db
username=$C9_USER
dbname=c9
echo "drop database test;
create database \`${dbname}\` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" | mysql -u root -S "${MYSQL_DIR}/socket/mysql.sock" > /dev/null || error "Failed to create ${application}" 188
echo "
delete from user;
grant all on *.* to '$username'@'$MYSQL_IP' with grant option;
grant all on *.* to '$username'@'localhost' with grant option;
flush privileges;" | mysql -u root -S "${MYSQL_DIR}/socket/mysql.sock" mysql > /dev/null || error "Failed to setup initial root user" 187
echo ""
echo "MySQL 5.1 database added. Please make note of these credentials:"
echo ""
echo " Root User: $username"
echo " Database Name: ${dbname}"
echo ""
}
cli() {
if ! isrunning
then
echo "Starting MySQL ..."
start
wait_to_start_db
fi
mysql -h $IP -u $C9_USER
}
case "$1" in
start)
start
;;
stop)
stop
;;
install)
install
;;
restart)
stop
start
;;
cli)
cli
;;
status)
if isrunning
then
echo "MySQL is running" 1>&2
else
echo "MySQL is stopped" 1>&2
fi
exit 0
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment