Solved

Auto user privs

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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