|
7 | 7 | echo "[cont-init.d] $(basename $0): $*"
|
8 | 8 | }
|
9 | 9 |
|
| 10 | +start_db() { |
| 11 | + log "Starting database..." |
| 12 | + |
| 13 | + # Start mysqld. |
| 14 | + CUR_PWD="$(pwd)" |
| 15 | + cd /etc/services.d/mysqld |
| 16 | + ./run & |
| 17 | + pid="$!" |
| 18 | + cd "$CUR_PWD" |
| 19 | + |
| 20 | + # Wait until it is ready. |
| 21 | + for i in $(seq 1 30); do |
| 22 | + if /etc/services.d/mysqld/data/check; then |
| 23 | + break |
| 24 | + fi |
| 25 | + sleep 1 |
| 26 | + done |
| 27 | + |
| 28 | + if ! /etc/services.d/mysqld/data/check; then |
| 29 | + log "ERROR: Failed to start the database." |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +stop_db() { |
| 35 | + # Kill mysqld. |
| 36 | + log "Shutting down database..." |
| 37 | + if ! kill -s TERM "$pid" || ! wait "$pid"; then |
| 38 | + log "ERROR: initialization failed." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
10 | 43 | # Make sure mandatory directories exist.
|
11 | 44 | mkdir -p \
|
12 | 45 | /config/log/nginx \
|
@@ -43,60 +76,50 @@ if [ -d /config/mysql ] && [ -f /config/db_init_in_progress ]; then
|
43 | 76 | rm -r /config/mysql
|
44 | 77 | fi
|
45 | 78 |
|
46 |
| -# Initialize the database data directory. |
| 79 | +# Create the database directory if required. |
47 | 80 | if [ ! -d /config/mysql ]; then
|
48 |
| - MYSQL_DATABASE=nginxproxymanager |
49 |
| - MYSQL_USER=nginxproxymanager |
50 |
| - MYSQL_PASSWORD=password123 |
51 |
| - |
52 | 81 | touch /config/db_init_in_progress
|
53 | 82 |
|
54 | 83 | log "Initializing database data directory..."
|
55 | 84 | mysql_install_db --datadir=/config/mysql >/config/log/init_db.log 2>&1
|
| 85 | + chown -R $USER_ID:$GROUP_ID /config/mysql |
56 | 86 | log "Database data directory initialized."
|
| 87 | +fi |
57 | 88 |
|
58 |
| - log "Starting database to perform its intialization..." |
59 |
| - |
60 |
| - # Start mysqld. |
61 |
| - chown -R "$USER_ID":"$GROUP_ID" /config/mysql |
62 |
| - /etc/services.d/mysqld/run >>/config/log/init_db.log 2>&1 & |
63 |
| - rc="$?" |
64 |
| - pid="$!" |
65 |
| - if [ "$rc" -ne 0 ]; then |
66 |
| - log "ERROR: Failed to start the database." |
67 |
| - exit 1 |
68 |
| - fi |
| 89 | +# Temporarily start the database. |
| 90 | +start_db |
69 | 91 |
|
70 |
| - # Wait until it is ready. |
71 |
| - for i in $(seq 1 30); do |
72 |
| - if /etc/services.d/mysqld/data/check; then |
73 |
| - break |
74 |
| - fi |
75 |
| - sleep 1 |
76 |
| - done |
| 92 | +# Initialize the database if required. |
| 93 | +if [ -f /config/db_init_in_progress ]; then |
| 94 | + MYSQL_DATABASE=nginxproxymanager |
| 95 | + MYSQL_USER=nginxproxymanager |
| 96 | + MYSQL_PASSWORD=password123 |
77 | 97 |
|
78 | 98 | # Secure the installation.
|
79 | 99 | log "Securing database installation..."
|
80 | 100 | printf '\nn\n\n\n\n\n' | /usr/bin/mysql_secure_installation >>/config/log/init_db.log 2>&1
|
81 | 101 |
|
82 | 102 | log "Initializing database ..."
|
| 103 | + |
83 | 104 | # Create the database.
|
84 | 105 | echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | mysql >>/config/log/init_db.log 2>&1
|
85 | 106 | # Create the user.
|
86 | 107 | echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | mysql >>/config/log/init_db.log 2>&1
|
87 | 108 | echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | mysql >>/config/log/init_db.log 2>&1
|
| 109 | +fi |
88 | 110 |
|
89 |
| - # Kill mysqld. |
90 |
| - log "Shutting down database..." |
91 |
| - if ! kill -s TERM "$pid" || ! wait "$pid"; then |
92 |
| - log "ERROR: initialization failed." |
93 |
| - exit 1 |
94 |
| - fi |
| 111 | +# Make sure to keep the database upgraded. |
| 112 | +if [ ! -f /config/db_init_in_progress ]; then |
| 113 | + log "Upgrading database if required..." |
| 114 | + /usr/bin/mysql_upgrade --silent |
95 | 115 | fi
|
96 | 116 |
|
97 | 117 | # Database initialized properly.
|
98 | 118 | rm -f /config/db_init_in_progress
|
99 | 119 |
|
| 120 | +# Stop the database. |
| 121 | +stop_db |
| 122 | + |
100 | 123 | # Generate dummy self-signed certificate.
|
101 | 124 | if [ ! -f /config/nginx/dummycert.pem ] || [ ! -f /config/nginx/dummykey.pem ]
|
102 | 125 | then
|
|
0 commit comments