Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2011
    Posts
    3

    Unanswered: 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:
    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!

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    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
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    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!

  4. #4
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    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
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  5. #5
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •