Support / KnowledgeBase

 
Search the KnowledgeBase Search

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

  • Applies to: (gs), All (dv)

  • Difficulty: Medium

  • Time needed: 30 minutes

  • Tools needed: AccountCenter access or Plesk administrator access

 
  • Applies to: (gs)
    • Difficulty: Medium
    • Time: 30
    • Tools needed: AccountCenter access, FTP
  • Applies to: All (dv)
    • Difficulty: Medium
    • Time: 30
    • Tools needed: Plesk administrator access, FTP

Overview

This article contains the basic information you need to connect from PHP to your MySQL database on your (mt) Media Temple service. The most common mistake is in the host name, so check there first:

  • (gs) Grid-Service: internal-db.s00000.gridserver.com
  • (dv) Servers: localhost

NOTE:

Do not use your domain name or IP address as the host name. This can result in an unreliable database server connection.

Requirements

Before you start, you'll need the following:

  • Host: internal-db.s00000.gridserver.comlocalhost
  • Username: db00000 or db00000_usernameSet in Plesk.
  • Password: Your password.
  • Database: Your database name. This should look like db00000_dbname.

You can see a summary of all these settings which are specific for your own server in the AccountCenter, under the Server GuideServer Guide.

Instructions

The host is based on your access domain. You may have to check the advanced section of your database settings to be able to specify this. For external connections, use external-db.s00000.gridserver.com instead and make sure your IP address has been added to the list of allowed external IPs. Click here for details.

For (dv) Dedicated-Virtual Server, localhost is the default host in most scripts and applications. You likely won't have to enter anything special for this parameter.

The default username with access to all of your databases is db followed by your site number, db00000. You can edit your database user password in the AccountCenter. See Database users on the (gs) Grid-Service for instructions on updating passwords and adding database users. If you want to test your username and password, try logging into phpMyAdmin.

This article has instructions for adding a database with a username and password.

CAUTION:

If you update the password for an existing database user, you must update it in any files or applications that use the old password. If you're not sure how to do this, you may want to create a new database user for your current project. The new database user will have the format db00000_username.

Your database name will be of the format db00000_db. You must create the database through the AccountCenter first. See How do I create a database? if you need to create your database.

Sample connection script

Below is a sample PHP connection script you can use to test your settings or as a model for your own scripts. Be sure to replace the four variables in the $link = mysql_connect(); line with your own information. Your information goes between the existing single quote marks.

Filename: mysql_connect.php


<?php
$link = mysql_connect(
   'internal-db.s00000.gridserver.com', 
   'db00000_dbname', 
   'password', 
   'db00000_username'
);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

<?php
$link = mysql_connect('localhost', 'username', 'password', 'database');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Resources

For more information regarding MySQL connections via PHP, please consult the help documentation released with your script or visit php.net.


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