简体   繁体   English

2个不同主机之间的2个MySQL连接

[英]2 MySQL connections across 2 differents host

I have 2 connection database (MySQL) in different host like this: 我在不同的主机中有2个连接数据库(MySQL),如下所示:

//database 1
$dbhost='localhost';
$dbuser='user1'; 
$dbpass='pass1';
$dbname='dbname1';

//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';  

function dbconnection($dbhost,$dbuser,$dbpass,$dbname){
    if(!mysql_connect($dbhost,$dbuser,$dbpass)){
        echo "Error occure on opening connection or database. Period.";
    }else{
        if(!mysql_select_db($dbname)){
            echo "Error occure on select databases !";
        }
    }
}
function dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams){
    $cxn = mysql_connect($dbhostams,$dbuserams,$dbpassams,$dbnameams);
    if( $cxn === FALSE ) {  
        die('mysql connection error: '.mysql_error()); 
    }else{
        if( !mysql_select_db($dbnameams) ){
            echo "Error occure on select databases !";
        }
    }
}

when i use: 当我使用时:

dbconnection($dbhost,$dbuser,$dbpass,$dbname);

at my page code, and use: 在我的页面代码中,并使用:

dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams);

at another line of code in same page, error occured, like this: 在同一页的另一行代码中,发生了如下错误:

Warning: Access denied for user: 'apache@localhost' (Using password: NO) in
/home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php on line 17

Warning: MySQL Connection Failed: Access denied for user: 'apache@localhost'
(Using password: NO) in /home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php
on line 17

mysql connection error:

what must i do to solve this problem? 我该怎么做才能解决这个问题?

You need to pass true as fourth parameter to the mysql_connect function. 您需要将true作为第四个参数传递给mysql_connect函数。

$database1 = mysql_connect($host1, $user1, $pass1); 
$database2 = mysql_connect($host2, $user2, $pass2, true); 

mysql_select_db('database1', $database1);
mysql_select_db('database2', $database2);

And to query your database use this: 要查询数据库,请使用以下命令:

mysql_query('SELECT * FROM table', $database1);
mysql_query('SELECT * FROM table', $database2);

Check this answer here on Stackoverflow.... link 在Stackoverflow ...上检查此答案... 链接

First of all, change the variable names, it will be easier for you to read the code ... Second, the problem seems to be with the connection, as it is detecting that the username is "apache". 首先,更改变量名,这将使您更容易阅读代码...第二,问题似乎出在连接上,因为它正在检测到用户名是“ apache”。 Check the variables names in the functions ... change them and try again. 检查函数中的变量名称...对其进行更改,然后重试。

//database 1
$dbhost='localhost';
$dbuser='user1'; 
$dbpass='pass1';
$dbname='dbname1';

//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';  

function dbconnection($_dbhost,$_dbuser,$_dbpass,$_dbname){
    if(!mysql_connect($_dbhost,$_dbuser,$_dbpass)){
        echo "Error occure on opening connection or database. Period.";
    }else{
        if(!mysql_select_db($_dbname)){
            echo "Error occure on select databases !";
        }
    }
}
function dbconnectionams($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams){
    $cxn = mysql_connect($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams);
    if( $cxn === FALSE ) {  
        die('mysql connection error: '.mysql_error()); 
    }else{
        if( !mysql_select_db($dbnameams) ){
            echo "Error occure on select databases !";
        }
    }
}

Your global variables ( $dbhost , $dbuser , etc.) are not within the scope of your function calls; 全局变量( $dbhost$dbuser等)不在函数调用的范围内; for example, if those function calls take place within another function, you will need to declare the variables within the function with the global keyword in order to access them (otherwise PHP thinks you're referring to different variables of the same name that are local to the function): 例如,如果这些函数调用发生在另一个函数中,则需要使用global关键字声明该函数中的变量才能访问它们(否则,PHP会认为您引用的是本地具有相同名称的不同变量该功能):

function foo() {
  global $dbhost, $dbuser, $dbpass, $dbname;
  dbconnection($dbhost,$dbuser,$dbpass,$dbname);
}

Read more on PHP variable scope . 阅读有关PHP变量作用域的更多信息。

I didn't see anything obviously wrong, I tested your code as provided and both connections worked. 我没有发现任何明显的错误,我按提供的方式测试了您的代码,并且两个连接均正常工作。

Check to see if mysql.default_user , mysql.default_host etc. is set within your php.ini 检查是否在php.ini中设置了mysql.default_usermysql.default_host等。

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

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