
我正在嘗試重定向所做語句的輸出裡面互動mysqlsh
環境。
我在 OSX 上的終端機內運行 mysqlsh,但在說明頁面中沒有找到合適的參數來實現重新路由。透過預設的「pipe grep > and_run.txt」重新路由輸出不起作用,因為 mysqlsh 環境有它自己的一組可接受的命令。
我的命令是:
:$ mysqlsh root@localhost:3306/my_schema
# now the mysqlsh interactive console is open with an active connection to my_schema
mysqlsh> shell.options.set('resultFormat','json')
mysqlsh> session.runSql("SELECT * FROM my_schema.my_table")
# prints the schema as json array - i would like to rerout this output
註:我依照一位樂於助人的使用者的建議從 SO 移走了這個問題(thx @user1934428)
答案1
實現我的目標的一種方法是不使用“交互模式”,mysqlsh
而是為每個語句開啟一個新連接。
這不是我的首選方法,因為我猜這會為每個命令初始化一個新會話,但它有效。
mysqlsh --uri root@localhost:3306/my_schema --sql --execute "select * from my_table;" --result-format=json/array > my_objects.json
我真的很感興趣/感謝更乾淨的解決方案。