开发者

Why I cannot create DB in MySQL throw PHP?

I have this code:

$link = mysql_connect("localhost", "ctmanager", "blablabla");
if ( ! $link )
   die ("开发者_C百科I cannot connect to MySQL.<br>\n");
else
   print "Connection is established.<br>\n";

// Create the "ct" database.
   mysql_query("create database ct", $link) or die("I cannot create the DB: ".mysql_error()."<br>\n");

And I get this error message:

I cannot create the DB: Access denied for user 'ctmanager'@'%' to database 'ct'

Does anybody have any idea why I cannot create a DB and why I have '@%' symbols in the error message?

ADDED

ctmanager is the administrator. He should have permissions to add databases and users.


The user ctmanager@% is not allowed to create databases. It doesn't have the CREATE DATABASE privilege in MySQL. As a privileged user, run this command in MySQL:

GRANT CREATE DATABASE ON *.* TO ctmanager@'%';

@% means "at any host". You can give different permissions to the same user depending on which host the user connects from by putting a hostname after tha @ like this: user@example.com (for "user" connection from "example.com").


This means that the user ctmanager is not allowed to connect with the MySql server from "any machine" (@'%' means "at any machine").

Usually, access rights are not only given my user, but also depending on which machine the user connects from. "Any machine" means that the user can connect from any computer via network. Probably, the user ctmanager only has access rights from "localhost" or "127.0.0.1".

You'll have to add a new user that is allowed to access the database from any machine (PhpMyAdmin - if installed - can help you here).


The part after the @ is the hostname you're (supposed) to be connecting from, in MySQL's privilege tables. % means "any host". So, the user ctmanager connecting from any host does not have access to the ct database. Amend your privileges to allow it.


I know this is an old post, but I found this while searching for the same problem. The way to correctly grant permissions for a user to create a database is:

GRANT CREATE ON . to 'user'@'localhost' IDENTIFIED BY 'password';


Have you tried using mysql_create_db()? After this, you can select the database with mysql_select_db() and use it as you are used to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜