If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem connecting site to existing database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-11, 13:42
RecklessCoder RecklessCoder is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
Problem connecting site to existing database

I am having trouble to get my website connecting to MySQL. I set everything up using MAMP.

If I use:
Code:
<?php
$db_host = 'localhost'; 
$db_username = 'root';  
$db_pass = 'root';  
 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
?>
I get the following error message:
Quote:
Warning: mysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/*****/Sites/Scripts/Connect_to_MySQL.php on line 11

Warning: mysql_connect() [function.mysql-connect]: No such file or directory in /Users/*****/Sites/Scripts/Connect_to_MySQL.php on line 11
could not connect to mysql
However, I can successfully connect using this:
Code:
<?php
mysql_connect(
  ':/Applications/MAMP/tmp/mysql/mysql.sock',
  'root',
  'root');
?>
Anyone know whats going on? How can I get the first code to work?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 06-12-11, 17:54
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Have a look at your my.cnf as this defines the location of the socket file that it is attempting to pick up. It is currently defined with /var/mysql/mysql.sock but according to the attempt that did work this should have been /Applications/MAMP/tmp/mysql/mysql.sock.

The location of the my.cnf is determined in the following order:

$HOME/.my.cnf
$MYSQL_HOME/my.cnf
/etc/mysql/my.cnf
/etc/my.cnf

Try to locate one of these and correct it. What you need to search for is the client section and change the configuration value in there. Here is a sample below:

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 06-12-11, 20:00
RecklessCoder RecklessCoder is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks for the response Ronan!

Today is my first day fooling around with MySQL/php so I'm not entirely able to solve the problem based off of what you wrote, but it definitely gave me enough info to do some research on my own. That in itself is a huge help. Thanks again!
Reply With Quote
  #4 (permalink)  
Old 06-13-11, 02:47
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
No problem, there are two ways in which you can connect to a database, the first way is using a host or ip address and a port (default is 3306), the second way is using sockets (on a UNIX/Linux/Mac OS) and on Windows shared memory. The sockets and shared memory can only be accessed from the local machine. If you were to perform a remote connection then it will use the IP and port method.

The file my.cnf is used to configure the default way the client connects to the database. In your case it is saying that it will connect with a socket. A socket on UNIX/LInux/Mac OS is a special file on the operating system. The MySQL server creates this file when it is launched and listens for any connections coming in on this file. The location of the socket file is key to access the database server. If the client attempts to connect to a file that does not exist then no connection can be established. So you need to make sure that the client connects correctly.

You have overwritten the default in the second PHP piece of code with the correct location to the socket file. This means that your client socket configuration in my.cnf does not appear to be correct.

If you need any more help just let us know.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 06-14-11, 00:35
RecklessCoder RecklessCoder is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
Once again you're a big help Ronan! That definitely helps put everything in its place. I'm now off and running (slowly)

Thanks!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On