Hello everyone, there was a task to automatically collect data from Mysql.

There are 20 servers, each of them has Mysql (the database table structures are all the same).

It is necessary for the script to connect to the servers and execute a specific request.

Question:

  • How best to implement this task?
  • Is it possible to execute mysql_connect() in a loop?

Who will advise what? Thank you in advance.

    3 answers 3

    Yeah, and if tomorrow you need to add another 20 servaks? Will you write more lines into the code? Based on the question - I understood it this way - there are N number of servers with the same in structure bases, and you need to do the same with each base. Then you can create a table (although you may come up with an array in the file), which will contain the settings for connecting to the database. So pseudocode about this

    1. We receive an array with a configuration of connections to servers.
    2. We pass through this array by loop.
    3. In the body of the loop we create a connection. We do something with the base. Close the connection.
    • The answer is not watched above? - Artem
    • I wrote my answer when your was not - Ale_x
    • And he did. But the speed of execution of this code leaves much to be desired. - icedev
    • 3 servers prescribed. Runtime 2.5 minutes = (no ice. - icedev
    • 2.5 minutes (no ice - relative rating) What do you do with the base - what request do you send? it is necessary to look for the reduction of the time nada in another it seems to me) Need more information - Ale_x
     $array=Array( 0=array('sname'=>'localhost','uname'=>'root','upass'=>'root','dbname'=>'name'), 1=array('sname'=>'localhost','uname'=>'root','upass'=>'root','dbname'=>'name'), 2=array('sname'=>'localhost','uname'=>'root','upass'=>'root','dbname'=>'name'), 3=array('sname'=>'localhost','uname'=>'root','upass'=>'root','dbname'=>'name'), ); Foreach ($array as $key=>$value) { $link= mysql_connect($value['sname'],$value['uname'],$value['upass']); mysql_select_db($value[dbname],); MYSQL_QUERY(); // тут запросы нужные mysql_close(); } 

    If it is easier and in a cycle of 20, then it is easier and faster than the answer above.

    ps may be that with the $ value messed up the keys, but this is not the point, the essence is in the answer. values, substitute your own.

    • I did this $ azs_arr = Array ('5.81.171.233' => '****', '5.119.205.138' => 'skpadm', '5.106.61.102' => '*****'); $ dbname = "oc"; foreach ($ azs_arr as $ err => $ key) {$ link = mysql_connect ($ err, $ key, $ dbpassword); and so on - icedev
    • Well, you can and so, you can put one more :) And take the answer. The main essence of the solution caught. - Artem

    "There are 20 servers, each of them has Mysql (the database table structures are all the same)." - I do not quite understand why it is needed, but it does not matter. And about such a thing as replication in MySQL, you did not think? Maybe it will be better?

    • The idea is interesting. But, replication will not work. Each of the 20 servers works in their own way, adds its own values ​​(DIFFERENT). - icedev