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


$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

No, die is same as exit; it displays a message if included and terminates the program.

You could do something like this:

<?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);
?>

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:


$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

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?

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

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

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.

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