Community Forums
Connect with us on LinkedIn
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 24
  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Exclamation [case 45836] CPANEL API not adding created user to created database

    Hello,

    I am using the cPanel API to automatically create a database, create a database user and then finally add that user with all permissions to that database.

    This script has been working correctly for weeks and I have created around 100 databases with users.

    Today I decided to clean up the databases that I had automatically created and deleted them through the Cpanel control panel.

    Here is my PHP script

    <?php

    $domainAddress = 'mydomain';
    $databaseuser = 'myuser';
    $databasepass = 'mypass';

    //create CPANEL database
    include("xmlapi.php");
    $db_host = "localhost";
    $cpuser = "ACCOUNT_USER";
    $databasename = $domainAddress;//do not prepend with username
    //$databaseuser = random_string('alnum', 5);//api will do that for you
    //$databasepass = random_string('alnum', 16);

    $xmlapi = new xmlapi($db_host);
    $xmlapi->password_auth("MAIN_ACCOUNT_USER","MAIN_ACCOUNT_PASS");
    $xmlapi->set_debug(1);//this setting will put output into the error log in the directory that you are calling script from
    $xmlapi->set_output('array');//set this for browser output
    //create database
    $createdb = $xmlapi->api1_query($cpuser, "Mysql", "adddb", array($databasename));
    foreach($createdb as $v)
    {
    $result = $v['result'];
    }
    if ($result == 1)
    {
    //create user
    $usr = $xmlapi->api1_query($cpuser, "Mysql", "adduser", array($databaseuser, $databasepass));
    }
    foreach($usr as $v)
    {
    $result2 = $v['result'];
    }
    if ($result2 == 1)
    {
    //add user to database
    $addusr = $xmlapi->api1_query($cpuser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));

    }

    ?>

    This successfully creates a database called ACCOUNT_mydomain. It also successfully creates a user called ACCOUNT_myuser. HOWEVER IT FAILS when adding the user to the database.

    The debug file says this:

    [29-Dec-2010 01:32:11] RESPONSE:
    <?xml version="1.0" ?>
    <cpanelresult><module>Mysql</module><func>adduserdb</func><type>event</type><source>internal</source><apiversion>1</apiversion><data><result>You do not have access to that database (mydomain)!
    </result></data> <event>
    <result>1</result>
    </event>
    </cpanelresult>
    [29-Dec-2010 01:32:11] SimpleXML var_dump:
    SimpleXMLElement Object
    (
    [module] => Mysql
    [func] => adduserdb
    [type] => event
    [source] => internal
    [apiversion] => 1
    [data] => SimpleXMLElement Object
    (
    [result] => You do not have access to that database (mydomain)!

    )

    [event] => SimpleXMLElement Object
    (
    [result] => 1
    )

    )

    [29-Dec-2010 01:32:11] Associative Array var_dump:
    Array
    (
    [module] => Mysql
    [func] => adduserdb
    [type] => event
    [source] => internal
    [apiversion] => 1
    [data] => Array
    (
    [result] => You do not have access to that database (mydomain)!

    )

    [event] => Array
    (
    [result] => 1
    )

    )

    I have already been through this issue with a chat technician who troubleshooted my script and has confirmed it should be working correctly.

    As I have mentioned this script was working flawlessly for over a month.

    My personal feeling is that deleting the databases and the users without deleted the links between the users and their databases has got the CPANEL API tied up in a Knot.

    I also read that mysql will reject long user names and I did try shortening them down as low as I could but no difference. Once again... it was working only 5 hours ago.

    Does anyone have any suggestions?

    Thanks

  2. #2
    Member
    Join Date
    Nov 2005
    Posts
    5

    Default re: [case 45836] CPANEL API not adding created user to created database

    I've been using a script like yours for about 18 months and it stopped working today as well. Nothing has been changed. I'm guessing considering the timing of both our problems that a recent CPanel update is responsible. If someone could give us a workaround though that would be great. I have quite a few sites every day that I use this for!

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Default re: [case 45836] CPANEL API not adding created user to created database

    I had the same problem which started about the same time yours did.

    Finally fixed it by adding the last line of code: (this is in PERL)

    $dbcreated = system("/usr/local/cpanel/bin/mysqlwrap ADDDB $databasename;
    /usr/local/cpanel/bin/mysqlwrap ADDUSER $username $password;
    /usr/local/cpanel/bin/mysqlwrap ADDUSERDB $databasename $username ALL;
    /usr/local/cpanel/bin/dbmaptool <$cpanel_username> --type mysql --dbs <$databasename> --dbusers $username;");

    Not sure if it was the "right" way to do it, but at least my program is actually working again.


    For a solution in PHP check out the bottom of:

    Frequently Asked Scripts: "How do I automate XYZ after creating an account?" - cPanel Integration


    The basic issue is that when they updated cpanel they dramatically changed the way databases are named and referenced. "cPanel 11.25.1 has a feature set called DB Mapping, which is responsible for DB Prefixing."

    This is probably what caused your script to break.

    Hope this helps.

    donk

    P.S. Isn't it FUN to waste an entire day on research, testing, and debugging for what turns out to be one line of code, simply because nobody notified you that they were making changes and "improvements" to the server?

  4. #4
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default re: [case 45836] CPANEL API not adding created user to created database

    Hi all,

    @tdmohr
    The reason why your script is failing is most likely the third API request:
    Code:
    //add user to database 
    $addusr = $xmlapi->api1_query($cpuser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));
    In particular, your variables $databasename and $databaseuser only contain the unique portion of the data resource. When you make the request to create the database, the API "adddb" and "adduser" will dynamically had the user prefix before the SQL statement is passed to MySQL (if prefixing is enable, which is the default). The "adduserdb" API call does not do this. It will take its arguments literally.

    So, what is happening in your script:
    1)"adddb" request will make the MySQL db "$cpuser_mydomain"
    2)"adduser" request will make the MySQL db virtual user "$cpuser_myuser"
    3)"adduserdb" request attempts to assigned 'ALL' SQL privileges for "myuser" to db "mydomain"....which fails

    This solution is to modify $databasename before the "adduserdb", if you cPanel instance is using DB Prefixing. This should be illustrated in the blog post that donk references, Frequently Asked Scripts: "How do I automate XYZ after creating an account?" - cPanel Integration

    Also, your script is checking the result values of the API. Error checking is a really good habit. However, API1 functions (in particular the Mysql functions) do not return a status based on the action requests. It's based on the status of the execution of the cPanel API call. So, as long as cPanel doesn't croak and die, the status will likely be "1" regardless of if the MySQL actions is performed successfully. Kinda lame, but that's the way that it has been implemented and it's been that way for a long time. There's a feature request to convert the API1 Mysql module into an API2 module so that proper status results can be supplied in the API response. So hopefully in the near future this will be better for all In the meantime, you'd have to do your own manual error checking against MySQL to verify that the MySQL resource was created...which depending on your implementation may prove to be cumbersome

    @donk
    I'm fairly certain that ADDDB, ADDUSER and ADDUSERDB will behave identically to their API1 cousins Mysql::adddb and Mysql::adduserdb IF you are invoking mysqlwrap as the user. If you're performing the action as root, then you'll probably have to call dbmaptool like you have done. The reason why is that the mysqlwrap will not pass a user uid to mysqladmin and thus mysqladmin and the Cpanel::Mysql module will not know who to map the new db resources to. I haven't tested this directly, but will if you find this statement to be in error. If you script is executing as root, then you can call "/usr/local/cpanel/bin/mysqladmin" directly. It will take the same arguments as mysqlwrap. The only difference is that you first argument is the uid of the user to affect.
    Code:
    #example
    /usr/local/cpanel/bin/mysqladmin 501 ADDDB $databasename
    FYI: In cPanel 11.30, "mysqlwrap" has been renamed to "cpmysqlwrap"

    Regards,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  5. #5
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default re: [case 45836] CPANEL API not adding created user to created database

    PS. Y'all can checkout the DB Mapping white paper here http://www.cpanel.net/DBMappingWhitePaper_r12.pdf for detailed information concerning how 11.28's DB Mapping feature was implemented. There's not much info on the API, since they didn't change according to their documented usage and arguments.

    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  6. #6
    Member
    Join Date
    Nov 2005
    Posts
    5

    Default re: [case 45836] CPANEL API not adding created user to created database

    Hi Dave,

    Thanks for your response. I'm a bit confused though. You're responding to tdmohr like he's programmed something that won't work (it may not be the best coding but it DID work). Just like him, I've been using my script for over a year until it stopped working a few days ago. I don't think it's a coincidence that we all started experiencing problems after the same cpanel update on the same day. Here's the code that I have for creating the user, database and adding them to the database. The user gets created, the database gets created but I get the same error when adding the user to the database. Did something change in adduserdb?


    //CREATE DATABASE
    <cpanel Mysql="adddb($FORM{'newdb'})">
    <cpanel Mysql="adduser($FORM{'newdb'},$FORM{'newpwd'})">
    <cpanel Mysql="adduserdb($FORM{'newdb'},$FORM{'newdb'},'ALL')">

  7. #7
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default re: [case 45836] CPANEL API not adding created user to created database

    Hi Goosemoose,

    On the contrary, I think the quality of tdmohr's code is just fine. In fact I was happy to see the error checking. I just wanted to make sure that everyone knew, despite his efforts, that error checking those response statuses is not something a developer can depend on.

    Is your script executed by the user? in the cPanel interface? Or is it executed by root or a reseller or within the WHM interface? What day did this start occurring, the 29th? What version of cPanel are you currently running. What release tier is you cPanel instance tracking (ie, STABLE, RELEASE, EDGE, etc).

    In my previous testing, the "adduserdb" always required explicit, exact db and virtuser names as the argument. However it's possible that if you updated to a newer cPanel version with a bug or the old version had a bug that was too permissive in it's arguments, these kind of discrepancies could cause the exhibited behavior.

    If this is a change in behavior then I want to make sure it's noted and the change considered fully. I will need as much detail as possible for those experiencing the issue. I will perform my own testing too.

    Regards,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  8. #8
    Member
    Join Date
    Nov 2005
    Posts
    5

    Default re: [case 45836] CPANEL API not adding created user to created database

    Hi David,

    Okay, I understand what you were saying now.

    I run the script as a reseller in cPanel (as root I wasn't able to get a list of just those users databases so user worked better). I'm actually the only person using the server. I've been using this script over a year. The user and the database are created fine.

    I'm using LiquidWeb as my host so they have cPanel automatically updating (everything should be STABLE). Right now cPanel is 11.28.60. I noticed the problem on the 29th but I might not have used the script in a day or two. I normally use it 7 days a week but slowed down a bit because of the holidays.

    If I can help in any other way, please let me know.

  9. #9
    Member
    Join Date
    Jun 2008
    Posts
    220

    Default re: [case 45836] CPANEL API not adding created user to created database

    Well, I'm having this issue simply trying to use the mySQL database wizard. I am trying to restore a backup which includes creating the cPanel account anew, then creating the database, uploading my WordPress file structure, and importing the mySQL dump. I've done this operation about two dozen times, and today it doesn't work. When adding the database user, I get "You do not have access to that database."

    It's very frustrating when a cPanel update apparently adds a new level of functionality that breaks something that's worked for some time. I'm not using any scripts, just your provided interface to add a user to a database. And, all of a sudden, it doesn't work.

    How can I make this simple operation work again?

    BTW, I did not opt in to the new database functionality. Mapping is fine for our needs, so we took an "if it isn't broken, don't fix it" approach.

    Thanks,

    Mark
    Last edited by markb14391; 01-02-2011 at 02:13 PM. Reason: Clarity

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Default re: [case 45836] CPANEL API not adding created user to created database

    Quote Originally Posted by markb14391 View Post
    Well, I'm having this issue simply trying to use the mySQL database wizard.
    I'm having the same problem as well I also have a few boxes with Liquidweb which were (presumably) automatically udated, I saw someone else above was with them as well.

    Anyway, what's the word? Should I have them reverted to an earlier version of cPanel, or is there a fix in the works, etc... This functionality is something that we absolutely need restored as soon as possible.

    Quote Originally Posted by markb14391 View Post
    How can I make this simple operation work again?
    ^ This

    Ok, thanks in advance everyone

  11. #11
    Member
    Join Date
    Oct 2010
    Posts
    2

    Default re: [case 45836] CPANEL API not adding created user to created database

    Just thought I would post my fix to a similar problem we were having using the CPANEL API for assigning a user to a database. We use Wildfire to automatically install some websites, which include a database. Parts of the scripts quit working for us last Thursday sometime. It was creating a database, creating a database user, but was failing on assigning the user to the database, or at least it seemed to be failing to grant the user any permissions.

    Wildfire has portion of its script which read like so:

    Code:
    {:cpanel_assign_database_user['$cp_username','$cp_password','$cp_serverip','$cp_skin','$database_username','$database_name']:}
    after reading through this very helpful thread, I figured I would try this:

    Code:
    {:cpanel_assign_database_user['$cp_username','$cp_password','$cp_serverip','$cp_skin','$cp_username'_'$database_username','$cp_username'_'$database_name']:}
    Spent all weekend trying to figure out what the problem was. Didn't discover this thread here until this morning.
    Thanks for the help !

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Default re: [case 45836] CPANEL API not adding created user to created database

    I have a similar problem, but i don't use the API.

    I can make a DB and user with
    Code:
    http://$user:$password@$host:2082/frontend/$skin/sql/addb.html?db=".$db_name;
    and
    Code:
    http://$user:$password@$host:2082/frontend/$skin/sql/adduser.html?user=".$db_uname."&pass=".$db_password;
    but
    Code:
    http://$user:$password@host:2082/frontend/skin/sql/addusertodb.html?user=".$db_uname."&db=".$db_name."&ALL=ALL"
    don't work anymore. (script is running for half a year now)

    If i log the returned content of the URL i get

    MySQL® Database Wizard
    User HAbzz6 was added to the database WEtFpQ.
    But The user was not added. (DB and USER do get created..)

    version:

    WHM 11.28.60
    CENTOS 5.5 x86_64 xenpv

    I have tried on several servers.

    Thanks for any advice.
    Last edited by Wim*; 01-03-2011 at 04:39 PM. Reason: typo

  13. #13
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default re: [case 45836] CPANEL API not adding created user to created database

    Hi All,

    Our QA and Dev team are investigating this change in behavior now. It appears that builds 11.28.54 and beyond are currently affected.

    I will post again with any relavent details.

    As noted in my earlier post, you can remedy the situation now by modifying your script to passing the literal database name and database virtual username when invoking Mysql::adduserdb. The user prefix is the cPanel user name plus an underscore, eg 'dave_' for user "dave".

    There's a new API1 Mysql function, "prefix" that can help determine if DB Prefixing in on/off for the cPanel instance. The example below illustrates making the Mysql::prefix call using the PHP XML-API client class and the resulting response. In this scenario, the result is TRUE (the data->result node value): db prefixing is enabled (the default for cPanel instances) so the developer's script should prepend the prefix to the arguments for a succesful call to Mysql::adduserdb

    PHP Code:
    URL: https://10.0.0.1:2087/xml-api/cpanel
    DATA: cpanel_xmlapi_user=dave&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=prefix&cpanel_xmlapi_apiversion=1


    RESPONSE:
     <?xml version="1.0" ?>
     <cpanelresult>
        <module>Mysql</module>
        <func>prefix</func>
        <type>event</type>
        <source>internal</source>
        <apiversion>1</apiversion>
        <data>
            <result>1</result>
        </data>
        <event>
            <result>1</result>
        </event>
    </cpanelresult>
    I presume that our Dev team will restore the functionality, however, the official word, at this point, is to utilize something like the above in your scripts.

    Regards,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  14. #14
    Member
    Join Date
    Nov 2005
    Posts
    5

    Default re: [case 45836] CPANEL API not adding created user to created database

    David,

    Unfortunately I am using cpaneluser_dbuser & cpaneluser_dbname (hadn't looked at the code for a long time and realized it after reviewing the thread again). So I don't see anything in php that I can change to get this to work right now. I even tried hard coding values in. Suggestions?

  15. #15
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default re: [case 45836] CPANEL API not adding created user to created database

    Goosemoose,

    I suggest you investigate want values are being passed to cPanel. One way to do this is to install the API Tracer momentarily to verify the argument values that your script is passing in the API calls. You can download the tarball on the Developer Downloads page. (NOTE: the API Tracer logs all API input, raw, to the cPanel error log, so it's possible that sensitive information can be logged and we typical discourage using it on a production machine.)

    Additionally, you can also enable cPanel Mysql commands debugging by updating your cPanel config and restarting cPanel:
    Code:
    echo 'mysqldebug=1' >> /var/cpanel/cpanel.config;/etc/init.d/cpanel restart
    Again, you only want this temporarily while you troubleshoot your issue. This will only log MySQL commands issued by cPanel. It should record the commands/API calls in question, however, I'm not 100% certain that every cPanel Mysql command has a debug log hook...just FYI to all that are reading this post.

    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

Page 1 of 2 1 2 LastLast
Similar Threads & Tags
Similar threads

  1. Adding FILE permission for a user created by cPanel
    By deswamp in forum New User Questions
    Replies: 0
    Last Post: 10-08-2010, 07:09 AM
  2. How can I see if any user have created a pgsql database?
    By Kent Brockman in forum cPanel and WHM Discussions
    Replies: 0
    Last Post: 10-16-2008, 10:47 PM
  3. Packages created dont show when new account is being created in WHM-Pls help!!
    By net4hosting in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 08-22-2008, 05:24 AM
  4. auto-created database and user
    By dnevins in forum cPanel and WHM Discussions
    Replies: 3
    Last Post: 10-17-2006, 04:51 AM
  5. User subdomains NOT created through cPanel
    By phoxxy in forum cPanel and WHM Discussions
    Replies: 9
    Last Post: 05-16-2005, 01:30 AM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube