操作失败。检查用户是否存在。
你的PHP应用在执行用户管理操作时遇到了问题:
'pdo_code' => 'HY000',
'db_code' => 1396,
'db_error' => 'Operation CREATE USER failed for 'user'@'host''
function createMySQLUserSafe($pdo, $username, $password, $host = 'localhost') {
try {
$stmt = $pdo->prepare("SELECT COUNT(*) FROM mysql.user WHERE User = ? AND Host = ?");
$stmt->execute([$username, $host]);
$userExists = $stmt->fetchColumn() > 0;
if ($userExists) {
$dropStmt = $pdo->prepare("DROP USER ?@?");
$dropStmt->execute([$username, $host]);
}
$createStmt = $pdo->prepare("CREATE USER ?@? IDENTIFIED BY ?");
$createStmt->execute([$username, $host, $password]);
$grantStmt = $pdo->prepare("GRANT USAGE ON *.* TO ?@?");
$grantStmt->execute([$username, $host]);
return true;
} catch (PDOException $e) {
if ($e->getCode() == '1396') {
error_log("用户创建失败: " . $e->getMessage());
return false;
} else {
throw $e;
}
}
}