?
Solved

Auto user privs

Posted on 2010-09-23
6
Medium Priority
?
247 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
5 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 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