How to execute a MySQL command from a shell script?

前端 未结 14 1154
生来不讨喜
生来不讨喜 2020-11-28 01:28

How can I execute an SQL command through a shell script so that I can make it automated?

I want to restore data I have collected in a SQL file using a shell script.

14条回答
  •  余生分开走
    2020-11-28 01:50

    mysql -h "hostname" -u usr_name -pPASSWD "db_name" < sql_script_file
    

    (use full path for sql_script_file if needed)

    If you want to redirect the out put to a file

    mysql -h "hostname" -u usr_name -pPASSWD "db_name" < sql_script_file > out_file
    

提交回复
热议问题