在 WHM/cPanel 服务器上通过 PHP 以编程方式创建数据库

发布于 2024-09-04 17:35:31 字数 49 浏览 4 评论 0原文

我想知道是否可以在 WHM/cPanel 服务器上从 PHP 创建新的数据库和用户。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-09-11 17:35:31

好吧,就是这样。

“mysql_create_db”函数在 cPanel 服务器上无法正常工作。

有一个解决方案,通过使用专有的 cPanel 函数,如下所示

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

不过, 还有一个现成的脚本,可以在这里使用 http://www.zubrag .com/scripts/cpanel-database-creator.php

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

话少情深 2024-09-11 17:35:31

对于控制面板:

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');

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');
吖咩 2024-09-11 17:35:31

创建数据库:

mysql_query('create database foo');

mysql_create_db('foo');

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

创建MySQL用户:

您可以通过生成SQL并运行来创建用户帐户通过 mysql_query (就像第一个示例一样): http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

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

泪眸﹌ 2024-09-11 17:35:31

只要您用来连接数据库的用户具有 MySQL 中定义的适当权限,这就不成问题。

要创建数据库,用户需要 CREATE 权限 (删除它们将是 DROP 权限),要创建用户,您需要拥有 创建用户权限。您可能还需要 GRANT OPTION 权限 能够向其他用户授予权限。

因此,只要您可以通过 WHM/cPanel 创建的数据库用户拥有这些权限,就应该没问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文