Page 1 of 1

multiple mysql server

Posted: Tue Aug 12, 2008 3:41 am
by memborg
Hi there

I am developing a centralized admin module in an environment with multiple mysql database servers which contains multiple databases. Depending on the data I want to recieve I connect to a the desired mysql server. The admin is used to administer several websites through a central CMS system. The users only need to know where they log in to edit their sites, but the sites can be placed on several different hosts.

My db_connect code

Code: Select all

 
function db_connect($in_db = 'custommain', $ip = ''){
    $db_name = $in_db;
    
    if($db_name == 'custommain'){
        $mysql_access1 = mysql_connect(admin_db_host, admin_db_user, admin_db_pass) or die ('Database error!');
        mysql_select_db($db_name, $mysql_access1);
        
        return $mysql_access1;
        
    }else {
 
        if($ip != ''){
            $arrHosts = $GLOBALS['arrHosts'];
            $mysql_access2 = mysql_connect($ip, $arrHosts[$ip]['db_user'], $arrHosts[$ip]['db_pass'], TRUE) or die ('Database error!');
            mysql_select_db($db_name, $mysql_access2);
            echo mysql_error();
            
            return $mysql_access2;
            
        }else{
            $mysql_access3 = mysql_connect(db_host, db_user, db_pass) or die ('Database error!');
            mysql_select_db($db_name, $mysql_access3);
            echo mysql_error();
            
            return $mysql_access3;
        }
    }
}
 
As you can see I have a default action for admin, when I call it wihe custommain. The problem occurs when I want to make a new connect, to a different host from admin. The host IP is not changed at all. It is still the IP from when I connected to the default server.

I've also made a wrapper

Code: Select all

 
function dbCloseOpen($in_db, $ip = ''){
    mysql_close($GLOBALS['db']);
    unset($GLOBALS['db']);
    $GLOBALS['db'] = db_connect($in_db, $ip);
}
 
To be sure that the previous connection is reset.

I hope some one can tell me what I'm doing wrong here or if this is the wrong approah.

Re: multiple mysql server

Posted: Tue Aug 12, 2008 8:13 am
by nowaydown1
Greetings,

I didn't see anything in your code that really stuck out to me as the cause of your problem. If it's not connecting to the correct database, I would try to echo both of your parameters that are being passed to your functions. It's possible that something is getting passed that shouldn't be.

When in doubt, break stuff. :lol:

Re: multiple mysql server

Posted: Wed Aug 13, 2008 6:43 am
by memborg
I've have seemed to located the problem.

When I first open a connection it is to the default server 217.195.176.230 later on I have to open a new connection to another server where the website i located, 217.195.176.157.

Everything gets passed to the function but when I try to open the new connection is uses the old IP address and not the new one.
I even call mysql_connect this way: mysql_connect(ip, user, pass, true) to force it to make a new link, but it still uses the first ip address