Create database user
Hi guys,
Any ideas why this code creates a database, but doesn't creat the user. It throws the error:
The "name" argument cannot be empty. at /usr/local/cpanel/Cpanel/MysqlFE.pm line 437
The file is named newDatabase.live.php
This is the code:
)) {
// Vars
$cpanelApiEndpoint = '['data'>[0] != 1) {
// Handle error
echo "Error creating database: " . $result['cpanelresult'>['error'>;
} else {
// DATABASE CREATED SUCCESSFULLY
// CREATE USER
$requestData = array(
'cpanel_jsonapi_version' => 2,
'cpanel_jsonapi_module' => 'MysqlFE',
'cpanel_jsonapi_func' => 'createdbuser',
'name' => $databaseUser,
'password' => $databasePassword
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cpanelApiEndpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$cpanelUsername:$cpanelPassword");
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if (isset($result['cpanelresult'>['data'>[0]) && $result['cpanelresult'>['data'>[0] == 1) {
echo "Database user created successfully.";
} else {
$errorMessage = isset($result['cpanelresult'>['error'>) ? $result['cpanelresult'>['error'> : "Unknown error";
echo "Error creating database user: " . $errorMessage;
}
}
}
?>
newDb
-
The error message "The 'name' argument cannot be empty" indicates that the 'name' parameter (which represents the name of the database user) is not being provided or is empty in your API request. To fix this issue, you need to make sure that the 'name' parameter is correctly set in your API request. 0 -
The code I provided has the name declared in line 8. I also tried: $requestData = array( 'cpanel_jsonapi_version' => 2, 'cpanel_jsonapi_module' => 'MysqlFE', 'cpanel_jsonapi_func' => 'createdbuser', 'name' => 'harcodedName', 'password' => 'hardcodedPassword' ); So... I also tried with the harcoded name domainname_XXX Any other ideas? 0 -
check your API connections 0 -
I'm not sure what you mean... The API is $cpanelApiEndpoint = 'https://my-awesome-app.com:2083/json-api/cpanel'; $cpanelUsername = 'rightUsername; $cpanelPassword = 'rightPassword'; $databaseName = 'my-awesome-app_' . $dbName; // CREATE DATABASE $requestData = array( 'cpanel_jsonapi_version' => 2, 'cpanel_jsonapi_module' => 'MysqlFE', 'cpanel_jsonapi_func' => 'createdb', 'db' => $databaseName ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $cpanelApiEndpoint); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "$cpanelUsername:$cpanelPassword"); $response = curl_exec($ch); curl_close($ch); // Process the response (handle errors, etc.) $result = json_decode($response, true); if ($result['cpanelresult'>['data'>[0] != 1) { // Handle error echo "Error creating database: " . $result['cpanelresult'>['error'>; } else { // Database created successfully echo "Database created successfully"; } } function createDbUser($userDbName) { $cpanelApiEndpoint = '['data'>[0]) && $result['cpanelresult'>['data'>[0] == 1) { echo "Database user created successfully."; } else { $errorMessage = isset($result['cpanelresult'>['error'>) ? $result['cpanelresult'>['error'> : "Unknown error"; echo "Error creating database user: " . $errorMessage; } } if(isset($_POST['createDb'>)) { $dbName = $_POST['dbName'>; createDb($dbName); } if(isset($_POST['createDbUser'>)) { $userDbName = $_POST['userDbName'>; createDbUser($userDbName); } ?> newDb newUser 0 -
Hi @cosmin.balanean , I believe I've identified your issue, and I have a few additional tips to help as well. I noticed you are using cPanel API 2's function Guide to UAPI - Create a database user using UAPI Regardless of using UAPI or cPanel API 2, both require the db user name be prefixed with the cPanel account (up to first 8 characters) as part of the request in the format of "cpaneluser_dbuser". Depending on the version of the database you are using, there are specific character limits to this name:
- MySQL 5.6 = 16 char limit
- MySQL 7+ = 32 char limit
- MariaDB = 47 char limit
uapi --user=testuser Mysql create_user name='placeholderNewUser' password='fakepass123'
I receive the following error: "The name "placeholderNewUser" does not begin with the required prefix "testuser"." The 'name' value must be prefixed as 'testuser_placeholderNewUser'.0 - Create a database user using UAPI Regardless of using UAPI or cPanel API 2, both require the db user name be prefixed with the cPanel account (up to first 8 characters) as part of the request in the format of "cpaneluser_dbuser". Depending on the version of the database you are using, there are specific character limits to this name:
-
That was it: This function uses the parameter 'dbuser', not 'name'. @cPanelTJ Thank you 0
Please sign in to leave a comment.
Comments
6 comments