创建db和用户mysql并设置权限php

时间:2012-03-15 13:23:34

标签: php mysql database privileges

有没有办法创建一个新的MySQL数据库,一个新的MySQL用户,并使用PHP为新数据库赋予新的用户权限?

5 个答案:

答案 0 :(得分:10)

你可以这样做:

mysql_connect('localhost','user',password);
mysql_query("CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';");
mysql_query("GRANT ALL ON db1.* TO 'username'@'localhost'");
mysql_query("CREATE DATABASE newdatabase");
mysql_close();

您可以查看GRANTCREATE USER

上的MySQL文档

答案 1 :(得分:8)

是的,因为所有这些操作都可以通过常规SQL查询来执行。

但是,我不会使用root用户的数据库连接运行PHP脚本。

答案 2 :(得分:6)

如果您在CPanel上托管项目,则mysql_query方法将无法创建数据库,用户和授予权限。

您必须使用XML Api for CPanel。

<?php
include("xmlapi.php");

$db_host = 'yourdomain.com'; 
$cpaneluser = 'your cpanel username';
$cpanelpass = 'your cpanel password'; 

$databasename = 'testdb';
$databaseuser = 'test'; // Warning: in most of cases this can't be longer than 8 characters
$databasepass = 'dbpass'; // Warning: be sure the password is strong enough, else the CPanel will reject it

$xmlapi = new xmlapi($db_host);    
$xmlapi->password_auth("".$cpaneluser."","".$cpanelpass."");    
$xmlapi->set_port(2082);
$xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false  
$xmlapi->set_output('array');//set this for browser output  
//create database    
$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));   
//create user 
$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));   
 //add user 
$addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array("".$cpaneluser."_".$databasename."", "".$cpaneluser."_".$databaseuser."", 'all'));
?>

here下载xmlapi.php,或者只是在Google上搜索它。

这对我来说非常有用。

答案 3 :(得分:0)

我建议你使用phpmyadmin。

你需要做的步骤:

  1. 打开phpmyadmin
  2. 转到管理部分
  3. 点击添加用户帐户
  4. 输入用户名和密码
  5. 设置权限
  6. 点击[go]按钮
  7. 就是这样 在youtube [click here ]

    上查看

    如果您想了解更多关于phpMyadmin go with official

    的信息

    但是如果有任何特殊原因要使用php,这里是sql表达

    CREATE USER 'tesrytss'@'%'//user name
    IDENTIFIED VIA mysql_native_password USING '***';.//set pass
    GRANT SELECT, INSERT, UPDATE, DELETE, FILE ON *.* TO 'tesrytss'@'%' // previlages and username and location
    REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; //other requerment
    

答案 4 :(得分:-2)

<!--

Go to setup on line 48
Created by kierzo@kierzo.com

-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Create Database</title>
</head>

<body>




<!-- Title -->
<p><h1>Create Database</h1></p>
<p></p>
<p>


<!-- The form -->
<form action="<?php $currentFile = $_SERVER["PHP_SELF"];$parts = Explode('/', $currentFile);echo $parts[count($parts) - 1];?>" method="post">

Database Name: <input name="databasename" type="text" />
<br>
DB Pass: <input name="dbpass" type="text" />
<br>
Admin Pass: <input name="passbox" type="password" />

<p><input name="Submit1" type="submit" value="submit" /></p>

</form>

<!-- end form -->

</p>

</body>

</html>



<?php

//*********************** CONFIG SETUP ************************************//
// Created by kierzo@kierzo.com
//
// set the admin pass for the page
$adminpass = "*******";   // change ******* with your page pass
//
// set mysql root pass
$mysqlRootPass = "*******";  // change ******* with your mysql root pass
//
//
//*********************** CONFIG SETUP ************************************//


// if isset set the varibables

if(isset($_POST["passbox"]) && ($_POST["databasename"])){


$databasename = $_POST["databasename"];
$password = $_POST["passbox"];
$dbpass = $_POST["dbpass"];

}
else { exit;}

if(($password) == ($adminpass)) {


}
else {

echo "Incorrect Password!";

exit;}

// store connection info...

$connection=mysqli_connect("localhost","root","$mysqlRootPass");


// check connection...

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


  // Create database
  echo "<br><br>";
#echo "$sql";
$sql="CREATE DATABASE $databasename";

if (mysqli_query($connection,$sql))
  {
  echo "<h2>Database <b>$databasename</b> created successfully!</h2>";
  }
else
  {
  echo "Error creating database: " . mysqli_error($con);
  }


    // Create user

$sql='grant usage on *.* to ' . $databasename . '@localhost identified by ' . "'" . "$dbpass" . "'";
echo "<br><br>";
#echo "$sql";
if (mysqli_query($connection,$sql))
  {
  echo "<h2>User Created... <b>$databasename</b> created successfully!</h2>";
  }
else
  {
  echo "Error creating database user: " . mysqli_error($con);
  }


      // Create user permissions

$sql="grant all privileges on $databasename.* to $databasename@localhost";
echo "<br><br>";
#echo "$sql";
if (mysqli_query($connection,$sql))
  {
  echo "<h2>User permissions Created... <b>$databasename</b> created successfully!</h2>";
  }
else
  {
  echo "Error creating database user: " . mysqli_error($con);
  }

  echo "<p>Database Name: $databasename</p>";
  echo "<p>Database Username: $databasename</p>";
  echo "<p>Database Password: $dbpass</p>";
  echo "<p>Database Host: localhost</p>";
?>