How To Create a MySQL Database In cPanel With PHP

When you need to create a new database on a shared hosting account you normally need to log in to cPanel, create the database, create a user, and add the user to the database. That may not sound like a big deal, but it gets annoying fast when you need more than a couple of databases, or when you’re trying to find a way to quickly install some kind of a script or CMS (e.g. WordPress) on multiple domains.

There are various hacks & PHP scripts that automatically access cPanel and create a database – or so I’ve heard. I couldn’t find anything that would work on my host without modification. So I wrote my own basic cPanel class that can create MySQL databases and users (based in part on 5ubliminal’s script). You can use it as a building block for your own PHP scripts and tools.

Download The Script

cpanel.class.zip (8 Kb)

The archive contains :

  • includes/cpanel.class.php – the class itself. Feel free to extend or modify it. Links/credits are appreciated.
  • includes/ecurl.class.php – a cURL wrapper class. This is a modified version of another 5ubliminal’s script.
  • cpanel_createdb.php – a simple example that demonstrates how to use the class.

Notes

As you might have guessed, the script needs the cURL module to function. I haven’t tested it extensively; some small modifications might be necessary to make it work with cPanel skins other than x3. For example, the mysql_list_databases() function would probably need different regexps. Feel free to report any incompatibilities in the comments.

By the way, if you make a one-step WordPress using this, let me know – I was too lazy to program it myself 😉

Related posts :

32 Responses to “How To Create a MySQL Database In cPanel With PHP”

  1. David Grega says:

    For those with reseller access, you may wish to consider using our APIs for this task. Unlike individual theme pages that can change, the APIs will function for a long time to come and are theme-independent. Contact sales at cPanel.net if you need assistance with the APIs. Note, you will need reseller access or higher to be able to use the APIs.

  2. White Shadow says:

    I’d have to agree with you there. Users who have the required level of access should probably use an “official” API, that’s more reliable than theme-dependent hacks.

    My post was intended for people who are using shared hosting, which often has many limitations. Still, even experienced developers sometimes use shared hosts due to price and some marginal SEO benefits.

    Edit : Spoke too soon. Interestingly enough, there doesn’t appear to be any way to create databases with the official API.

  3. Hello,

    Thanks for the script it’s just what i’ve been looking for.

    As default the script adds a random password to the db_user. However i wish to apply a password to the user from a form (method post).

    I have tried to set the variable $dbpassword = $_POST[‘password_field’]; and changed the function mysql_create_db_user() in the cpanel.class.php however i’m having trouble setting the password.

    Do you have any pointers on what changes i should make?

  4. White Shadow says:

    Hmm, have you tried simply passing the password as a parameter to the mysql_easy_create() function? For example :

    $rez = $cpanel->mysql_easy_create('example_db','some_username','some_password');
    
  5. Just tried that, but to no success.

    I can make do with a random password for the time being.

    Thanks anyway

  6. David Grega says:

    @White Shadow: I didn’t notice the edit on your post. We do have an API call for creating databases. It is an API1 function call:

    As for adding a user to the database, that’s also an API1 function call:

    API1 can be called remotely by means of the XML-API.

    Drop me an email to sales (at) cPanel.net if you have any questions about our APIs.

  7. Hello again,

    The time has come for me to look into adding the user to the database as described above in my previous post.

    I was wondering if you have made any progress with regard to this.

    Thanks

  8. Hi,

    Sorry for the error but I meant – add the custom user password to the db opposed to the random generated password.

    Regards

  9. White Shadow says:

    Not really. If manually changing the $db_password variable didn’t help the problem is probably in the API, which may be specific to your cPanel version. You could view the source of the user creation form in cPanel and see if it uses a different URL or parameter names.

  10. Hi,

    Thanks for your reply.

    I have tried your previous suggestion and passed the password through the function as a parameter and it worked perfectly.

    The reason it failed to work on the previous attempt is i had modified the cpanel class and forgotten to restore it back to it’s original state.

    Doh!!

    Thank You for your help in this matter.

    It is greatly appreciated.

    Tony

  11. gdgdf says:

    On test

  12. Nathan Adams says:

    Fantastic,

    This has saved me a lot of trouble and works perfectly with hostpapa straight out of the box.

    Many,many thanks

  13. Arvind says:

    Hello

    I am trying to execute the script, but it does not show any message at all, and the browser keeps showing a message on the status bar that ” Transferring data from designshack.info”

    Please help

    Thanks
    Arvind

  14. White Shadow says:

    What script, exactly? Assuming you just used the example script, it should always show something – even if it’s just an error message.

    Also, it looks like your site is down. Maybe the script doesn’t work because of that.

  15. Arvind says:

    Hello

    I tried creating a database using your script, and I get the following error:

    503 Service Unavailable
    The server is temporarily busy, try again later! Powered By LiteSpeed Web Server
    LiteSpeed Technologies is not responsible for administration and contents of this web site!

    I contacted my webhost, they tried running the script in Apache, but the script is basically executing in an indefinite loop without doing anything.

    Is there some parameter issue or cpanel version issue here? My host uses CPanel 11.

    Regards
    Arvind

  16. White Shadow says:

    That’s… interesting. I use cPanel 11 myself, so that shouldn’t be a problem. Also, as far as I can tell, there is simply no way to get an infinite loop in the script itself. I’m not so sure about the eCurl library though, so maybe using a newer version of that might help.

    I suspect it’s more likely that the script makes a slow/problematic HTTP request via the eCurl library and so stays blocked forever, waiting for a request that never returns any data.

  17. Arvind says:

    Hello again,

    For your reference I am attaching the relevant portion of the log file below:
    It shows a timeout error, is this the issue. Also, how to resolve this.

    Regards,
    Arvind

    ATTACHED BELOW- Error log from server

    error_log:2009-06-30 06:10:22.115 [INFO] [59.92.179.107:1522-0#APVH_aquariusweb.in] ExtConn timed out while processing.
    error_log:2009-06-30 06:10:22.115 [NOTICE] [59.92.179.107:1522-0#APVH_aquariusweb.in] connection to [uds://tmp/lshttpd/APVH_aquariusweb.in_Suphp.sock] on request #0, confirmed: 1, Connection timed out!
    error_log:2009-06-30 06:10:22.115 [NOTICE] [59.92.179.107:1522-0#APVH_aquariusweb.in] [uds://tmp/lshttpd/APVH_aquariusweb.in_Suphp.sock] Request in process stage, fail with 503
    error_log:2009-06-30 06:10:22.121 [NOTICE] [59.92.179.107:1522-0#APVH_aquariusweb.in] oops! 503 Service Unavailable
    error_log:2009-06-30 06:10:22.121 [NOTICE] [59.92.179.107:1522-0#APVH_aquariusweb.in] Content len: 0, Request line:
    error_log:2009-06-30 06:10:22.121 [INFO] [59.92.179.107:1522-0#APVH_aquariusweb.in] File not found [/home/aquarius/public_html/503.shtml]

  18. Arvind says:

    Hello again

    Today I tried running the script and it runs but it is giving the following message:

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/aquarius/public_html/createdb/includes/ecurl.class.php on line 35

    Can you please tell me what this error is?

    Thanks,
    Arvind.

  19. White Shadow says:

    This means your server is running PHP in safe mode which is an obsolete, deprecated and wholly unpleasant mode of operation.

    You could try commenting out the offending line 35 in ecurl.class.php. That should get rid of the error, but I can’t guarantee something else wouldn’t crop up.

  20. Arvind says:

    Hello again

    I looked at the php info for my server and safe mode is off. Is this error due to the other part of the error message ie “….an open_basedir is set in /home/aquarius/public_html/createdb/includes/ecurl.class.php on line 35”

    Thanks

Leave a Reply