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 :
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.
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.
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?
Hmm, have you tried simply passing the password as a parameter to the mysql_easy_create() function? For example :
Just tried that, but to no success.
I can make do with a random password for the time being.
Thanks anyway
@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.
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
Hi,
Sorry for the error but I meant – add the custom user password to the db opposed to the random generated password.
Regards
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.
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
On test
Fantastic,
This has saved me a lot of trouble and works perfectly with hostpapa straight out of the box.
Many,many thanks
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
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.
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
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.
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]
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.
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.
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