Solved

Auto user privs

Posted on 2010-09-23
6
239 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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