tags:

views:

57

answers:

4

I have this code:

$link = mysql_connect("localhost", "ctmanager", "blablabla");
if ( ! $link )
   die ("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.

A: 

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).

Thorsten Dittmar
A: 

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.

Ignacio Vazquez-Abrams
+1  A: 

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: [email protected] (for "user" connection from "example.com").

Emil Vikström
The problem is that ctmanager is a privilege user.
Roman
What do SHOW GRANTS say?
Emil Vikström
A: 

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.

DrColossos
Quote from the PHP site: "The function mysql_create_db() is deprecated. It is preferable to use mysql_query() to issue an sql CREATE DATABASE statement instead."
Hippo