从 PHP 创建 MySQL 用户和数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2745675/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:31:51  来源:igfitidea点击:

Create MySQL user and database from PHP

phpdatabasemysql

提问by robjmills

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 用户并授予新用户对新数据库的权限?

EDIT- should be pointed out this is run from 1 server to another, so Server A trying to install a DB/user on Server B

编辑- 应该指出这是从一台服务器运行到另一台服务器,因此服务器 A 尝试在服务器 B 上安装数据库/用户

i've got this:

我有这个:

$con = mysql_connect("REMOTE.IP.ADDRESS","root","pass");
mysql_query("CREATE DATABASE ".$db."",$con)or die(mysql_error());
mysql_query("GRANT ALL ON ".$db.".* to  ".$user." identified by '".$dbpass."'",$con) or die(mysql_error());

but i'm getting an error on the grant query:

但我在授权查询中遇到错误:

"Access denied for user 'root'@'MY.REMOTE.SERVER.HOST.NAME' to database 'dbname'"

采纳答案by Tim Post

This answer has been edited several times based on new info provided by the OP

此答案已根据 OP 提供的新信息进行了多次编辑

Is root actually allowedto connect to the server from the host that you are connecting from? If the error string is returning the canonical name of the server, there's a very good chance that 'localhost' is not pointing to 127.0.0.1 :

实际上是否允许root从您正在连接的主机连接到服务器?如果错误字符串返回服务器的规范名称,则很有可能 'localhost' 未指向 127.0.0.1 :

"Access denied for user 'root'@'MY.SERVER.HOST.NAME' to database 'dbname'"

“用户 'root'@'MY.SERVER.HOST.NAME' 对数据库 'dbname' 的访问被拒绝”

That should echo something like "Access denied for user 'root'@localhost'", not the name of the server.

这应该回显类似“用户'root'@localhost'的访问被拒绝”之类的内容,而不是服务器的名称。

Try:

尝试:

$con = mysql_connect("127.0.0.1","root","pass");

Edit(After more information provided in comments)

编辑(在评论中提供更多信息后)

If you are connecting from a totally different host, you have to tell MySQL user@remote_hostname_or_ipis allowed to connect, and has appropriate privileges to create a database and users.

如果你从一个完全不同的主机连接,你必须告诉 MySQLuser@remote_hostname_or_ip被允许连接,并且有适当的权限来创建数据库和用户。

You can do this rather easily using phpmyadmin (on the MySQL server), or a query like:

您可以使用 phpmyadmin(在 MySQL 服务器上)或以下查询轻松完成此操作:

CREATE USER 'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret';

GRANT ALL PRIVILEGES ON * . * TO  'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

I would advise not naming this user 'root' , just create a user with all of the global privileges needed. In the example, I used 192.168.1.1, that could easily be a hostname, just make sure DNS is set up appropriately. Specify the host to match exactlyas it appears in logs when you connect to the remote server.

我建议不要将此用户命名为 'root' ,只需创建一个具有所需的所有全局权限的用户即可。在示例中,我使用了 192.168.1.1,这很容易成为主机名,只需确保 DNS 设置正确即可。指定主机以与连接到远程服务器时在日志中显示的完全匹配。

You may also want to adjust limits to taste. More information on the CREATE USERsyntax can be found here, GRANThere.

您可能还想根据口味调整限制。关于更多信息CREATE USER语法可以在这里找到GRANT在这里

Edit

编辑

If using MySQL 4 - CREATE is not an option. You would just use GRANT (4.1 Docs On User Management)

如果使用 MySQL 4 - CREATE 不是一个选项。您只需使用 GRANT(4.1 用户管理文档

Edit

编辑

If using C-Panel, just use the API. While yes, it does have its quirks, its easier to maintain stuff that uses it rather than ad-hoc work arounds. A lot of successful applications use it without issue. Like any other API, you need to stay on top of changes when using it.

如果使用 C-Panel,只需使用 API。虽然是的,但它确实有它的怪癖,它更容易维护使用它的东西而不是临时解决方法。许多成功的应用程序都可以毫无问题地使用它。与任何其他 API 一样,您需要在使用时随时掌握变化。

回答by Josh K

I believe you'd have to create a connection (with the rootuser) to an existing database (like mysql) and then run the create query.

我相信您必须创建root到现有数据库(如mysql)的连接(与用户),然后运行创建查询。

回答by Your Common Sense

You don't see a database connect in this example for the obvious reason: it is supposed that you learned already that any database action requires connect first.
It's the way the books being written: the things from the previous lessons being omitted in the next ones. Or every chapter will be 2 times bigger and you'll never finish the book.

由于显而易见的原因,您在本示例中没有看到数据库连接:假设您已经了解到任何数据库操作都需要先连接。
这就是写书的方式:上一课的内容在下一课中被省略。否则每一章都会大 2 倍,而你永远也读不完这本书。

so yes, you need to connect to the database first, using mysql_connect().

所以是的,您需要先使用 mysql_connect() 连接到数据库。

to create a user you can use mysql GRANT query

要创建一个用户,您可以使用 mysql GRANT 查询

though I am never done it from the script but from the shell only

虽然我从来没有从脚本中完成过,而只是从 shell 中完成的

回答by Gazler

You would need to connect to the sql server first:

您需要先连接到 sql 服务器:

$conn=@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
    or die("Err:Conn");

Then the query will execute. A lot of shared hosting servers disable the creation of databases via PHP though.

然后查询将执行。但是,许多共享托管服务器禁止通过 PHP 创建数据库。