Initialize database from app service

This commit is contained in:
2026-06-11 14:00:34 +07:00
parent bfd6b1d8cf
commit 1b2f57bd49
3 changed files with 108 additions and 8 deletions
+106
View File
@@ -0,0 +1,106 @@
<?php
mysqli_report(MYSQLI_REPORT_OFF);
$host = getenv('DB_HOST') ?: 'db';
$port = (int)(getenv('DB_PORT') ?: 3306);
$rootPass = getenv('DB_ROOT_PASSWORD') ?: 'rootwebgis';
$appUser = getenv('DB_USER') ?: 'webgis';
$appPass = getenv('DB_PASSWORD') ?: 'webgis';
function log_line($message) {
echo "[initdb] " . $message . PHP_EOL;
}
function connect_mysql($host, $user, $pass, $db = null, $port = 3306) {
$conn = mysqli_init();
$conn->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
if (@$conn->real_connect($host, $user, $pass, $db, $port)) {
$conn->set_charset('utf8mb4');
return $conn;
}
return null;
}
$conn = null;
$lastError = '';
for ($i = 1; $i <= 90; $i++) {
$conn = connect_mysql($host, 'root', $rootPass, null, $port);
if ($conn) {
break;
}
$lastError = mysqli_connect_error();
log_line("Menunggu database siap ($i/90): " . ($lastError ?: 'belum siap'));
sleep(2);
}
if (!$conn) {
log_line("Database tidak bisa dihubungi. Host=$host Port=$port Error=$lastError");
exit(1);
}
log_line('Database siap.');
$safeUser = $conn->real_escape_string($appUser);
$safePass = $conn->real_escape_string($appPass);
$conn->query("CREATE USER IF NOT EXISTS '{$safeUser}'@'%' IDENTIFIED BY '{$safePass}'");
$conn->query("GRANT ALL PRIVILEGES ON *.* TO '{$safeUser}'@'%'");
$conn->query("FLUSH PRIVILEGES");
function table_exists($conn, $db, $table) {
$db = $conn->real_escape_string($db);
$table = $conn->real_escape_string($table);
$result = $conn->query("SHOW TABLES FROM `{$db}` LIKE '{$table}'");
return $result && $result->num_rows > 0;
}
function import_sql_file($conn, $path, $label) {
if (!is_file($path)) {
log_line("Lewati {$label}: file tidak ditemukan {$path}");
return;
}
$sql = file_get_contents($path);
if ($sql === false || trim($sql) === '') {
log_line("Lewati {$label}: file kosong");
return;
}
log_line("Import {$label} dari {$path}");
if (!$conn->multi_query($sql)) {
log_line("Import {$label} gagal: " . $conn->error);
exit(1);
}
do {
if ($result = $conn->store_result()) {
$result->free();
}
if ($conn->errno) {
log_line("Import {$label} gagal: " . $conn->error);
exit(1);
}
} while ($conn->more_results() && $conn->next_result());
log_line("Import {$label} selesai.");
}
if (!table_exists($conn, 'spbu_db', 'spbu')) {
import_sql_file($conn, '/var/www/html/01/spbu_db.sql', 'spbu_db');
} else {
log_line('spbu_db sudah ada.');
}
if (!table_exists($conn, 'webgis2', 'jalan')) {
import_sql_file($conn, '/var/www/html/02/webgis2.sql', 'webgis2');
} else {
log_line('webgis2 sudah ada.');
}
if (!table_exists($conn, 'webgis_uas', 'rumah_ibadah')) {
import_sql_file($conn, '/var/www/html/05/webgis_uas.sql', 'webgis_uas');
} else {
log_line('webgis_uas sudah ada.');
}
log_line('Inisialisasi database selesai.');
-3
View File
@@ -1,3 +0,0 @@
CREATE USER IF NOT EXISTS 'webgis'@'%' IDENTIFIED BY 'webgis';
GRANT ALL PRIVILEGES ON *.* TO 'webgis'@'%';
FLUSH PRIVILEGES;