开发者

Multiple instances of mysql client and stdin

开发者 https://www.devze.com 2023-01-18 05:23 出处:网络
I have several hosts from which i want to do the same query. So imagine, i have on each server the database db and a table test like :

I have several hosts from which i want to do the same query. So imagine, i have on each server the database db and a table test like :

mysql> desc test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI |         |       | 
| data  | varchar(255)     | YES  |     | NULL    |       | 
+-------+------------------+------+-----+---------+-------+

Obviously, each table test has different data but they all have the same type. Let's say i have 2 host : h1 and h2 which host these tables.

Now i need to write a script which do the same query on each host and get the results on standard output. first idea was :

shell> (mysql -h h1 -u myusername -p mypwd -d db -e "select * f开发者_如何学JAVArom test"; 
        mysql -h h2 -u myusername -p mypwd -d db -e "select * from test";) 
       > out.txt

I want to do it faster, so what i did was :

shell> (mysql -h h1 -u myusername -p mypwd -d db -e "select * from test" & 
        mysql -h h2 -u myusername -p mypwd -d db -e "select * from test" &) 
       > out.txt

The problem is that i have some collision in my output file like

458 Karma police
459 876 Paint it black  Everything in its right place
460 street spirit

You have two rows on line 2.

My idea was that mysql buffer the result, so the buffer could end in the middle of a row. In this example, the buffer would stop at 459. But i can't figure out how to solve the problem.

Any ideas ?


Use a loop

for host in h1 h2 ; do
    mysql -h $host -u myusername -p mypwd -d db -e "select * from test" >> out.txt
done

Using >> appends to the file. Hosts will be accessed in order, not simultaneously, and so all results from h1 will be entered first, then all results from h2. If this is insufficient then there's another option but it's more complex.

0

精彩评论

暂无评论...
验证码 换一张
取 消