Jump to content

Recommended Posts

I have been banging my head on the wall for three days over this.  I don't normally give up, but this is obviously above my knowledge level.  I have a PHP script that as part of it's function, it creates a database within a cPanel control panel.  That works fine.  What I have tried to add to that is giving a specific user permissions to the database but all of my efforts have failed.  Everytime I run the script with my new additions it gives me Access denied for user "userIcreated@localhost" for database "The DatabaseIcreated".  I am starting to wonder if it is possible to add a user to a MySQL database programmatically when the database resides within cPanel.  Does anyone know if this is possible?  I want to think that something like this is very possible with PHP, I just don't have the skillset to do it.  I have to believe I am close, it is just the username that seems to be wrong.  Please note I AM NOT A PROGRAMMER.  I just learn fast and have been reading alot.  I am attaching my code for review:

 

// cPanel variables
$cpanel_user = "MyAdminUserForCpanel";
$cpanel_password = "MyPassword!";
$cpanel_host = "mydomain.net";
$cpanel_skin = "x";

// Path to cURL on your server
$curl_path = "/usr/bin/curl";


if ($success != "") { //Everything OK to  create database

$result = exec("$curl_path 'http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adddb.html?db=$databasekey'");  //$databasekey is defined earlier and works fine.  The database gets created here just fine.


} else {

echo 'There was an error creating your databse information .  The System Administrator has been notified';
echo mysql_error();

}//End the database creation routine and move on to inserting privileges for the PROD user to access the database.

// This file contains the database access information. This file also establishes a connection to MySQL and selects the database.

//Set the database access information as constants.
DEFINE ('DB_USER', 'MyAdminUserForCpanel');
DEFINE ('DB_PASSWORD', 'MyPassword!');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', "$databasekey");

if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)) { // Make the connnection.

if (!mysql_select_db (DB_NAME)) { // If it can't select the database.

	// Handle the error.
	echo 'Could not select the database: ' . mysql_error();	

	// Print a message to the user, include the footer, and kill the script.
	echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>';
	exit();

} // End of mysql_select_db IF.

} else { // If it couldn't connect to MySQL.

mysql_select_db(DB_NAME);
    @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'";
    $result = @mysql_query ($query) or die(mysql_error());

    if (mysql_affected_rows() == 1) {

    mysql_close();

} // End of $dbc IF.

}

well you better check it in the

    $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'";

I want to note that I am on a shared reseller hosting account and as such do not have ROOT MySQL access.  I just assumed because I can create the database programmatically that I could add users to the database at the same time, but maybe not.  Anyone have any ideas?

Well from what i see you have alot of diffent "typ'os" until they are cleared, i'm not sure what to point out

 

This doesn't help me much.  Care to elaborate what you see that is a typo or error besides what I already said is correct in my "real" code I am using?

maybe posting your real code will reveal real problems

 

just another one from the same line as the one above

<?php   $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'";
?>

 

missing the close bracket ")"

Just an update for anyone interested.  I finally muddled through this on my own and have discovered that outside of the Cpanel interface, I do not have sufficient privileges to grant users privileges on databases.  cPanel must use root MySQL access to accomplish this.  I will have to regroup and rethink my strategy.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.