Merge pull request #18 from GameServerPanel/copilot/fix-f2bbb7ee-9cc1-48a5-99b7-b310d6a561e9

Add MySQL database auto-creation for new game servers in billing module
This commit is contained in:
Frank Harris 2025-09-09 13:02:03 -04:00 committed by GitHub
commit 484f6018f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 225 additions and 0 deletions

View file

@ -0,0 +1,56 @@
# MySQL Auto-Create Feature
This feature automatically creates MySQL databases for each new game server created through the billing system.
## Required Settings
Add the following settings to your OGP settings table to enable MySQL auto-creation:
### Required Settings:
- `mysql_auto_create` - Set to '1' to enable, '0' to disable
- `mysql_root_user` - MySQL root username for creating databases (e.g., 'remoteuser')
- `mysql_root_password` - MySQL root password
- `mysql_host` - MySQL server hostname (e.g., 'mysql.iaregamer.com')
### Optional Settings:
- `mysql_port` - MySQL server port (defaults to '3306' if not set)
- `mysql_special_user` - Additional user to grant access (like 'dayzhivemind' in original script)
- `mysql_special_password` - Password for the special user
- `mysql_init_sql_file` - Path to SQL file to import into new databases (e.g., '1.9.0_fresh.sql')
- `mysql_default_server_id` - MySQL server ID from mysql_servers table to track databases in OGP
## How it works:
1. When a new server is created, the system generates:
- Database name: `server_<home_id>` (e.g., `server_1745`)
- Database user: Same as database name (e.g., `server_1745`)
- Random 12-character password
2. The system creates the database and grants privileges:
- Full privileges to the database user from localhost and any host (%)
- If mysql_special_user is set, grants full privileges to that user too
- Flushes privileges
3. If mysql_init_sql_file is specified, imports that SQL file into the new database
4. If mysql_default_server_id is set, adds the database to OGP's mysql_databases table for tracking
## Example Settings SQL:
```sql
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_auto_create', '1');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_user', 'remoteuser');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_password', 'Pkloyn7yvpht!');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_host', 'mysql.iaregamer.com');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_port', '3306');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_user', 'dayzhivemind');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_password', 'Pkloyn7yvpht!');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_init_sql_file', '/path/to/1.9.0_fresh.sql');
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_default_server_id', '1');
```
## Logging
The system logs MySQL database creation events:
- Success: "MYSQL DB CREATED - Database server_<id> created for server <id>"
- Failure: "MYSQL DB CREATION FAILED - <error details>"

View file

@ -2,6 +2,129 @@
require_once("includes/lib_remote.php");
require_once("modules/config_games/server_config_parser.php");
function createMysqlDatabase($home_id, $settings, $db) {
// Generate database name and user based on server ID
$dbID = "server_" . $home_id;
$dbPass = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 12);
// Check if MySQL auto-creation is enabled
if (!isset($settings['mysql_auto_create']) || $settings['mysql_auto_create'] != '1') {
return false;
}
// Check if we have the required MySQL settings
if (empty($settings['mysql_root_user']) || empty($settings['mysql_root_password']) || empty($settings['mysql_host'])) {
return false;
}
$mysql_host = $settings['mysql_host'];
$mysql_user = $settings['mysql_root_user'];
$mysql_pass = $settings['mysql_root_password'];
$mysql_port = isset($settings['mysql_port']) ? $settings['mysql_port'] : '3306';
try {
// Create MySQL connection
if (function_exists('mysqli_connect')) {
$link = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, "", $mysql_port);
if (!$link) {
$db->logger("MYSQL DB CREATION FAILED - Could not connect to MySQL server for server " . $home_id);
return false;
}
// Create database
$query = "CREATE DATABASE IF NOT EXISTS `" . mysqli_real_escape_string($link, $dbID) . "`";
mysqli_query($link, $query);
// Grant privileges to database user locally
$query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $dbID) . "'@'localhost' IDENTIFIED BY '" . mysqli_real_escape_string($link, $dbPass) . "'";
mysqli_query($link, $query);
// Grant privileges to database user remotely
$query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $dbID) . "'@'%' IDENTIFIED BY '" . mysqli_real_escape_string($link, $dbPass) . "'";
mysqli_query($link, $query);
// If there's a special user defined, grant it access too (like dayzhivemind in the original script)
if (!empty($settings['mysql_special_user']) && !empty($settings['mysql_special_password'])) {
$query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $settings['mysql_special_user']) . "'@'%' IDENTIFIED BY '" . mysqli_real_escape_string($link, $settings['mysql_special_password']) . "'";
mysqli_query($link, $query);
}
// Flush privileges
mysqli_query($link, "FLUSH PRIVILEGES");
// Import SQL file if specified
if (!empty($settings['mysql_init_sql_file'])) {
$sql_file = $settings['mysql_init_sql_file'];
if (file_exists($sql_file)) {
$sql_content = file_get_contents($sql_file);
mysqli_select_db($link, $dbID);
mysqli_multi_query($link, $sql_content);
}
}
mysqli_close($link);
} else {
// Fallback to old mysql functions
$link = mysql_connect($mysql_host . ':' . $mysql_port, $mysql_user, $mysql_pass);
if (!$link) {
$db->logger("MYSQL DB CREATION FAILED - Could not connect to MySQL server for server " . $home_id);
return false;
}
// Create database
$query = "CREATE DATABASE IF NOT EXISTS `" . mysql_real_escape_string($dbID, $link) . "`";
mysql_query($query, $link);
// Grant privileges
$query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($dbID, $link) . "'@'localhost' IDENTIFIED BY '" . mysql_real_escape_string($dbPass, $link) . "'";
mysql_query($query, $link);
$query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($dbID, $link) . "'@'%' IDENTIFIED BY '" . mysql_real_escape_string($dbPass, $link) . "'";
mysql_query($query, $link);
if (!empty($settings['mysql_special_user']) && !empty($settings['mysql_special_password'])) {
$query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($settings['mysql_special_user'], $link) . "'@'%' IDENTIFIED BY '" . mysql_real_escape_string($settings['mysql_special_password'], $link) . "'";
mysql_query($query, $link);
}
mysql_query("FLUSH PRIVILEGES", $link);
if (!empty($settings['mysql_init_sql_file'])) {
$sql_file = $settings['mysql_init_sql_file'];
if (file_exists($sql_file)) {
$sql_content = file_get_contents($sql_file);
mysql_select_db($dbID, $link);
mysql_query($sql_content, $link);
}
}
mysql_close($link);
}
// Add database to OGP tracking if mysql_server_id is configured
if (!empty($settings['mysql_default_server_id'])) {
// Try to add to the mysql_databases table directly using the main db connection
$db->query("DELETE FROM OGP_DB_PREFIXmysql_databases WHERE db_user = '" . $db->realEscapeSingle($dbID) . "'");
// Insert new database record directly
$query = "INSERT INTO OGP_DB_PREFIXmysql_databases (mysql_server_id, home_id, db_user, db_passwd, db_name, enabled) VALUES (" .
$db->realEscapeSingle($settings['mysql_default_server_id']) . ", " .
$db->realEscapeSingle($home_id) . ", '" .
$db->realEscapeSingle($dbID) . "', '" .
$db->realEscapeSingle($dbPass) . "', '" .
$db->realEscapeSingle($dbID) . "', 1)";
$db->query($query);
}
$db->logger("MYSQL DB CREATED - Database " . $dbID . " created for server " . $home_id);
return true;
} catch (Exception $e) {
$db->logger("MYSQL DB CREATION FAILED - Error creating database for server " . $home_id . ": " . $e->getMessage());
return false;
}
}
function exec_ogp_module()
{
global $db,$view,$settings;
@ -241,6 +364,13 @@ function exec_ogp_module()
echo "<h4><br><p>".get_lang('starting_installations')."</p></h4><br>";
//PANEL LOG
$db->logger( "CREATED NEW SERVER " . $home_id);
// CREATE MYSQL DATABASE FOR NEW SERVERS
if($order['finish_date'] == 0){
$settings = $db->getSettings();
createMysqlDatabase($home_id, $settings, $db);
}
// SEND EMAIL to new server only
if($order['finish_date'] == 0){
$settings = $db->getSettings();

View file

@ -0,0 +1,39 @@
-- MySQL Auto-Create Settings Configuration
-- Execute these SQL statements to configure MySQL auto-creation for game servers
-- Enable MySQL auto-creation (set to '1' to enable, '0' to disable)
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_auto_create', '1')
ON DUPLICATE KEY UPDATE value = '1';
-- MySQL connection settings (REQUIRED - update these with your actual MySQL server details)
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_user', 'remoteuser')
ON DUPLICATE KEY UPDATE value = 'remoteuser';
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_password', 'Pkloyn7yvpht!')
ON DUPLICATE KEY UPDATE value = 'Pkloyn7yvpht!';
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_host', 'mysql.iaregamer.com')
ON DUPLICATE KEY UPDATE value = 'mysql.iaregamer.com';
-- Optional: MySQL port (defaults to 3306 if not set)
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_port', '3306')
ON DUPLICATE KEY UPDATE value = '3306';
-- Optional: Special user that gets access to all created databases (like dayzhivemind in original script)
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_user', 'dayzhivemind')
ON DUPLICATE KEY UPDATE value = 'dayzhivemind';
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_password', 'Pkloyn7yvpht!')
ON DUPLICATE KEY UPDATE value = 'Pkloyn7yvpht!';
-- Optional: Path to SQL file to import into each new database (like 1.9.0_fresh.sql)
-- INSERT INTO ogp_settings (setting, value) VALUES ('mysql_init_sql_file', '/path/to/1.9.0_fresh.sql')
-- ON DUPLICATE KEY UPDATE value = '/path/to/1.9.0_fresh.sql';
-- Optional: MySQL server ID from ogp_mysql_servers table to track databases in OGP
-- (Set this to the ID of your MySQL server in the mysql module if you want OGP to track the databases)
INSERT INTO ogp_settings (setting, value) VALUES ('mysql_default_server_id', '1')
ON DUPLICATE KEY UPDATE value = '1';
-- To verify settings, run:
-- SELECT * FROM ogp_settings WHERE setting LIKE 'mysql_%';