简体   繁体   English

与 php class 连接到两个 mysql 数据库

[英]Connecting with php class to two mysql databases

I have a strange problem with my php class, which I use for connecting mysql database.我的 php class 有一个奇怪的问题,我用它来连接 mysql 数据库。 I tested on localhost and it works great, but after uploading to server it behaves like that:我在 localhost 上进行了测试,效果很好,但是在上传到服务器后,它的行为如下:

Here's connecting method from my class:这是我的 class 的连接方法:

public function __construct($server, $user, $pass, $database, $encoding = "utf8") {
  self::$counter++;

  if($this->connect==null) {
    if(self::$counter>1) { $new_link = true; } else { $new_link = false; }
    if($this->connect = MySQL_Connect($server, $user, $pass, $new_link)) {
      mysql_select_db($database, $this->connect);
      $this->execute("SET NAMES '"+$encoding+"'");

      $this->server = $server;
      $this->user = $user;
      $this->password = $pass;
      $this->database = $database;
      $this->encoding = $encoding;

      return true;
    }
      else {
      echo "Error while connecting database!";
    }
  }
}

And here's the connecting code:这是连接代码:

$SQL_server = "server1";
$SQL_user = "user1";
$SQL_pass = "pass1";
$Database = "database1";

$mysql = new mysql($SQL_server, $SQL_user, $SQL_pass, $Database);

$SQL_server2 = "server2";
$SQL_user2 = "user2";
$SQL_pass2 = "pass2";
$Database2 = "database2";

$mysql2 = new mysql($SQL_server2, $SQL_user2, $SQL_pass2, $Database2);

And the strange thing is, that error which I received is this: Access denied for user 'user2'@'%' to database 'database1'奇怪的是,我收到的错误是:用户'user2'@'%'拒绝访问数据库'database1'
It seems like variables mixed up or something like that, but I don't know why is it happening.似乎变量混合在一起或类似的东西,但我不知道为什么会这样。 When I delete the code for second connection, it works, but not together.当我删除第二次连接的代码时,它可以工作,但不能一起工作。 I though it could by error with my static variable counter (which counts number of connections) and it may cause that $new_link attribute is not set to true, but even when I set it to true without any condition, it still doesn't work.我虽然我的 static 变量计数器(它计算连接数)可能会出错,它可能导致 $new_link 属性未设置为 true,但即使我在没有任何条件的情况下将其设置为 true,它仍然不起作用.

I suffer from this problem before two days and i got solution for that is我在两天前遇到了这个问题,我得到了解决方案

Its server level issue not your code level它的服务器级别问题不是您的代码级别

for that you have to set one line in your server为此,您必须在服务器中设置一行

edit file named my.cnf it is in my /etc/my.cnf编辑名为 my.cnf 的文件,它位于我的 /etc/my.cnf

max_connections = 500最大连接数 = 500

Full File content is完整文件内容是

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
max_allowed_packet=100M
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
max_connections = 500

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


long_query_time = 0
slow_query_log = 1
slow_query_log_file=/var/log/mysqld.slow.query.log

SET GLOBAL general_log = 'ON';
log=/var/log/mysqld.general.log
/etc/my.cnf (END) 

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

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