一 環境說明:我有100多臺機器,須要去每臺機器上面的數據庫導出對應的數據,而且須要傳兩個參數服務器ip和數據庫id,導出來以後,再把全部的文件合成一個excle文件python
二 實現過程:mysql
1 首先本身整理一份ip對應id的文件,例如:sql
`3070c023c4294c6faff627467a2ba8b0 58.116.1.1`
2 寫一個導出數據的腳本shell
#!/bin/bash ip=$1 id=$2 mysql -uedu_platform -pedu_platform -h$ip --database edu_platform -e" SELECT COALESCE(bc.name,'-') AS '學校',t.device_count AS '綁定數量' FROM ( SELECT bs.school_business_key, COUNT(DISTINCT bu.device_number) AS device_count FROM base_user bu INNER JOIN bd_student bs ON bs.id = bu.id WHERE bu.category = 'student' AND bu.device_number IS NOT NULL AND bu.device_number <> '' AND bs.school_business_key = '$id' GROUP BY bs.school_business_key) t LEFT JOIN bd_school bc ON bc.business_key = t.school_business_key">/home/shell9-12/file/$1_$2.xls # 導出的數據都放到指定目錄下面 if [ $? == 0 ];then echo "INFO:導出成功" else echo "Error:導出失敗" fi
3 寫一個傳參數的腳本start.sh,注意:兩個參數不要寫反了
數據庫
c1 和c2是要傳遞的兩個參數,school.txt是你包含id和ip的文件信息bash
while read c1 c2 do sh /home/shell9-12/tongji.sh $c2 $c1 #執行第二個腳本, done < school.txt
而後你執行.start.sh腳本,會在/home/shell0-12/file這個目錄下生成不少xls文件
服務器
4 整理數據,把每一個文件的內容放到一個文件裏我用的python實現的ide
import os dir = "./file" # print(os.listdir(dir)) for name in os.listdir(dir): with open("%s/%s" % (dir, name), encoding="utf-8") as f, open("tongji02.xls", "a") as f1: for i in f: s = i.replace("學校 綁定數量", "").strip() if s: f1.write(s.strip()+"\n")