Hi,

I'm hosted on 50gigs.net and have been getting this error whenever I try to run a script that accesses the database. This is the error that I get -

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Can anyone throw some light on this?

    You should check this path
    '/var/run/mysqld/mysqld.sock'

    Most probably it is not where the mysql socket is.
    THe other thing may be you have not activated mysql in control panel.

    Have you gotten some info about the socket?

      Hi Halojoy!

      Thank you for replying!

      I checked phpinfo() and see this

      MYSQL_SOCKET /var/run/mysqld/mysqld.sock

      also sockets are enabled.

      so that seems to be alright. I'm hosted on 50gigs.net.

        We may need to see the PHP code involved in creating the connection (e.g. [man]mysql_connect/man).

          Hi,

          Here's the code for the mysql connection

          <?php

          function get_db_conn() {
          $conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);

          if (!$conn) {
          echo "Unable to connect to DB: " . mysql_error();
          exit;
          }

          mysql_select_db($GLOBALS['db_name'], $conn);
          return $conn;
          }

          if (!mysql_select_db($GLOBALS['db_name'])) {
          echo "Unable to select db_name: " . mysql_error();
          exit;
          }

          function get_prints($user) {
          $conn = get_db_conn();
          $res = mysql_query('SELECT from, to, time FROM footprints WHERE to=' . $user . ' ORDER BY time DESC', $conn);

          if (!$res) {
          echo "Could not successfully run query ($sql) from DB: " . mysql_error();
          exit;
          }

          $prints = array();
          while ($row = mysql_fetch_assoc($res)) {
          $prints[] = $row;
          }
          return $prints;
          }

          The error that i'm getting is preceeded by "Unable to select db_name: "

            Unable to select db_name:

            That is good.
            Because this means you have successfully connected to MySQL.

            But there is no such database $GLOBALS['db_name']
            Either the name is wrong
            or you have to create such a database first. In controlpanel.

              Hi Halojoy,

              I checked my mysql admin and the database does exist. I even hardcoded the name into the function parameter and its the same thing.

                OKay. Good.
                You can connect to mysql.
                The database exist.

                I do not like you use $GLOBALS ... use something else
                because that is not what anyone use or should use.

                I am puzzled by one a couple of things here.
                You actually select 2 times with mysql_select_db()
                The second time:
                if (!mysql_select_db($GLOBALS

                It is probably not possible to select same db 2 times.

                <?php
                
                function get_db_conn() {
                $conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);
                
                if (!$conn) {
                echo "Unable to connect to DB: " . mysql_error();
                exit;
                }
                
                
                mysql_select_db($GLOBALS['db_name'], $conn);
                return $conn;
                }
                
                
                if (!mysql_select_db($GLOBALS['db_name'])) {
                echo "Unable to select db_name: " . mysql_error();
                exit;
                }

                  hi halojoy!

                  Thanks for the suggestion, i removed the second call and its working fine now. thanks a lot!

                    Write a Reply...