SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    or die another mysql connect

    Alright gang,

    I am running two mirrored mysql servers that are splitting the load on mysql SELECT requests. I am sort of worried about one failing and leaving 50% of requests out to dry (since the split is 50/50).

    So, I was wondering if I could write my connection script to allow for using the other mysql connection if the first attempt fails

    PHP Code:
    $connection=mysql_connect ("xxx.xx.xx.xxx""user""pw") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("db_db",$connection); 
    Can I do or die mysql_connect to my other server option?

    Example: or die (mysql_connect ("xxx.xx.xx.xx2", "user", "pw"));

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, die is same as exit; it displays a message if included and terminates the program.

    You could do something like this:
    PHP Code:
    <?php
       
    if(!$connection=mysql_connect ("xxx.xx.xx.xxx""user""pw"))
           if(!
    $connection=mysql_connect ("xxx.xx.xx.xx2""user""pw"))
               or die (
    'I cannot connect to the database because: ' mysql_error());
       
    mysql_select_db ("db_db",$connection);
    ?>

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see a lot more people doing the !$connect way of making sure connection happens instead of doing the "or" like I learned way back when.

    In fact, I can't even find a php tutorial page for more uses on the OR option. Is there one?

    So, with that, there'd be no way to do:

    PHP Code:
    $connection=mysql_connect ("xxx.xx.xx.xxx""user""pw") or mysql_connect ("xxx.xx.xx.xx2""user""pw");

    //or get even crazier

    $connection=mysql_connect ("xxx.xx.xx.xxx""user""pw") or mysql_connect ("xxx.xx.xx.xx2""user""pw") or die(mysql_error()); 
    Trying to limit lines of code is all.

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Alright....here is what you should be doing. NEVER EVER use "or die()" in production code. NEVER EVER! You need to implement a real error handling system. Also your poor man load balancing is stupid. STOP DOING IT. Get some real database servers behind a real load balance. Doing it within your code is doomed to failure as you found out when one of them fails.


    Furthermore, never use die or exit in production code for any error handling. Never. Got it?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't like the load balance style? I have four mysql servers. One primary that handles all the INSERT/UPDATE queries, with the other three handling all the SELECT Queries only. MySQL has the technology built into it to link them.

    But yes, in my connection script I choose the mysql server (by choice of private IP) that it should go to. I was told this was a great way to do it by the server techs. I have a single web server running apache and mail, and it doesn't really break a sweat, though the database servers are working hard.

    Doing about 1.3M pages opened daily with the setup, and it did struggle until I got another mysql server added; now it's flying.

    I did have the decision early on to have duplicate web/mysql servers doing it all behind a load balancer, or start focusing on the mysql with remote mysql servers, since that was where all my headache was coming from.

    I'll change my code and get rid of the "or die" segments.

    Cheers
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, if you think a loadbalancer setup is way to go, I'm spending close to $4K/mo in servers, so I'm all ears on other options.

    Cheers
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    What logic was referring to is this line:
    I am sort of worried about one failing and leaving 50% of requests out to dry (since the split is 50/50).
    A proper load balancer would not have this issue; it should detect the server down and route all connections to the live server.

    That said; What you've done with a line of code like that is NOT balancing, unless you've somehow configured mysql server 1 to reject every other connection. It's "flood server 1 until it refuses to serve, then use server 2". It's fallback, not balancing.


    If-Then-Else.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is true. It's only a temporary solution. I'd rather have one or two servers hit hard then having half (or third) of the requests responding with database down. Right now the load is split, but a secondary just in case.

    The nice thing is that, except for a few peak moments of the day, the server will be able to handle to influx if the other server drops.

    Cheers
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.


Bookmarks

Posting Permissions

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