国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

shell處理mysql增、刪、改、查

2019-11-15 02:11:57
字體:
供稿:網(wǎng)友
shell處理MySQL增、刪、改、查引言

????這幾天做一個任務,比對兩個數(shù)據(jù)表中的數(shù)據(jù),昨天用php寫了一個版本,但考慮到有的機器沒有php或者php沒有編譯mysql擴展,就無法使用mysql系列的函數(shù),腳本就無效了,今天寫個shell版本的,這樣,在所有linux系列機器上就都可以運行了。

shell是如何操作mysql的?

????shell操作mysql其實就是通過mysql命令通過參數(shù)去執(zhí)行語句,跟其他程序里面是一樣的,看看下面這個參數(shù):

-e, --execute=name  Execute command and quit. (Disables --force and history file.)

因此我們可以通過mysql -e來執(zhí)行語句,就像下面這樣:

mysql -hlocalhost -P3306 -uroot -p123456 $test --default-character-set=utf8 -e "select * from users"

執(zhí)行之后返回下面結(jié)果:

在shell腳本中操作mysql導出數(shù)據(jù)
MYSQL="mysql -h192.168.1.102 -uroot -p123456 --default-character-set=utf8 -A -N"#這里面有兩個參數(shù),-A、-N,-A的含義是不去預讀全部數(shù)據(jù)表信息,這樣可以解決在數(shù)據(jù)表很多的時候卡死的問題#-N,很簡單,Don't write column names in results,獲取的數(shù)據(jù)信息省去列名稱sql="select * from test.user"result="$($MYSQL -e "$sql")"dump_data=./data.user.txt>$dump_dataecho -e "$result" > $dump_data#這里要額外注意,echo -e "$result" > $dump_data的時候一定要加上雙引號,不讓導出的數(shù)據(jù)會擠在一行#下面是返回的測試數(shù)據(jù)3       吳彥祖  325       王力宏  326       ab      327       黃曉明  338       anonymous       32
插入數(shù)據(jù)
#先看看要導入的數(shù)據(jù)格式,三列,分別是id,名字,年齡(數(shù)據(jù)是隨便捏造的),放入data.user.txt12 tf 2313 米勒 2414 西安電子科技大學 9015 西安交大 9016 北京大學 90#OLF_IFS=$IFS#IFS=","#臨時設置默認分隔符為逗號cat data.user.txt | while read id name agedosql="insert into test.user(id, name, age) values(${id}, '${name}', ${age});"$MYSQL -e "$sql"done

輸出結(jié)果

+----+--------------------------+-----+| id | name                     | age |+----+--------------------------+-----+| 12 | tf                       |  23 || 13 | 米勒                   |  24 || 14 | 西安電子科技大學 |  90 || 15 | 西安交大             |  90 || 16 | 北京大學             |  90 |+----+--------------------------+-----+
更新數(shù)據(jù)
#先看看更新數(shù)據(jù)的格式,將左邊一列替換為右邊一列,只有左邊一列的刪除,下面數(shù)據(jù)放入update.user.txttf twoFile西安電子科技大學 西軍電西安交大 西安交通大學北京大學cat update.user.txt | while read src dstdoif [ ! -z "${src}" -a ! -z "${dst}" ]thensql="update test.user set name='${dst}' where name='${src}'"fiif [ ! -z "${src}" -a -z "${dst}" ]thensql="delete from test.user where name='${src}'"fi$MYSQL -e "$sql"done

輸出結(jié)果:

+----+--------------------------+-----+| id | name                     | age |+----+--------------------------+-----+| 12 | twoFile                  |  23 || 13 | 米勒                   |  24 || 14 | 西軍電 |  90 || 15 | 西安交通大學         |  90 |+----+--------------------------+-----+
dump數(shù)據(jù)到sql文件
#利用mysqldump這個命令可以很輕松的導出所有數(shù)據(jù)的sql語句到指定文件#導出root@localhost下面的exp.Opes中的所有數(shù)據(jù)到tt.sqlmysqldump -h localhost -u root -p exp Opes > ./tt.sql#回車之后輸入密碼就可以將所有sql語句輸出到tt.sql
導入數(shù)據(jù)到mysql數(shù)據(jù)庫
#設置編碼,不然可能出現(xiàn)亂碼mysql -hlocalhost -uroot --default-character-set=gbk -p exp< ./tt.sql#回車之后輸入密碼,導入tt.sql中的所有數(shù)據(jù)到exp數(shù)據(jù)庫中

本文版權(quán)歸作者iforever[]所有,未經(jīng)作者本人同意禁止任何形式的轉(zhuǎn)載,轉(zhuǎn)載文章之后必須在文章頁面明顯位置給出作者和原文連接。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜川市| 南京市| 荔浦县| 新竹市| 金沙县| 普格县| 修水县| 上蔡县| 囊谦县| 文昌市| 巴彦淖尔市| 宜良县| 错那县| 伊宁市| 锦州市| 广安市| 丽水市| 佳木斯市| 合作市| 长沙市| 海丰县| 拜泉县| 合水县| 万载县| 穆棱市| 雅江县| 临朐县| 京山县| 胶州市| 汉沽区| 江阴市| 页游| 永和县| 白山市| 井陉县| 西昌市| 特克斯县| 景德镇市| 尉氏县| 泰安市| 贡山|