mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-30 01:10:09 +02:00
72 lines
2.7 KiB
PHP
72 lines
2.7 KiB
PHP
<?php
|
|
|
|
// Database type
|
|
$dbType = 'mysql';
|
|
|
|
// Database credentials
|
|
$host = 'localhost';
|
|
$username = 'your_mysql_username';
|
|
$password = 'your_mysql_password';
|
|
|
|
try {
|
|
// Connect to database
|
|
if ($dbType == 'mysql') {
|
|
$pdo = new PDO("mysql:host=$host", $username, $password);
|
|
} elseif ($dbType == 'postgresql') {
|
|
$pdo = new PDO("pgsql:host=$host", $username, $password);
|
|
} elseif ($dbType == 'sqlite') {
|
|
$pdo = new PDO("sqlite:host=$host");
|
|
}
|
|
|
|
// Set PDO attributes
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// New database details
|
|
$newDatabaseName = 'new_database_name';
|
|
$newDatabaseUsername = 'new_database_username';
|
|
$newDatabasePassword = 'new_database_password';
|
|
|
|
// Create new database
|
|
if ($dbType == 'mysql') {
|
|
$pdo->exec("CREATE DATABASE `$newDatabaseName`");
|
|
} elseif ($dbType == 'postgresql') {
|
|
$pdo->exec("CREATE DATABASE $newDatabaseName");
|
|
} elseif ($dbType == 'sqlite') {
|
|
$pdo->exec("CREATE DATABASE $newDatabaseName");
|
|
}
|
|
echo "Created new database '$newDatabaseName'\n";
|
|
|
|
// Create new user with access to the new database
|
|
if ($dbType == 'mysql') {
|
|
$pdo->exec("CREATE USER '$newDatabaseUsername'@'localhost' IDENTIFIED BY '$newDatabasePassword'");
|
|
$pdo->exec("GRANT ALL PRIVILEGES ON `$newDatabaseName`.* TO '$newDatabaseUsername'@'localhost'");
|
|
} elseif ($dbType == 'postgresql') {
|
|
$pdo->exec("CREATE USER $newDatabaseUsername WITH PASSWORD '$newDatabasePassword'");
|
|
$pdo->exec("GRANT ALL PRIVILEGES ON DATABASE $newDatabaseName TO $newDatabaseUsername");
|
|
} elseif ($dbType == 'sqlite') {
|
|
// SQLite doesn't have users and privileges, so skip this step
|
|
}
|
|
echo "Created new user '$newDatabaseUsername'\n";
|
|
echo "Granted all privileges to user '$newDatabaseUsername' on database '$newDatabaseName'\n";
|
|
|
|
// Connect to the new database as the new user
|
|
if ($dbType == 'mysql') {
|
|
$pdo = new PDO("mysql:host=$host;dbname=$newDatabaseName", $newDatabaseUsername, $newDatabasePassword);
|
|
} elseif ($dbType == 'postgresql') {
|
|
$pdo = new PDO("pgsql:host=$host;dbname=$newDatabaseName", $newDatabaseUsername, $newDatabasePassword);
|
|
} elseif ($dbType == 'sqlite') {
|
|
$pdo = new PDO("sqlite:$newDatabaseName");
|
|
}
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Path to SQL file to import
|
|
$sqlFile = '/path/to/sql/file.sql';
|
|
|
|
// Import SQL file
|
|
$sql = file_get_contents($sqlFile);
|
|
$pdo->exec($sql);
|
|
echo "Imported SQL file '$sqlFile' into database '$newDatabaseName'\n";
|
|
|
|
} catch (PDOException $e) {
|
|
echo $e->getMessage();
|
|
}
|