mysql下通過shell指令碼插入資料

來源:互聯網
上載者:User

標籤:mysql

一、建立資料庫及表

建立資料庫

create database dic character set utf8;

建表

CREATE TABLE `dic` (  `id` int(22) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘id‘,  `principal` varchar(213) CHARACTER SET utf8 NOT NULL COMMENT ‘負責人‘,  `use` varchar(222) CHARACTER SET utf8 NOT NULL COMMENT ‘用途‘,  `type` varchar(222) CHARACTER SET utf8 NOT NULL COMMENT ‘機器類型‘,  `model` varchar(222) CHARACTER SET utf8 NOT NULL COMMENT ‘機器型號‘,  `place` varchar(222) CHARACTER SET utf8 NOT NULL COMMENT ‘位置‘,  `IP` varchar(222) CHARACTER SET utf8 NOT NULL COMMENT ‘IP‘,  `addtime` datetime NOT NULL COMMENT ‘時間‘,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;

二、編寫相關指令碼

注意事項

mysql -u使用者名稱 -p密碼 -e "命令;"  多命令都寫在雙引號裡面,通過分號分割。
INSERT INTO `表名` (`欄位`,`欄位`) VALUES (值,值);


以下兩個指令碼都可以實現

#!/bin/bashread -p "please enter you mysql password:" passwdTABLE="dic"query="NULL,‘張偉易‘,‘新dns伺服器1‘,‘實體機‘,‘DELL R410‘,‘14層A5‘,‘10.240.210.61‘,‘2015-06-06 13:04:09‘"echo $query/usr/bin/mysql dic -u root -p$passwd <<EOFINSERT INTO $TABLE VALUE($query);#EOF
#!/bin/bashread -p "please enter you mysql password:" passwdTABLE="dic"query="NULL,‘張偉易‘,‘新dns伺服器1‘,‘實體機‘,‘DELL R410‘,‘14層A5‘,‘10.240.210.61‘,‘2015-06-06 13:04:09‘"echo $query/usr/bin/mysql dic -u root -p$passwd -e "INSERT INTO $TABLE VALUE($query);"

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/0A/wKioL1VykizyyFU1AAMBmkc9fpI850.jpg" title="QQ20150606141720.jpg" alt="wKioL1VykizyyFU1AAMBmkc9fpI850.jpg" />



此外,還可以通過將普通文字格式設定化輸出後匯入到資料庫中

這裡以test1作為樣本

[[email protected] lxh]# cat test1小明    新伺服器1    實體機    DELL R410    14層A5    10.240.210.61

指令碼如下

#!/bin/bash>test_newread -p "please enter you mysql password:" passwdMYSQL=/usr/bin/mysqlTABLE="dic"TIME=`date +%F\ %T`P="\e[01;35m"#sed "s/\(^.*$\)/\1,$TIME/" test >> test_new##在每行行尾添加時間cat test1 | sed ‘s/^/NULL\t/g‘ |sed ‘s/\t/,/g‘ |sed "s/\(^.*$\)/\1,$TIME/" >>test_newcat test_new |while read line#echo "$TIME" >>mysql_err_logdoecho $linequery=`echo "$line" |awk -F ","  ‘{ printf("%s,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"", $1, $2, $3, $4, $5, $6, $7 , $8)}‘`echo $query 1>>mysql_err_log 2>&1echo $query$MYSQL dic -u root -p$passwd  <<EOFINSERT INTO $TABLE VALUES($query);EOFdone#echo -e " $P***********************$TIME**********************************" >>mysql_err_log

執行指令碼

[[email protected] lxh]# sh mysqltest.shNULL,小明,新伺服器1,實體機,DELL R410,14層A5,10.240.210.61,2015-06-06 14:26:07NULL,"小明","新伺服器1","實體機","DELL R410","14層A5","10.240.210.61","2015-06-06 14:26:07"

插入成功

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/0B/wKioL1VynFizALA3AAPIAsLuMl0934.jpg" title="qwe.jpg" alt="wKioL1VynFizALA3AAPIAsLuMl0934.jpg" />

mysql下通過shell指令碼插入資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.