Internet marketing resources, ecommerce web site design tutorials and  just for fun - free cell phone ringtones!
  Taming the Beast - quality web marketing and ecommerce development services

Simple MySQL connection test

Posted by Michael Bloch in web development (Tuesday May 30, 2006 )

You know the scenario – you’re trying to install some software that requires MySQL, but it can’t seem to make a MySQL connection. Your hosting service says it’s the installation routine of the software, the software author says it’s your hosting service.

Here’s a simple script for testing connectivity which should help settle the issue. Here’s what to do:

1. Check your hosting account control interface to ensure that the database is present and you have a user set up for that database. Also ensure you have the right password for the db.

2. Create an empty file with a text editor and save it as mysqltest.php

3. Add the following to that file:

<?php $link = mysql_connect('hostname','dbuser','dbpassword'); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } echo 'Connection OK'; mysql_close($link); ?>

Replace hostname (usually localhost), username and password with the details you have for your database. There’s no need to add the database name.

4. Upload to your server to a folder that’s viewable via a browser.

5. Load the file in your browser

If a connection is made, the resulting page will display ‘Connection OK’. Talk again with the software vendor and give them the URL to the script.

If a connection cannot be made, then you’ll probably see something like:

Warning: mysql_connect(): Access denied for user ‘dbuser’@’localhost’ (using password: YES) in /path/to/mysqltest.php on line 2
Could not connect to MySQL: Access denied for user ‘dbuser’@’localhost’ (using password: YES)

Copy and paste that error and send it to your web host, along with the details of your database.

Don’t forget to remove the file once the issue is settled.



 

 
7 comments for Simple MySQL connection test
  1. I did the same but the the files shows nothing except a blank page. See http://www.gkcb.org/demo/mysqltest.php

    Could you please tell me what’s wrong. Here is the server info:
    http://www.gkcb.org/phpinfo.php

    Thanks
    Ramit

    Comment by Ramit Pal — September 7, 2006 @ 4:06 am

  2. Hi Ramit, if you’d like to send me a copy of the file you’re using as an attachment; I’ll take a look – just be sure change the username and the password in your file to something generic before sending it :). My email address is michael@tamingthebeast.net

    Comment by Michael Bloch — September 7, 2006 @ 4:26 am

  3. Normally people make mistake maily at the MySql server name. Usually we put ‘localhost’ in that. Whereas many hosting servers name it different as mysql1.domainname.com and something like that. If that is corrected it should work. Moreover many hosting servers require your username after creation should be registered for particular database. It is not enough to just create. You need to register

    Comment by Shasstra — September 30, 2006 @ 9:58 pm

  4. I like this mysqltest.php. I suggest adding ‘root’ as dbuser, and no password. This will probably give you the “connection ok” result.

    Comment by mike — June 11, 2007 @ 10:10 pm

  5. Thanks for the tip Mike!

    Comment by Michael Bloch — June 12, 2007 @ 5:39 am

  6. Thanks for this great little script. It really helped me troubleshoot a MySQL connection issue.

    Thanks again!

    Thom

    Comment by Thom King — February 24, 2008 @ 3:22 pm

  7. This is great!

    I got a unique result:

    “Could not connect to MySQL: Client does not support authentication protocol requested by server; consider upgrading MySQL client”

    I already contacted my hosting company, hopefully they can help me, THANKS SO MUCH!

    Comment by Irene — April 22, 2008 @ 1:55 pm

Sorry, the comment form is closed at this time.