mysql查詢結果匯出到檔案

2021-08-09 07:15:29 字數 1388 閱讀 1590

環境:mysql

進入mysql shell

mysql-u

username--

password=password--

database=database

然後輸入匯出查詢結果命令

select * from table1   into outfile '/data/test1.txt'
報錯error 1045 (28000): access denied for user 『username 『@』localhost』 (using password: yes)

意思是這個賬戶許可權不夠。

然後換用 root 賬戶進行以上操作。

執行以上匯出查詢結果命令。

報錯error 1 (hy000): can』t create/write to file 『/data/test1.txt』 (errcode: 13)

原因是 對路徑 /data 沒有寫的許可權。

檢視當前路徑, mysql shell

\! pwd
顯示

/data

驗證當前賬戶是否對 /data 路徑具有寫的許可權。 mysql shell

\t /data/test1.txt
如果出現:

mysql: can』t create/write to file 『/data』 (errcode: 21)

error logging to file 『/data』

表示不具有寫許可權。

如果只出現

logging to file 『/data/test1.txt』

表示具有寫的許可權。

如果對該路徑不具有寫的許可權。可以回到 linux shell, 對該路徑授權:

chmod

757 /data

然後再執行匯出查詢結果命令即可。

還有可以直接在linux shell 進行直接匯出查詢結果。

mysql -u username --password=password --database=database --execute=

'select * from table1 '

-x> /data/test2.txt

說明

以上匯出的查詢結果test1.txt 與 test2.txt, 資料形式是不一樣的。

test1.txt 中將每列資料每行展示。

test2.txt 以xml 形式展示。

參考文章:

mysql查詢結果匯出到檔案

方法一 直接執行命令 mysql select count 1 fromtable into outfile tmp test.xls query ok,31 rows affected 0.00 sec 在目錄 tmp 下會產生檔案test.xls 遇到的問題 mysql select count...

MySQL查詢結果匯出到Excel

mysql uroot p use databa xx select user,host,password from user into outfile tmp data.xls select b.name,a.title,a.answer from plugin faq question a jo...

postgresql 將查詢結果匯出到檔案

方法1 進入查詢終端,輸入 o aa.out 查詢結果將輸出到當前目錄的aa.out 檔案 方法2 將查詢語句寫a.sql中,alias sql2 export pgpassword xx psql h 192.168.1.107 p 5439 u dev d data資料庫名 sql2 c a.s...