Database and User Created Via Script Issue
I am using the functions below to create a database and add a user to that database
After the script runs, i can confim on cpanel interface that the database has been created, and it shows the privileged user. The problem is when I ran the app, it says user has no access to database. When i remove the user and re add manually from cpanel, the app runs perfectly. Why is it not working on the first case.
function createDb($dbName) {
$cPanelUser = 'user';
$cPanelPass = 'password';
$buildRequest = "/frontend/paper_lantern/sql/addb.html?db=".$dbName;
$openSocket = fsockopen('localhost',2082);
if(!$openSocket) {
return "Socket error";
exit();
}
$authString = $cPanelUser . ":" . $cPanelPass;
$authPass = base64_encode($authString);
$buildHeaders = "GET " . $buildRequest ."\r\n";
$buildHeaders .= "HTTP/1.0\r\n";
$buildHeaders .= "Host:localhost\r\n";
$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
$buildHeaders .= "\r\n";
fputs($openSocket, $buildHeaders);
while(!feof($openSocket)) {
fgets($openSocket,128);
}
fclose($openSocket);
}
function addUserToDb($dbName) {
$cPanelUser = 'user';
$cPanelPass = 'password';
$userName = 'databaseuser'; //this user is already created
$buildRequest = "/frontend/paper_lantern/sql/addusertodb.html?user=".$userName."&db=".$dbName.'&ALL=ALL';
$openSocket = fsockopen('localhost',2082);
if(!$openSocket) {
return "Socket error";
exit();
}
$authString = $cPanelUser . ":" . $cPanelPass;
$authPass = base64_encode($authString);
$buildHeaders = "GET " . $buildRequest ."\r\n";
$buildHeaders .= "HTTP/1.0\r\n";
$buildHeaders .= "Host:localhost\r\n";
$buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
$buildHeaders .= "\r\n";
fputs($openSocket, $buildHeaders);
while(!feof($openSocket)) {
fgets($openSocket,128);
}
fclose($openSocket);
}
After the script runs, i can confim on cpanel interface that the database has been created, and it shows the privileged user. The problem is when I ran the app, it says user has no access to database. When i remove the user and re add manually from cpanel, the app runs perfectly. Why is it not working on the first case.
-
Hi, Why don't you use the WHM API provied? It will make things easier for you. Check the below link: WHM API 1 Sections - Databases - Developer Documentation - cPanel Documentation 0 -
Hello, Further on to @24x7server point the UAPI would actually be the best thing to use here, not manually adding. UAPI Functions - Mysql::create_database - Developer Documentation - cPanel Documentation UAPI Functions - Mysql::create_user - Developer Documentation - cPanel Documentation UAPI Functions - Mysql::set_privileges_on_database - Developer Documentation - cPanel Documentation 0
Please sign in to leave a comment.
Comments
2 comments