shell 報錯'@'localhost' (using password: YES)'root

來源:互聯網
上載者:User

標籤:

 shell中寫了簡單的一段

energy.sh內容:

#!/bin/bashhost=127.0.0.1user=******passwd=******dbname=******tablename=device_value_300sct007p01_yearMONTH=$(date +%Y-%m)sqlstr="insert into device_value_300sct007p01_year (id,year,month,device_id,device_value_id,value,type) select replace(uuid(), ‘-‘, ‘‘),substring(month,1,4),month,device_id,device_value_id,sum(value),type from device_value_300sct007p01_month where month = ${MONTH} group by month,device_id,device_value_id"mysql -h $host -u$user -p$passwd -D $dbname -e ‘select * from device_value_300sct007p01_year‘

最簡單的一個查詢,就是報錯‘@‘localhost‘ (using password: YES)‘root ,使用者名稱密碼都正確
最後發現原因:多了\r,指令碼是在windows下寫的,由於windows和linux分行符號不一致導致

sh  -x energy.sh   

+ host=$‘127.0.0.1\r‘+ user=$‘******\r‘+ passwd=$‘******\r‘+ dbname=$‘******\r‘+ tablename=$‘device_value_300sct007p01_year\r‘++ date +%Y-%m+ MONTH=$‘2015-03\r‘+ sqlstr=‘insert into device_value_300sct007p01_year (id,year,month,device_id,device_value_id,value,type) select replace(uuid(), ‘\‘‘-‘\‘‘, ‘\‘‘‘\‘‘),substring(month,1,4),month,device_id,devi‘group by month,device_id,device_value_idalue_300sct007p01_month where month = 2015-03++ -h $‘127.0.0.1\r‘ $‘-uroot\r‘ $‘-pyunho201311\r‘ -D $‘xcloud\r‘ -e ‘select * from device_value_300sct007p01_year‘energy.sh: line 9: -h: command not found+ mysql $‘\r‘ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

每個變數後都多了\r,導致使用者名稱密碼錯誤
解決方案:

刪除檔案中的\r

tr -d "\r" <energy.sh  >energy1.sh

再次運行

sh  energy1.sh

 

+----------------------------------+------+---------+--------------------+-----------------+-------+------+| id                               | year | month   | device_id          | device_value_id | value | type |+----------------------------------+------+---------+--------------------+-----------------+-------+------+| a9be43fcd5cfc6c3e7e07aac6fd20621 | 2015 | 2015-02 | 110000001000000031 |               1 |  2.00 | sum  || a9be43fcd5cfc6c3e7e07aac6fd20622 | 2015 | 2015-03 | 110000001000000031 |               1 |  3.00 | sum  || a9be43fcd5cfc6c3e7e07aac6fd20623 | 2015 | 2015-04 | 110000001000000031 |               1 |  4.00 | sum  || a9be43fcd5cfc6c3e7e07aac6fd20627 | 2015 | 2015-01 | 110000001000000031 |               1 |  1.00 | sum  |+----------------------------------+------+---------+--------------------+-----------------+-------+------+

 

shell 報錯'@'localhost' (using password: YES)'root

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.