我正在创建一个数据库和一个新的数据库用户。我正在尝试将所有权限授予该新创建的数据库用户。
但是我失败了:
它没有显示任何错误。我试图使用新的用户名和密码登录。我可以登录,但是看不到数据库,这意味着授予权限不正确。
请参阅我的代码:
$username = "bdsat_boishakhi29";
$password = $username;
$databasename = $username;
$admin_db = "mysql";
$admin_user = "bdsat";
$admin_password = "demo123456";
$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 XXX";
}
$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 Created... <b>$databasename</b> created successfully!</p>";
}
else
{
echo "<br>Error creating database user: " . mysqli_error($connection);
}
// Grant user permissions
$sql="GRANT ALL PRIVILEGES ON $databasename.* TO $username@'localhost' IDENTIFIED BY $password";
$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 XXX";
}
if (mysqli_query($sql))
{
echo "<p>User permissions Created... <b>$databasename</b> created successfully!</p>";
}
else
{
echo "<br>Error granting database permission: " . mysqli_error($connection2);
}
echo "<pre>";
print_r(mysqli_query("SHOW GRANTS FOR $username@'localhost'"));