How to execute a MySQL command from a shell script?

前端 未结 14 1166
生来不讨喜
生来不讨喜 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:53

    You forgot -p or --password= (the latter is better readable):

    mysql -h "$server_name" "--user=$user" "--password=$password" "--database=$database_name" < "filename.sql"
    

    (The quotes are unnecessary if you are sure that your credentials/names do not contain space or shell-special characters.)

    Note that the manpage, too, says that providing the credentials on the command line is insecure. So follow Bill's advice about my.cnf.

提交回复
热议问题