Solved

Auto user privs

Posted on 2010-09-23
6
241 Views
Last Modified: 2012-05-10
Hello, i have a script that creates a database from php but i cant connect, i get a Access denied for user 'username'@'localhost' (using password: YES) my connection values are correct so i guess it user privilidges?
How to i had that?
thanks
//Create database
		$con=mysql_connect("localhost","username","password");

if(!$con)
{ 
	die ("Connection failed :".mysql_error());
}
	else
	{
	echo "Connection good, ";
	}
		$qry=mysql_query("CREATE DATABASE monsmegm_$folderName",$con);
if(!$qry)
{
	die ("Query to database failed..".mysql_error());
	}
	else
	
// Create table
mysql_select_db("monsmegm_$folderName", $con);
$tables = array("offers", "rooms", "sight", "welcome");
foreach($tables as $name){
if($name == "offers"){
mysql_query("CREATE TABLE $name(
 idoffers  INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 offerDesc  VARCHAR(255) NOT NULL,
 offerImg  BLOB NOT NULL,
 offerName  VARCHAR(50) NOT NULL
)") or die(mySQL_error());
} elseif($name == "rooms"){
mysql_query("CREATE TABLE IF NOT EXISTS $name(
 idroom  INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 roomsDesc  VARCHAR(255) NOT NULL,
 roomimg  BLOB NOT NULL,
 roomName  VARCHAR(50) NOT NULL,
 roomPrice  INT(5) NOT NULL
)") or die(mySQL_error());
} elseif($name == "sight"){
mysql_query("CREATE TABLE IF NOT EXISTS $name(
idisight  INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 sightDesc  VARCHAR(255) NOT NULL,
 sightimg  BLOB NOT NULL,
 sightName  VARCHAR(50) NOT NULL
)") or die(mySQL_error());
} elseif($name == "welcome"){
mysql_query("CREATE TABLE IF NOT EXISTS $name(
wellid  INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 header  VARCHAR(50) NOT NULL,
 mainbody  BLOB NOT NULL,
 subtext  VARCHAR(255) NOT NULL,
 wellimg1  BLOB NOT NULL,
 wellimg2  BLOB NOT NULL
 )") or die(mySQL_error());
}
}
echo "Database with fields created..i hope";

// Execute query
mysql_query($sql,$con);

mysql_close($con);

Open in new window

0
Comment
Question by:vermer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33747984
vermer,

Firstly, are you certain the database is being created?  The main issue is that your user needs to be granted permissions to create databases in the first place (i.e., has been given GRANT CREATE).  If the database is being created, then you simply may need to assign SELECT or other DML rights to the user using GRANT so in either case see this reference to help:

http://dev.mysql.com/doc/refman/5.1/en/grant.html

Regards,

Kevin
0
 

Author Comment

by:vermer
ID: 33748602
So like?
<php?> //
GRANT
    CREATE [(column_list?)]
      [, FUNCTION [(column_list?)]] ...
    ON [CREATE] *.*
    TO user [IDENTIFIED BY [PASSWORD] 'password']
        [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
</?>
I expect the application to use the mysql admin account, and i have set all prividges including CREATE. am i on the right path?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33748713
If you have set the application to use an admin account, you should be able to just set in MySQL:

GRANT ALL ON *.* TO 'username';

Once you create the new database, just in case you can use:

$permSQL = "GRANT ALL ON databasename.* TO 'username';"
mysql_query($permSQL,$con);

Before calling your subsequent SQL statement.  
0
 

Author Comment

by:vermer
ID: 33750026
well it turns out this aint gonna happen as my server said; its not possible to create a database using the php script as it required root privileges for creating a database

oh well, thanks anyway
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37268874
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question