简体   繁体   English

如果其中之一未连接,如何在2个mysql数据库之间切换?

[英]How to switch between 2 mysql databases if one of them was not connected?

I have 2 database servers DRC and Production server.. 我有2个数据库服务器DRC和生产服务器。

I connected php config file with the Production server ip to connect its database (it is working fine), and i made an if statement if production database is not connected, not selected or dropped then go and connect with the DRC server to get its database. 我将php config文件与生产服务器ip连接以连接其数据库(工作正常),并且如果生产数据库未连接,未选择或删除,则发出了if语句,然后与DRC服务器连接以获取其数据库。

the code below make this action it is working fine for switching 2 databases on local host but it is not connecting to the DRC database if the Production one is not available. 下面的代码可以执行此操作,它可以在本地主机上切换2个数据库正常工作,但是如果生产1不可用,则它不连接到DRC数据库。

//Production constants
defined ('DB_SERVER')? null : define ("DB_SERVER" , "20.20.10.1");
defined ('DB_USER') ? null : define ("DB_USER" , "root");
defined ('DB_PASS') ? null : define ("DB_PASS" , "rootpass");
defined ('DB_NAME') ? null : define ("DB_NAME" , "dbname");

//DRC constants
defined ('DB_SERVER_DRC')? null : define ("DB_SERVER_DRC" , "20.20.10.2");
defined ('DB_USER_DRC') ? null : define ("DB_USER_DRC" , "root");
defined ('DB_PASS_DRC') ? null : define ("DB_PASS_DRC" , "rootpass");
defined ('DB_NAME_DRC') ? null : define ("DB_NAME_DRC" , "dbname");


function __construct() {
    $this->open_connection();
}

public function open_connection() {
    $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if (!$this->connection) {
        $this->open_connection_DRC();
    } else {
        $db_select = mysql_select_db(DB_NAME, $this->connection);
        if (!$db_select) {
            $this->open_connection_DRC();
        }
    }
}

public function open_connection_DRC() {
    $this->connection = mysql_connect(DB_SERVER_DRC, DB_USER_DRC, DB_PASS_DRC);
    if (!$this->connection) {
        die("Database connection failed: " . mysql_error());
    } else {
        $db_select = mysql_select_db(DB_NAME_DRC, $this->connection);
        if (!$db_select) {
            die("Database selection failed: " . mysql_error());
        }
    }
}

Any solution or other way to do that, Waiting for your positive response guys. 任何解决方案或其他方式,请等待您的积极响应。

mysql_connect - Returns a MySQL link identifier on success or FALSE on failure. mysql_connect-成功时返回MySQL链接标识符,失败时返回FALSE。 That's what you can do according to my knowledge. 根据我的知识,这就是您可以做的。 if it returns false connect to the other database. 如果返回false,则连接到另一个数据库。

Can you try with below change in open_connection_DRC function, as per PHP manual you need to pass 5th Boolean argument $new_link as true, when you are trying to open multiple connections. 您可以尝试在open_connection_DRC函数中进行以下更改吗?根据PHP手册,当您尝试打开多个连接时,需要将第5个布尔参数$ new_link传递为true。

I am assuming here, that you are connected to production sever and database is not there on production server, so you are trying to open new mysql connection to DRC server. 我在这里假设您已连接到生产服务器,并且生产服务器上没有数据库,因此您正在尝试打开与DRC服务器的新mysql连接。

public function open_connection_DRC() {
    $this->connection = mysql_connect(DB_SERVER_DRC, DB_USER_DRC, DB_PASS_DRC, true);

Also it is better to unset $this->connection once you found out that DB is not present on production server. 一旦发现生产服务器上不存在数据库,最好取消设置$ this-> connection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM