Why can't I connect to my MySQL database from PHP?
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.
- Make sure that your username and password are correct.
- Next, make sure that the database host is set to:
- for (ss) Shared-Server and (dv) Dedicated-Virtual - localhost
- for (gs) - internal-db.s#####.gridserver.com
- mt-example.com_-_friends
- db83_friends
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:
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.
$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
Fields marked with an asterisk(*) are required. Comment on this article