How to execute a MySQL command from a shell script?

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

    As stated before you can use -p to pass the password to the server.

    But I recommend this:

    mysql -h "hostaddress" -u "username" -p "database-name" < "sqlfile.sql"
    

    Notice the password is not there. It would then prompt your for the password. I would THEN type it in. So that your password doesn't get logged into the servers command line history.

    This is a basic security measure.

    If security is not a concern, I would just temporarily remove the password from the database user. Then after the import - re-add it.

    This way any other accounts you may have that share the same password would not be compromised.

    It also appears that in your shell script you are not waiting/checking to see if the file you are trying to import actually exists. The perl script may not be finished yet.

提交回复
热议问题