Support / KnowledgeBase

 
Search the KnowledgeBase Search

Why can't I connect to my MySQL database from PHP?

  • Applies to: (gs), (ss)

  • Difficulty: Easy

  • Time needed: 30 minutes

  • Tools needed: None

 

The following information can help you resolve connectivity issues with your databases on your (mt) Media Temple service.

NOTE:

Text in this color is used as an example. Please be sure to replace this text with the proper information for your site or server.  Be sure to replace #### with your (gs) site number.

  1. Make sure that your username and password are correct.
  2. Next, make sure that the database host is set to:
    1. for (ss) Shared-Server and (dv) Dedicated-Virtual - localhost
    2. for (gs) - internal-db.s#####.gridserver.com

    WARNING:

    Do not use your domain name or IP address. Database connections that do not use the above examples may work from time to time but are much more unreliable.



    For (ss) Shared-Server and (dv) Dedicated-Virtual Servers

    Your database name starts with your primary domain name with the "." replaced with an "_" and followed by "_-_". If your domain is mt-example.com, and your database name is friends, your database name would look like:
    • mt-example.com_-_friends

    Here are examples of how the PHP connect statement should look:

    $connection = mysql_connect("localhost",$username,$password);
    
    
    mysql_select_db("db_name", $connection);
    

    For (gs) Grid-Service

    Your database name starts with your database username followed by an underscore "_". If your username is db83, your database name would start with db83_, followed by the name you assign the database. This is known as a database name prefix.
    • db83_friends
    Here is an example of how the PHP connect statement should look:

    $link = mysql_connect('internal-db.s###.gridserver.com', 'db##', '<password>');
    mysql_select_db("db##_dbname", $link);

    NOTES:

    We do not allow any connections to the MySQL database via the ip (non-localhost). This is true whether you are trying to connect from the server itself or somewhere else on the internet. Your database location or server address must always be "localhost" unless you are on a (gs) Grid-Service.

    LINKS:
    For more information regarding MySQL connections via PHP, please consult the help documentation released with your script, or visit the main website of the creators of PHP, http://php.net.

    KB links for MySQL external connections:
    (dv) HOW TO: Enable non-localhost Connections for MySQL
    (gs) External MySQL Database Access using desktop-based software.

Revisions:

12-18-2008: Added Revisions Box

User Comments

No visitor comments posted. Post a comment

Fields marked with an asterisk(*) are required. Comment on this article

Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue