Solved

Auto user privs

Posted on 2010-09-23
6
234 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
  • 2
  • 2
6 Comments
 
LVL 59

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 59

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 142

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now