Create database programatically from PHP on WHM/cPanel server
Asked Answered
K

4

2

I was wondering whether it is possible to create a new database and user, from PHP, on an WHM/cPanel server.

Kaffraria answered 9/6, 2010 at 11:45 Comment(0)
K
3

Ok, this is the thing.

'mysql_create_db' function does not work properly on cPanel servers.

There is a solution to this though, by using the proprietary cPanel function like so

http://USER:PASS@HOST:2082/frontend/SKIN/sql/adddb.html?db=DB

There is also a ready-made script that can be used here http://www.zubrag.com/scripts/cpanel-database-creator.php

Kaffraria answered 13/6, 2010 at 8:23 Comment(1)
no error while having like- USER:PASS@HOST:2082/frontend/SKIN/sql/adddb.html?db=DB but also no database created; is there otherwayNorvall
H
2

For CPanel:

function create_db($cPanelUser,$cPanelPass,$dbName) {

$buildRequest = "/frontend/x3/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);


//echo "Created database $dbName";

}

create_db('username','password','dbName');
Hest answered 17/6, 2013 at 15:2 Comment(0)
S
0

To create a DB:

mysql_query('create database foo');

or

mysql_create_db('foo');

http://www.php.net/manual/en/function.mysql-create-db.php

To create a MySQL user:

You can create user account by generating the SQL and running it through mysql_query (just like the first example): http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Squamous answered 9/6, 2010 at 12:56 Comment(1)
I dont think this is possible. cPanel does not simply allow you to create databases like that using any cPanel user ID/pwd. You need to use the root credentials.Alexina
J
0

As long as the user you use to connect to the database has the appropriate privileges defined in MySQL this shouldn't be a problem.

To create databases the user needs the CREATE privilege (to drop them that would be DROP privilege), to create user(s) you need to have the CREATE USER privilege. You also might need the GRANT OPTION privilege to be able to grant privileges to other users.

So as long as the DB user(s) you can create through WHM/cPanel have these privileges, you should be ok.

Jelle answered 9/6, 2010 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.