Create mysqli database from php

0

I'm trying to create a database from php but I can not.

The server I use is a must 8 with plesk

and this is the code with which I am trying:

    <?php
$servername = "localhost";
$username = "DB_Admin";
$password = "$4Fd3rnke";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
    echo "Base de datos creada correctamente";
} else {
    echo "Error creando la base de datos: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

The error you are giving me is this:

Error creando la base de datos: Access denied for user 'DB_Admin'@'localhost' to database 'myDB'

I do not know if I have the fault in the user or it could be something else. The user created it with privileges to create databases.

What can I have wrong?

The user in plesk has it this way:

In the last attempt to give privileges to the user I have put this:

$sql = "CREATE DATABASE myDB GRANT ALL PRIVILEGES ON myDB.* To 'DB_Admin'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;";

But at the moment I get syntax error.

    
asked by Killpe 24.10.2018 в 19:11
source

2 answers

1

In the end it turns out that plesk passes olimpicamente of the permissions of the user. I have changed the user's permissions from phpmyadmin and it already works with the original code.

    
answered by 24.10.2018 / 21:31
source
1

The error message says that the user does not have access to the database, not that he does not have privileges to create databases. Try to give the user privileges once he creates the database:

GRANT ALL PRIVILEGES ON myDB.* To 'DB_Admin'@'localhost';

    
answered by 24.10.2018 в 19:24