feat : db kredensial fallback

This commit is contained in:
unknown
2026-06-12 23:04:30 +07:00
parent d7dd18ddba
commit bb883473e4
2 changed files with 202 additions and 9 deletions
+14 -9
View File
@@ -34,17 +34,22 @@ try {
// 1. Coba koneksi utama sesuai konfigurasi Environment / Env File
$conn = new mysqli($host, $username, $password, $database, (int)$port);
} catch (Exception $e) {
// 2. Coba koneksi Fallback ke Docker Host (host.docker.internal)
// 2. Coba koneksi Fallback Pertama: Backup Kredensial Keras Database Coolify (Internal Network)
try {
$conn = new mysqli("host.docker.internal", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_docker) {
// 3. Coba koneksi Fallback ke Localhost murni
$conn = new mysqli("tk4ksw0w40kg080wkk4soww4", "mysql", "KBw0yKLccMMoBOZJCf8WDHAO93uI65om0s4ajnzQEt5mkuh0NaIytoWb7TgR5ciq", "default", 3306);
} catch (Exception $e_backup) {
// 3. Coba koneksi Fallback Kedua ke Docker Host lokal (host.docker.internal)
try {
$conn = new mysqli("localhost", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_local) {
// Jika semua gagal, berikan respon error
sendJSON('error', 'Koneksi database utama & fallback gagal: ' . $e_local->getMessage());
exit();
$conn = new mysqli("host.docker.internal", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_docker) {
// 4. Coba koneksi Fallback Ketiga ke Localhost murni (XAMPP default)
try {
$conn = new mysqli("localhost", "root", "", "pemetaan_kemiskinan", 3306);
} catch (Exception $e_local) {
// Jika semua gagal, berikan respon error
sendJSON('error', 'Koneksi database utama & seluruh fallback gagal. Error Utama: ' . $e->getMessage() . ' | Error Backup: ' . $e_backup->getMessage());
exit();
}
}
}
}
+188
View File
@@ -0,0 +1,188 @@
<?php
// ========================================
// FILE: php/test_koneksi.php
// Alat Tes Koneksi Database Independen
// ========================================
// Set content type to HTML
header('Content-Type: text/html; charset=utf-8');
$status = null;
$message = null;
// Mengambil input post jika ada
$host = isset($_POST['host']) ? trim($_POST['host']) : '';
$user = isset($_POST['user']) ? trim($_POST['user']) : '';
$pass = isset($_POST['pass']) ? $_POST['pass'] : '';
$db = isset($_POST['db']) ? trim($_POST['db']) : '';
$port = isset($_POST['port']) ? trim($_POST['port']) : '3306';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Aktifkan error reporting untuk mysqli agar kita mendapat error detail
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$start_time = microtime(true);
// Coba hubungkan ke MySQL
$conn = new mysqli($host, $user, $pass, $db, (int)$port);
$end_time = microtime(true);
$execution_time = round(($end_time - $start_time) * 1000, 2);
$status = 'success';
$message = "Koneksi Berhasil! Terhubung ke server MySQL versi " . $conn->server_info . " dalam " . $execution_time . " ms.";
$conn->close();
} catch (Exception $e) {
$status = 'error';
$message = "Koneksi Gagal: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MySQL Connection Tester</title>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Outfit', sans-serif;
background-color: #0f172a;
color: #f1f5f9;
margin: 0;
padding: 40px 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
box-sizing: border-box;
}
.card {
width: 100%;
max-width: 500px;
background: rgba(30, 41, 59, 0.7);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 30px;
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
backdrop-filter: blur(10px);
}
h2 {
margin-top: 0;
color: #38bdf8;
font-weight: 700;
text-align: center;
border-bottom: 2px solid rgba(56, 189, 248, 0.2);
padding-bottom: 15px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 6px;
font-weight: 600;
color: #94a3b8;
font-size: 14px;
}
input {
width: 100%;
padding: 10px;
border: 1px solid rgba(255,255,255,0.1);
border-radius: 8px;
background: #0f172a;
color: white;
box-sizing: border-box;
font-size: 14px;
}
input:focus {
outline: none;
border-color: #38bdf8;
}
.btn {
width: 100%;
background: linear-gradient(135deg, #38bdf8 0%, #0284c7 100%);
color: white;
border: none;
padding: 12px;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
font-size: 15px;
transition: 0.2s;
margin-top: 10px;
}
.btn:hover {
transform: translateY(-1px);
box-shadow: 0 4px 15px rgba(56, 189, 248, 0.4);
}
.alert {
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
font-size: 14px;
line-height: 1.5;
}
.alert-success {
background-color: #dcfce3;
color: #166534;
border-left: 5px solid #22c55e;
}
.alert-error {
background-color: #fee2e2;
color: #991b1b;
border-left: 5px solid #ef4444;
}
</style>
</head>
<body>
<div class="card">
<h2>🔌 MySQL Connection Tester</h2>
<?php if ($status !== null): ?>
<div class="alert alert-<?php echo $status; ?>">
<strong><?php echo $status === 'success' ? '✔ Sukses' : '❌ Gagal'; ?>:</strong>
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<form method="POST" action="">
<div class="form-group">
<label for="host">MySQL Host / IP Address</label>
<input type="text" id="host" name="host" value="<?php echo htmlspecialchars($host); ?>" placeholder="Contoh: 103.xxx.xx.xx atau localhost" required>
</div>
<div class="form-group" style="display: flex; gap: 15px;">
<div style="flex: 2;">
<label for="user">Username</label>
<input type="text" id="user" name="user" value="<?php echo htmlspecialchars($user); ?>" placeholder="Contoh: root" required>
</div>
<div style="flex: 1;">
<label for="port">Port</label>
<input type="text" id="port" name="port" value="<?php echo htmlspecialchars($port); ?>" placeholder="3306" required>
</div>
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" id="pass" name="pass" value="<?php echo htmlspecialchars($pass); ?>" placeholder="Kosongkan jika tidak ada">
</div>
<div class="form-group">
<label for="db">Nama Database</label>
<input type="text" id="db" name="db" value="<?php echo htmlspecialchars($db); ?>" placeholder="Contoh: pemetaan_kemiskinan" required>
</div>
<button type="submit" class="btn">Test Koneksi ⚡</button>
</form>
<p style="text-align: center; margin-top: 20px; margin-bottom: 0; font-size: 13px;">
<a href="../index.html" style="color: #38bdf8; text-decoration: none;">&larr; Kembali ke Portal WebGIS</a>
</p>
</div>
</body>
</html>