How to execute a MySQL command from a shell script?

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

    How to execute an SQL script, use this syntax:

    mysql --host= localhost --user=root --password=xxxxxx  -e "source dbscript.sql"
    

    If you use host as localhost you don't need to mention it. You can use this:

    mysql --user=root --password=xxxxxx  -e "source dbscript.sql"
    

    This should work for Windows and Linux.

    If the password content contains a ! (Exclamation mark) you should add a \ (backslash) in front of it.

提交回复
热议问题