创建没有cpanel访问权限的数据库

时间:2013-08-15 18:40:18

标签: php mysql linux hosting shared

我有一个安装脚本,它在localhost上工作正常但在共享主机上我总是得到“访问被拒绝”,下面是安装脚本mysql代码。它真的很奇怪它只能在localhost本地工作。

if(isset($_POST['install'])) {
$AdminUsername = HtmlSpecialChars(strip_tags($_POST['admin_username']));
$AdminPassword = HtmlSpecialChars(strip_tags($_POST['admin_password']));
$dbhost = strip_tags($_POST['dbhost']);
$dbuser = strip_tags($_POST['dbuser']);
$dbpass = $_POST['dbpass'];
$sitename = strip_tags($_POST['sitename']);
$sql = "CREATE TABLE tickets
(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(32),
Email VARCHAR(40),
Subject VARCHAR(40),
Message TEXT(500),
open VARCHAR(10),
TimeCreated VARCHAR(40),
TimeUpdated VARCHAR(40),
hash VARCHAR(100)
)";
$sql2 = "CREATE TABLE config 
(
AdminUsername VARCHAR(32),
AdminPassword VARCHAR(32)
)";
$sql3 = "INSERT INTO config (AdminUsername,AdminPassword) VALUES ('$AdminUsername','$AdminPassword')";
 if(empty($dbhost)) {
echo "<div style='color:red;'>MySQL Database Host should not be empty!</div><br /><br />";
} elseif(empty($dbuser)) {
echo "<div style='color:red;'>MySQL Database User should not be empty!</div><br /><br />";
} elseif(empty($dbpass)) {
echo "<div style='color:red;'>MySQL Database Password should not be empty!</div><br /><br />"; 
} elseif(empty($sitename)) {
echo "<div style='color:red;'>Please enter a name for your new installation (It can be whatever you want)</div><br /><br />";
} elseif(empty($AdminUsername)) {
echo "<div style='color:red;'>Admin Username should not be empty!</div><br /><br />";
} elseif (empty($AdminPassword)) {
echo "<div style='color:red;'>Admin Password should not be empty!</div><br /><br />";
}
else {
$file = fopen('../configuration/config.php','r+');
fwrite($file, '<?php $dbhost = "'. $dbhost.'"; $dbuser = "'. $dbuser.'"; $dbpass = "'. $dbpass.'";  $sitename = "'. $sitename.'"; $temp = new mysqli ($dbhost, $dbuser, $dbpass);');
include('../configuration/config.php');
$temp->query("CREATE DATABASE tests");
$file2 = fopen('../configuration/config.php','r+');
fwrite($file2, '<?php $dbhost = "'. $dbhost.'"; $dbuser = "'. $dbuser.'"; $dbpass = "'. $dbpass.'";  $sitename = "'. $sitename.'"; $dbname = "tests"; $con = new mysqli ($dbhost, $dbuser, $dbpass,$dbname);');
include('../configuration/config.php');
fopen('install.lock','w');
header('Location: ../index.php');
}
}
}
} else {
echo "installation is locked";
}

我怎样才能使它适用于共享主机?拜托,我真的很感激!提前致谢 我无法在stackoverflow上找到答案,所以我决定将其作为一个新问题发布

2 个答案:

答案 0 :(得分:0)

您无权执行此操作。

如果用户没有权限,则无法创建数据库。您必须为特定用户设置这些权限。我怀疑如果共享主机首先阻止它,它会让你。

答案 1 :(得分:0)

它不会让你从共享的托管环境中出来,但它会来自localhost并不奇怪。这就是mysql权限的工作方式,它们由用户和用户发出请求的ip定义。您的安装脚本可以在localhost上运行,因为'user'@'localhost'具有Create和insert权限,但是看起来'user'@'sharedhostingip'没有这些权限,因此您的请求得到满足拒绝访问响应。如果您的用户有权修改用户,那么请尝试向您的用户@服务器的IP地址授予创建和插入权限,否则您必须联系您的DBA才能获得这些权限。

希望这会有所帮助。