创建MySQL数据库与用户后权限授予失败问题求助
Fixed: Grant Permissions Not Taking Effect for New MySQL User
I see exactly what's causing your permission issue—let's fix this up step by step:
The Core Issues in Your Code
Let's break down the problems that are stopping the grants from working:
- Missing quotes in the GRANT statement: When specifying the user and password in your
GRANTquery, you didn't wrap them in single quotes. MySQL treats unquoted values as database/table identifiers, so it wasn't actually applying the permissions to yourbdsat_boishakhi29@localhostuser. - Unnecessary reconnection: You reconnected to the database right before running the
GRANTcommand—this is totally redundant and doesn't help anything. - Broken error handling: When checking for grant errors, you referenced
$connection2which doesn't exist; you should use your existing$connectionvariable instead. - Invalid SHOW GRANTS call: The final
mysqli_queryforSHOW GRANTSdidn't pass the database connection as the first parameter, so it wasn't executing correctly.
Corrected Code
Here's the fixed version with explanations for each change:
<?php $username = "bdsat_boishakhi29"; $password = $username; $databasename = $username; $admin_db = "mysql"; $admin_user = "bdsat"; $admin_password = "demo123456"; // Initial connection (keep this, no need to reconnect later) $connection = mysqli_connect("localhost", $admin_user, $admin_password); if (mysqli_connect_errno()) { echo "<br>Failed to connect to MySQL: " . mysqli_connect_error(); } else { echo "<br>Connected successfully!"; } // Create Database $sql = "CREATE DATABASE $databasename"; if (mysqli_query($connection, $sql)) { echo "<br>Database <b>$databasename</b> created successfully!"; } else { echo "<br>Error creating database: " . mysqli_error($connection); } // Create User $sql = "CREATE USER '" . $username . "'@'localhost' IDENTIFIED BY '" . $password . "'"; if (mysqli_query($connection, $sql)) { echo "<p>User <b>$username</b> created successfully!</p>"; } else { echo "<br>Error creating database user: " . mysqli_error($connection); } // Grant Permissions (fixed quotes around user and password) $sql = "GRANT ALL PRIVILEGES ON $databasename.* TO '" . $username . "'@'localhost' IDENTIFIED BY '" . $password . "'"; if (mysqli_query($connection, $sql)) { echo "<p>Full permissions granted to user <b>$username</b> for database <b>$databasename</b>!</p>"; // Optional: Flush privileges (needed for some older MySQL versions) mysqli_query($connection, "FLUSH PRIVILEGES"); } else { echo "<br>Error granting database permissions: " . mysqli_error($connection); // Fixed variable name } // Show Grants (fixed by passing the connection object) echo "<pre>"; $result = mysqli_query($connection, "SHOW GRANTS FOR '" . $username . "'@'localhost'"); while ($row = mysqli_fetch_array($result)) { print_r($row); } echo "</pre>"; ?>
Key Fixes Explained
- Quoted user/password in GRANT: Wrapping
$username@'localhost'and$passwordin single quotes tells MySQL these are string values, so it correctly applies the permissions to your new user. - Removed redundant reconnection: We use the initial connection throughout the script to avoid any unnecessary state changes.
- Fixed error variable: Changed
$connection2to$connectionso error messages work correctly. - Proper SHOW GRANTS execution: Passed the
$connectionobject tomysqli_queryand added a loop to actually print the grants (your original code just printed the result object, not the grant details). - Optional FLUSH PRIVILEGES: Added this command to ensure MySQL immediately applies the permission changes—this is more critical for older MySQL versions (pre-8.0), but it doesn't hurt to include it.
After running this corrected code, your new user should be able to see and fully access the target database when logging in.
内容的提问来源于stack exchange,提问作者Nazmul Hasan Nero
相关产品推荐
相关产品推荐

