File: setup-mysql.sh

package info (click to toggle)
ruby-moneta 0.7.20-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,768 kB
  • ctags: 1,390
  • sloc: ruby: 7,405; sh: 116; makefile: 5
file content (104 lines) | stat: -rwxr-xr-x 2,305 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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}
if [ "$(id -u)" -eq 0 ]; then
    user="mysql"
else
    user="$(whoami)"
fi

# Some vars #

socket=$datadir/mysql.sock
# Commands:
mysqladmin="mysqladmin --no-defaults --user root --port $port --host 127.0.0.1 --socket=$socket --no-beep"
mysqld="/usr/sbin/mysqld --no-defaults --user=$user --bind-address=127.0.0.1 --port=$port --socket=$socket --datadir=$datadir"

# Main code #

if [ "$action" = "stop" ]; then
    if $mysqladmin ping 2>/dev/null; then
        pid=$(lsof -Fp -n -i TCP:$port -s TCP:LISTEN | sed -n -s 's/^p//p')
        if [ -z "$pid" ]; then
            echo "Unable to find MySQL port" >&2
            exit 1
        fi
        $mysqladmin shutdown || true
        for i in 1 2 3 4 5 6 7 8 9 10; do
            sleep 1
            if ! $mysqladmin ping 2>/dev/null; then
                exit
            fi
        done
        echo "Killing MySQL database server by signal" >&2
        kill -15 $pid
        for i in 1 2 3 4 5 6 7 8 9 10; do
            sleep 1
            if ! $mysqladmin ping 2>/dev/null; then
                exit
            fi
        done
        kill -9 $pid
    fi
    exit
fi

rm -rf $datadir
mkdir -p $datadir
chmod go-rx $datadir
chown $user: $datadir

mysql_install_db --no-defaults --user=$user --datadir=$datadir --rpm --force

tmpf=$(mktemp)
cat > "$tmpf" <<EOF
USE mysql;
UPDATE user SET password=PASSWORD('') WHERE user='root';
UPDATE user SET plugin='' WHERE user='root';
FLUSH PRIVILEGES;
EOF

$mysqld --bootstrap --skip-grant-tables < "$tmpf"

unlink "$tmpf"

# Start the daemon
$mysqld &

pid=$!

# Wait for the server to be actually available
c=0;
while ! nc -z 127.0.0.1 $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

# Check if the server is running
$mysqladmin status

for db in moneta moneta_activerecord1 moneta_activerecord2; do
    # Drop the database if it exists
    $mysqladmin --force --silent drop $db || true
    # Create new empty databases
    $mysqladmin create $db
done