ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

mysql sock error using code igniter

November 04, 2007 1:58am

Subscribe [3]
  • #1 / Nov 04, 2007 1:58am

    kingconnections

    13 posts

    I am moving this a site over for a client and it uses codeigniter.  I am a complete noob to this framework.  I got everything copied over and all. I changed the database info in the database.php file.  But I get this mysql can not connect to socket error.

    Can’t connect to local MySQL server through socket ‘/usr/local/mysql-5.0/data/mysql.sock’ (2)


    If I use the standard coding skills I am able to connect to the db fine.  ie


    $conn = mysql_connect(‘host.sever.net’,‘user1’,‘User1’);
    if (!conn) {
    die('Could not connect: ' . mysql_error());
    }
    echo ‘Connected successfully’;

    if (!mysql_select_db(“test”)) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
    }

    $sql = “SELECT email
          FROM   user
          “;

    $result = mysql_query($sql);

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

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you’re expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you’ll
    //    then create $userid, $fullname, and $userstatus
    while ($row = mysql_fetch_assoc($result)) {
    echo $row["email"];
    echo "
    ";
    }

    mysql_free_result($result);


    mysql_close($conn);

    Any ideas?  Any help would be great!!!

  • #2 / Nov 04, 2007 3:14am

    thurting

    213 posts

    I would check your config file again.  Also, make sure the DB allows connections from whatever server you are connecting from (though if you can connect using non CI methods this shouldn’t be an issue).

  • #3 / Nov 04, 2007 3:22pm

    llbbl

    324 posts

    this not a CI issue its something wrong with your mysql configuration.

  • #4 / Nov 04, 2007 4:25pm

    kingconnections

    13 posts

    So is there any way to change code igniter to use the standart mysql functions instead of a sock based call?  Like I said, the standard way works fine, this just happened when I put on codeigniter.

    Thanks for the replies!!!!

  • #5 / Nov 04, 2007 8:11pm

    llbbl

    324 posts

    Codeigniter uses the standard methods…

    Try looking at the CI db code? Try searching google for your problem. Sock errors are a result when you tried to start mysql when its already running or if your mysql configuration (my.cnf) is fubar or your didn’t start mysql properly or you are running more than one copy of mysql or something else like that… 

    This will take someone who knows about mysql and knows linux administration to poke around on your system till they figure it out. It is not as simple as download purple widget A and will fix your problem.

  • #6 / Nov 04, 2007 8:56pm

    kingconnections

    13 posts

    I understand about the socket error being a mysql config issue.  But that doesn’t really make since in this scenerio where I can connect and query the db using the coded published aboved, but not the codeigniter connections.  If it really was a mysql issue they I shouldn’t be able to write a sample script like the one I provided and have it work right?  Thanks for all the help.

  • #7 / Nov 05, 2007 1:57am

    kingconnections

    13 posts

    It almost seems like it is trying to use local socket. I am using a remote db, would that matter?  Where can I see what my connection string is for this?

  • #8 / Nov 05, 2007 2:12am

    llbbl

    324 posts

    In the /system/application/config/database.php file did you set the hostname correctly?

    Is the database driver mysql?

    $db['default']['dbdriver'] = "mysql";
  • #9 / Nov 05, 2007 2:15am

    kingconnections

    13 posts

    yep both are set correctly.  HMMM wierd.

  • #10 / Nov 05, 2007 3:11am

    kingconnections

    13 posts

    Hmm just got off the phone with godaddy.  Seems for some reason it is trying to use local connection instead of the remote one.  Anyone got any ideas?  Is there any debugging in this framework for these types of issues?

  • #11 / Nov 05, 2007 5:01am

    llbbl

    324 posts

    Again, doesn’t sound like a CI issue… lol

  • #12 / Nov 05, 2007 5:30am

    John_Betong

    690 posts

    Hi KingConnections,

    Try inserting this code in your config/dbase.php file immediately after the cache statements.
    I changed the $sql statement to suit my own tables.

    // TESTING
        $myhost  = $db['default']['hostname'];
        $myuser  = $db['default']['username'];
        $mydbase = $db['default']['database'];
        $mypass  = $db['default']['password'];
        $mysql   = 'SELECT * FROM jokes WHERE id < 10';
        
        echo '
    $myhost     ==> '    .$myhost;
        echo '
    $mydbase    ==> '     .$mydbase;
        echo '
    $myuser     ==> '     .$myuser;
        echo '
    $mypass     ==> '     .$mypass;
        echo '
    $mysql      ==> '     .$mysql;
        
        $conn = mysql_connect($myhost, $myuser, $mypass);
        if (!$conn) {
            die('
    Could not connect: ' . mysql_error());
        }
        echo '
    Connected successfully
    
    ';
        
        if (!mysql_select_db($mydbase)) {
            echo '
    Unable to select ==> ' .$mydbase . mysql_error();
            exit;
        }
        
        $sql         = "SELECT email     FROM user ";
            // I CHANGED THIS STATEMENT
        $result = mysql_query($mysql);
        
        if (!$result) {
            echo "
    Could not successfully run query ($sql) from ==> " .$mydbase . mysql_error();
        exit;
        }
        if (mysql_num_rows($result) == 0) {
            echo "
    No rows found, nothing to print so am exiting";
            exit;
        }
        
        // While a row of data exists, put that row in $row as an associative array
        // Note: If you’re expecting just one row, no need to use a loop
        // Note: If you put extract($row); inside the following loop, you’ll
        // then create $userid, $fullname, and $userstatus
        while ($row = mysql_fetch_assoc($result)) {
            // echo $row["email"];
            echo $row['id'] .' ==> ' ;
            echo $row['title'];
            echo "
    ";
        }
        
        mysql_free_result($result);
        mysql_close($conn); 
        die;
    // TESTING

    Results

      $myhost ==> localhost
      $mydbase ==> johnsjokes
      $myuser ==> john_betong
      $mypass ==> 0336-1462-5035
      $mysql ==> SELECT * FROM jokes WHERE id<10
      Connected successfully


      1 ==> The weighing machine
      2 ==> The elephant and the turtle
      3 ==> The hinge and the teapot
      4 ==> Stress management
      5 ==> The song
      6 ==> The gambler & taxi-driver
      7 ==> The newlyweds
      8 ==> Grandpa and his grand-daughter
      9 ==> The Christmas present

    Cheers,

    John_Betong
     

  • #13 / Nov 05, 2007 10:51am

    kingconnections

    13 posts

    John,
    Thanks I will try that and see what happens. 

    llbl,
    I don’t think it is a problem with code igniter itself.  I do think it is a configuration issue somewhere.  I am just trying to track it down.  The database I am using is a remote database.  You can not use socket to connect to a remote database in php/mysql.  So it doesn’t make any since that I am getting a socket error when my database is remote.  For remote databases you use tcp/ip type of mysql connection.  As I said I took this project over from someone and have no codedigniter experience.  I am not saying anything is wrong with codeigniter at all. 

    Thanks for all you help!!!

  • #14 / Nov 05, 2007 12:09pm

    kingconnections

    13 posts

    OK, something wierd is going on.  I did that and it did not work at all.  So I thought that maybe the database settings were not getting read at all.  I even tried to make it echo something out at the top of the screeen and no dice.  So I completely renamed the database.php file to database2.php, just trying to get a different error. And I got the exact same error.  How can it be reading that file if it is not there? 

    So to recap I completely removed the database.php file and it still redirected me to that error.  Maybe my path configuration is wrong?

  • #15 / Nov 05, 2007 5:53pm

    llbbl

    324 posts

    It connects over port 3306. Your webserver might have that outgoing port blocked or your database server might have that incoming port blocked.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases