Hej i gruppen.
Koden skal lave x antal tomme databaser til Joomla! udvikling.
Jeg kører følgende kode der skal sætte Bruger og Kodeord på en database, efter databasen er blevet skabt:
$sql= "CREATE USER " . "'" . $LocalUsername . "'@'" . $servername . "'" . " IDENTIFIED BY " . "'" . $LocalPassword . "'";
if ($conn->query($sql) === TRUE) {
echo "\n\n USER and PASSWORD created successfully \n\n";
} else {
die ("\n\n Error creating USER and PASSWORD: " . $conn->error . "\n\n");
Jeg får følgende fejl:
Error creating USER and PASSWORD: Operation CREATE USER failed for 'prefix0'@'localhost'
Hvor ligger fejlen? og hvordan udbedrer man den?
Har inspiration fra:
http://dev.mysql.com/doc/refman/5.5/en/create-user.html med MySQL 5.5
Bruger Ubuntu Server 14.04.2
Mvh,
Thomas
Her Følger hele koden, som jeg kører med:
"sudo php -f generate_databases_in_php.php"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Database creation in PHP</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "MySQL root";
$password = "Indsaet MySQL root password her";
$startDB= 0;
$endDB= 2;
$PrefixDB="prefix";
$spreadsheetCSV="databases.csv";
// Open speadsheet for append.
$csvFile = fopen($spreadsheetCSV, "a");
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("\n Connection open failed: \n" . $conn->connect_error);
}
for($LocalDB = $startDB; $LocalDB <= $endDB; $LocalDB++)
{
//$LocalDB=system("mkpasswd 'date +%N' ");
$DatabaseName= $PrefixDB . $LocalDB;
// Create DATABASE
$sql= "CREATE DATABASE " . $DatabaseName ;
if ($conn->query($sql) === TRUE) {
echo "\n\n DATABASE created successfully \n\n";
} else {
die ("\n\n Error creating DATABASE: " . $conn->error . "\n\n");
}
//$LocalUsername = substr(system("mkpasswd 'date +%N'"), 0);
$LocalUsername = $DatabaseName;
$LocalPassword = substr(system("mkpasswd 'date +%N'"), 0);
echo "\n\n Local Password is " . $LocalPassword . "\n\n" ;
// Create USER and PASSWORD
// HER GÅR DET GALT!
$sql= "CREATE USER " . "'" . $LocalUsername . "'@'" . $servername . "'" . " IDENTIFIED BY " . "'" . $LocalPassword . "'";
if ($conn->query($sql) === TRUE) {
echo "\n\n USER and PASSWORD created successfully \n\n";
} else {
die ("\n\n Error creating USER and PASSWORD: " . $conn->error . "\n\n");
}
// GRANT ALL ON LocalDB.* TO USERNAME
$sql = "GRANT ALL ON " . $DatabaseName . ".*" . " TO " . $LocalUsername . "@" . $servername ;
echo "\n\n" . $sql . "\n\n";
if ($conn->query($sql) === TRUE) {
echo "\n\n Granting created successfully \n\n";
} else {
die ("\n\n Error creating GRANTING: " . $conn->error . "\n\n");
}
fputcsv($csvFile, array((string)$LocalDB, $DatabaseName, $LocalUsername, $LocalPassword));
};
// Close database connection
$conn->close();
// Close csv spreadsheet file
fclose($csvFile);
?>
</body>
</html>
Mvh,
Thomas