1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/bin/sh
set -eu
[ $# -ge 2 ] || {
echo "Usage: debian/setup-mysql.sh port data-dir" >&2
exit 1
}
# CLI arguments #
port=$1
datadir=$2
action=${3:-start}
# Some vars #
socket=$datadir/mysql.sock
# Commands:
mysqladmin="mysqladmin -u root -P $port -h localhost --socket=$socket"
mysqld="/usr/sbin/mysqld --no-defaults --bind-address=localhost --port=$port --socket=$socket --datadir=$datadir"
# Main code #
if [ "$action" = "stop" ]; then
$mysqladmin shutdown
exit
fi
rm -rf $datadir
mkdir -p $datadir
chmod go-rx $datadir
mysql_install_db --datadir=$datadir --rpm --force >> $datadir/bootstrap.log 2>&1
tmpf=$(mktemp)
cat > "$tmpf" <<EOF
USE mysql;
UPDATE user SET password=PASSWORD('') WHERE user='root';
FLUSH PRIVILEGES;
EOF
$mysqld --bootstrap --skip-grant-tables < "$tmpf" >> $datadir/bootstrap.log 2>&1
unlink "$tmpf"
# Start the daemon
$mysqld > $datadir/run.log 2>&1 &
pid=$!
# wait for the server to be actually available
c=0;
while ! nc -z localhost $port; do
c=$(($c+1));
sleep 3;
if [ $c -gt 20 ]; then
echo "Timed out waiting for mysql server to be available" >&2
if [ "$pid" ]; then
kill $pid || :
sleep 2
kill -s KILL $pid || :
fi
exit 1
fi
done
$mysqladmin create test
|