Jump to content
  • 0

How to add a second login server to Flux?


puzzles

Question


  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  5
  • Reputation:   4
  • Joined:  12/04/11
  • Last Seen:  

For my server setup, I have two SQL databases, one on the athena server and one on the web server. The web server has replication databases of the athena server's databases. I want to have Flux using the web server's replicated databases when querying for data. This is where I have my problem; when a user wants to change account settings like sex, email or password, I want to connect to the main database and not the replicated one. I have narrowed it down and believe Flux CP makes the connection here (in files changepass.php, changesex.php, changemail.php, confirmemail.php):

$sth = $server->connection->getStatement($sql);

 

How can I alter this statement in the few locations it will be necessary so it connects to an alternative host address?

Edited by puzzles
Link to comment
Share on other sites

1 answer to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  5
  • Reputation:   4
  • Joined:  12/04/11
  • Last Seen:  

I solved this issue partially. I created a new value for the connect function to specify which server to connect to. This function works only when a user is logged out (useful for quick loading databases/who's online/rankings). It will not work for logged in characters because their connection is called at /lib/Flux/SessionData.php:355 which applies to all queries. This means when set to a replicated server it breaks gender changes/preferences/slot changes etc. If anyone knows a solution to this issue, please let me know.
 
Replace the connect, getConnection and getStatement functions at lib\Flux\connection.php with these: 

 

private function connect(Flux_Config $dbConfig, $altServer)
    {
        $dsn = 'mysql:';
        
        // Differentiate between a socket-type connection or an ip:port
        // connection.
        if ($sock=$dbConfig->getSocket()) {
            $dsn .= "unix_socket=$sock";
        }
        else {
            if(isset($altServer)) $dsn .= 'host='.Flux::config($altServer.'.Hostname');
            else $dsn .= 'host='.$dbConfig->getHostname();
            if ($port=$dbConfig->getPort()) {
                $dsn .= ";port=$port";
            }
        }
        
        // May or may not have a database name specified.
        if ($dbName=$dbConfig->getDatabase()) {
            if(isset($altServer)) $dsn .= ";dbname=".Flux::config($altServer.'.Database');
            else $dsn .= ";dbname=$dbName";
        }
        
        $persistent = array(PDO::ATTR_PERSISTENT => (bool)$dbConfig->getPersistent());
        if(isset($altServer)) return new PDO($dsn, Flux::config($altServer.'.Username'), Flux::config($altServer.'.Password'), $persistent);
        else return new PDO($dsn, $dbConfig->getUsername(), $dbConfig->getPassword(), $persistent);
    }   

 

private function getConnection($altServer)
    {
        if (!$this->pdoMain) {
            // Establish connection for main databases.
            $pdoMain       = $this->connect($this->dbConfig, $altServer);
            $this->pdoMain = $pdoMain;
            
            if ($encoding=$this->dbConfig->getEncoding()) {
                $sth = $this->getStatement("SET NAMES ?");
                $sth->execute(array($encoding));
            }
            if ($timezone=$this->dbConfig->getTimezone()) {
                $sth = $this->getStatement("SET time_zone = ?");
                $sth->execute(array($timezone));
            }
        }
        return $this->pdoMain;
    }
   public function getStatement($statement, $options = array(), $altServer=null)
    {
        $dbh = $this->getConnection($altServer);
        if(isset($options)) $sth = $dbh->prepare($statement, $options);
        else $sth = $dbh->prepare($statement);
        @$sth->setFetchMode(PDO::FETCH_CLASS, 'Flux_DataObject', array(null, array('dbconfig' => $this->dbConfig)));
        
        if ($sth) {
            return new Flux_Connection_Statement($sth);
        }
        else {
            return false;
        }
    }
Add the following to config\application.php:
    'MyServer' => array(
        'Hostname'        => '127.0.0.1',
        'Username'        => 'username',
        'Password'        => 'password',
        'Database'        => 'database'
    ),
    
Use this new function for the places you'd like to change servers:
getStatement($sql,null,'MyServer');
 
The item and monster database connections are both made at /lib/Flux/TemporaryTable.php:241
Link to comment
Share on other sites

×
×
  • Create New...