创建数据库和用户 mysql 并设置权限 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9720587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create db and user mysql and set privileges php
提问by user1218364
Is there a way to create a new MySQL database, a new MySQL user and give the new user privileges on the new database all using PHP?
有没有办法使用 PHP 创建一个新的 MySQL 数据库、一个新的 MySQL 用户并授予新用户对新数据库的权限?
回答by Fabian Leutgeb
You could do something like this:
你可以这样做:
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();
You may look at the MySQL documentation on GRANTand CREATE USER
您可以查看有关GRANT和CREATE USER的 MySQL 文档
回答by Your Common Sense
Yes, as all these actions can be performed by regular SQL queries.
是的,因为所有这些操作都可以通过常规 SQL 查询来执行。
However, I'd refrain from running PHP scripts with database connection from root user.
但是,我会避免使用来自 root 用户的数据库连接运行 PHP 脚本。
回答by Tamás Pap
If you are hosting your project on CPanel, then the mysql_query
method will not work to create databases, users, and to grant permissions.
如果您在 CPanel 上托管您的项目,则该mysql_query
方法将无法创建数据库、用户和授予权限。
You have to use the XML Api for CPanel.
您必须使用 CPanel 的 XML Api。
<?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'));
?>
Download xmlapi.php from here, or simply search on google for it.
从这里下载 xmlapi.php ,或者直接在谷歌上搜索。
This is what worked for me perfectly.
这对我来说非常有效。
回答by insCode
i will suggest you to use phpmyadmin.
我会建议你使用 phpmyadmin。
you need to do steps:
你需要做的步骤:
- open phpmyadmin
- go to admin section
- hit on add user account
- put user name and password
- set privileges
- hit the [ go ]button
- 打开 phpmyadmin
- 转到管理部分
- 点击添加用户帐户
- 输入用户名和密码
- 设置权限
- 点击[开始]按钮
that's all see in action on youtube [ click here ]
这都是在 youtube 上看到的 [点击这里]
in case if you want to know more about phpMyadmin go with official
如果您想了解有关 phpMyadmin 的更多信息,请与官方联系
but if there is any special reason to do so with php the here is the sql commend
但是如果有任何特殊原因需要用 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
回答by Aravinthkumar Kandasamy
open PHP myadmin or MySql workbench go to query window then run below query
打开 PHP myadmin 或 MySql 工作台转到查询窗口然后运行下面的查询
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON db1.* TO 'username'@'localhost'"
回答by kierzo
<!--
Go to setup on line 48
Created by [email protected]
-->
<!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 [email protected]
//
// 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>";
?>