175

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. I want to connect to a server and restore data. The command works when executed separately via SSH command line.

This is the command I use:

mysql -h "server-name" -u root "password" "database-name" < "filename.sql"

This is the shell script code that creates the file ds_fbids.sql and pipes it into mysql.

perl fb_apps_frm_fb.pl
perl fb_new_spider.pl ds_fbids.txt ds_fbids.sql
mysql -h dbservername -u username -ppassword dbname < ds_fbids.sql

What is the correct way to do this?

16 Answers 16

207

You need to use the -p flag to send a password. And it's tricky because you must have no space between -p and the password.

$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"

If you use a space after -p it makes the mysql client prompt you interactively for the password, and then it interprets the next command argument as a database-name:

$ mysql -h "server-name" -u "root" -p "XXXXXXXX" "database-name" < "filename.sql"
Enter password: <you type it in here>
ERROR 1049 (42000): Unknown database 'XXXXXXXX'

Actually, I prefer to store the user and password in ~/.my.cnf so I don't have to put it on the command-line at all:

[client]
user = root
password = XXXXXXXX

Then:

$ mysql -h "server-name" "database-name" < "filename.sql"

Re your comment:

I run batch-mode mysql commands like the above on the command line and in shell scripts all the time. It's hard to diagnose what's wrong with your shell script, because you haven't shared the exact script or any error output. I suggest you edit your original question above and provide examples of what goes wrong.

Also when I'm troubleshooting a shell script I use the -x flag so I can see how it's executing each command:

$ bash -x myscript.sh
10
  • Thanks for a quick response. I tired by putting the password in the command line itself. The real problem is I am putting this command in .sh file and then executing this shell script. The command in the file is not executed on command line, but the same command works perfectly fine when I execute only the command on command line.
    – MUFC
    Commented Nov 8, 2011 at 19:33
  • + mysql -h dbservername -u user-name -ppassword dbname</br> : No such file or directoryids.sql</br> + $'\r' : command not found2: This is the error message I got
    – MUFC
    Commented Nov 8, 2011 at 21:25
  • Ok, then I would infer that your current working directory is not where the ids.sql file is located. Also you may have embedded newlines in your script. Commented Nov 8, 2011 at 21:29
  • I do have new lines embedded in my shell script after every command. All that my shell script contain are 3 command line command which I dont want to run separately so i created a shell script to make them run without my intervention and I put newline afetr every commmand. Is that is causing problem ?
    – MUFC
    Commented Nov 8, 2011 at 21:43
  • 1
    @racl101, if you want it to be more clear in your scripts, use --password=xxxxxx Commented Jan 18, 2015 at 16:51
148

Use this syntax:

mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
4
  • 29
    Some more details about the options from the manual: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit
    – wranvaud
    Commented Sep 14, 2016 at 11:30
  • Could it run with a heredoc?
    – zx1986
    Commented Aug 10, 2018 at 12:04
  • 1
    @zx1986 Yes and No, to HEREDOC. Depends on how you mean to use it. Using it to replace the "command1;command2;....;commandn" part of this answer will not work. Using it to replace the use of the redirected file in the OP's syntax can work. I've addressed that issue in my answer to this question.
    – Chindraba
    Commented Jan 19, 2019 at 7:30
  • -s is redundant if -B is used
    – Tim
    Commented Feb 5, 2019 at 21:13
61

All of the previous answers are great. If it is a simple, one line sql command you wish to run, you could also use the -e option.

mysql -h <host> -u<user> -p<password> database -e \
  "SELECT * FROM blah WHERE foo='bar';"
2
  • 1
    Query within double("") quotes was what i need to do. Thanks Commented Feb 26, 2017 at 20:02
  • I see, and I take it you do need to include the semicolon at the end of the query?
    – Lori
    Commented Mar 6, 2020 at 14:26
23

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.

1
  • 1
    How to specify the database? should it be inside -e, like -e "use abc; source dbscript.sql"? Commented Jun 5, 2014 at 12:36
10

The core of the question has been answered several times already, I just thought I'd add that backticks (`s) have beaning in both shell scripting and SQL. If you need to use them in SQL for specifying a table or database name you'll need to escape them in the shell script like so:

mysql -p=password -u "root" -Bse "CREATE DATABASE \`${1}_database\`;
CREATE USER '$1'@'%' IDENTIFIED BY '$2';
GRANT ALL PRIVILEGES ON `${1}_database`.* TO '$1'@'%' WITH GRANT OPTION;"

Of course, generating SQL through concatenated user input (passed arguments) shouldn't be done unless you trust the user input.It'd be a lot more secure to put it in another scripting language with support for parameters / correctly escaping strings for insertion into MySQL.

7

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.

5

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.

9
  • 2
    You missed the "automated" part of the question, and temporarily removing the password is a Really Bad Idea. Commented Nov 8, 2011 at 19:15
  • I read it as "restore" and "automated" which means "automated but not forever". But like I said "if security is not a concern". I agree - it's a REALLY bad idea. Commented Nov 8, 2011 at 19:22
  • I am sorry if I have created confusion. What I meant by Automated is, I have two perl scripts which are used to generate the .sql file, but the command to dump that file in to DB is not run by the shell script, but it works absolutely file if I run that command on a command line. I want to eleminate the effort of running that command on command line and run it through the shell script itself.
    – MUFC
    Commented Nov 8, 2011 at 21:15
  • 1
    Vaibav: if you could put the actual shell script inside your question, I may be able to help further. Commented Nov 8, 2011 at 21:24
  • perl fb_apps_frm_fb.pl </br> perl fb_new_spider.pl ds_fbids.txt ds_fbids.sql` </br> mysql -h dbservername -u username -ppassword dbname < ds_fbids.sql
    – MUFC
    Commented Nov 8, 2011 at 21:30
5
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
2
  • @Gus, First of all thanks for the valuable comments. It worked like a charm for me. I want the output to be an excel or .csv file. how can I achieve that. Thanks in Advance. Commented Nov 20, 2014 at 16:37
  • @Ash_and_Perl I only edited this answer, thank vine not me, it's his answer. If you have a question of your own, and you have tried to find a solution on your own already, I suggest you create a question. That way you can detail what you tried, how it failed, and people can give you a full, complete answer (and get points for it!).
    – Gus
    Commented Nov 20, 2014 at 22:25
3

Use

echo "your sql script;" | mysql -u -p -h db_name
3

To "automate" the process of importing the generated .sql file, while avoiding all the traps that can be hidden in trying to pass files through stdin and stdout, just tell MySQL to execute the generated .sql file using the SOURCE command in MySQL.

The syntax in the short, but excellent, answer, from Kshitij Sood, gives the best starting point. In short, modify the OP's command according to Kshitij Sood's syntax and replace the commands in that with the SOURCE command:

#!/bin/bash
mysql -u$user -p$password $dbname -Bse "SOURCE ds_fbids.sql
SOURCE ds_fbidx.sql"

If the database name is included in the generated .sql file, it can be dropped from the command.

The presumption here is that the generated file is valid as an .sql file on its own. By not having the file redirected, piped, or in any other manner handled by the shell, there is no issue with needing to escape any of the characters in the generated output because of the shell. The rules with respect to what needs to be escaped in an .sql file, of course, still apply.

How to deal with the security issues around the password on the command line, or in a my.cnf file, etc., has been well addressed in other answers, with some excellent suggestions. My favorite answer, from Danny, covers that, including how to handle the issue when dealing with cron jobs, or anything else.


To address a comment (question?) on the short answer I mentioned: No, it cannot be used with a HEREDOC syntax, as that shell command is given. HEREDOC can be used in the redirection version syntax, (without the -Bse option), since I/O redirection is what HEREDOC is built around. If you need the functionality of HEREDOC, it would be better to use it in the creation of a .sql file, even if it's a temporary one, and use that file as the "command" to execute with the MySQL batch line.

#!/bin/bash
cat >temp.sql <<SQL_STATEMENTS
...
SELECT \`column_name\` FROM \`table_name\` WHERE \`column_name\`='$shell_variable';
...
SQL_STATEMENTS
mysql -u $user -p$password $db_name -Be "SOURCE temp.sql"
rm -f temp.sql

Bear in mind that because of shell expansion you can use shell and environment variables within the HEREDOC. The down-side is that you must escape each and every backtick. MySQL uses them as the delimiters for identifiers but the shell, which gets the string first, uses them as executable command delimiters. Miss the escape on a single backtick of the MySQL commands, and the whole thing explodes with errors. The whole issue can be solved by using a quoted LimitString for the HEREDOC:

#!/bin/bash
cat >temp.sql <<'SQL_STATEMENTS'
...
SELECT `column_name` FROM `table_name` WHERE `column_name`='constant_value';
...
SQL_STATEMENTS
mysql -u $user -p$password $db_name -Be "SOURCE temp.sql"
rm -f temp.sql

Removing shell expansion that way eliminates the need to escape the backticks, and other shell-special characters. It also removes the ability to use shell and environment variables within it. That pretty much removes the benefits of using a HEREDOC inside the shell script to begin with.

The other option is to use the multi-line quoted strings allowed in Bash with the batch syntax version (with the -Bse). I don't know other shells, so I cannot say if they work therein as well. You would need to use this for executing more than one .sql file with the SOURCE command anyway, since that is not terminated by a ; as other MySQL commands are, and only one is allowed per line. The multi-line string can be either single or double quoted, with the normal effects on shell expansion. It also has the same caveats as using the HEREDOC syntax does for backticks, etc.

A potentially better solution would be to use a scripting language, Perl, Python, etc., to create the .sql file, as the OP did, and SOURCE that file using the simple command syntax at the top. The scripting languages are much better at string manipulation than the shell is, and most have in-built procedures to handle the quoting and escaping needed when dealing with MySQL.

3

I have written a shell script which will read data from properties file and then run mysql script on shell script. sharing this may help to others.

#!/bin/bash
    PROPERTY_FILE=filename.properties

    function getProperty {
       PROP_KEY=$1
       PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
       echo $PROP_VALUE
    }

    echo "# Reading property from $PROPERTY_FILE"
    DB_USER=$(getProperty "db.username")
    DB_PASS=$(getProperty "db.password")
    ROOT_LOC=$(getProperty "root.location")
    echo $DB_USER
    echo $DB_PASS
    echo $ROOT_LOC
    echo "Writing on DB ... "
    mysql -u$DB_USER -p$DB_PASS dbname<<EOFMYSQL

    update tablename set tablename.value_ = "$ROOT_LOC" where tablename.name_="Root directory location";
    EOFMYSQL
    echo "Writing root location($ROOT_LOC) is done ... "
    counter=`mysql -u${DB_USER} -p${DB_PASS} dbname -e "select count(*) from tablename where tablename.name_='Root directory location' and tablename.value_ = '$ROOT_LOC';" | grep -v "count"`;

    if [ "$counter" = "1" ]
    then
    echo "ROOT location updated"
    fi
1
  • If you are a root user then using "sudo mysql" plainly will also work. auth_socket comes enabled by default for the root user. Was looking if there's any way of interpolating SQL queries with bash variables. And this answered it.
    – Blaze
    Commented Nov 24, 2020 at 10:04
2

An important consideration for accessing mysql from a shell script used in cron, is that mysql looks at the logged in user to determine a .my.cnf to load.

That does not work with cron. It can also get confusing if you are using su/sudo as the logged in user might not be the user you are running as.

I use something like:

mysql --defaults-extra-file=/path/to/specific/.my.cnf -e 'SELECT something FROM sometable'

Just make sure that user and group ownership and permissions are set appropriately and tightly on the .my.cnf file.

2

To take input from terminal using script shell and send it to mysql, write this command in shell script or bash script.

echo "Creating MySQL user and database"

echo "What is your DB_NAME?"

read db_name

echo "What is your DB_PASS?"

read db_pass

echo "What is your DB_HOST?"

read db_host

echo "What is your USER_NAME?"

read user_name

echo "What is your USER_PASS?"

read user_pass

mysql -u$db_name -p$db_pass -h$db_host -e "CREATE USER '$user_name'@'%' IDENTIFIED BY '$user_pass';GRANT SELECT ON *.* TO '$user_name'@'%';"

echo "YOU HAVE SUCCESSFULLY CREATED A USER $user_name WITH PASSWORD $user_pass"
1
#!/bin/sh
#Procedures = update
#Scheduled at : Every 00.05 

v_path=/etc/database_jobs
v_cnt=0

MAILTO="[email protected] [email protected] [email protected]"
touch "$v_path/db_db_log.log"

#test
mysql -uusername -ppassword -h111.111.111.111 db_name -e "CALL functionName()" > $v_path/db_db_log.log 2>&1
if [ "$?" -eq 0 ]
  then
   v_cnt=`expr $v_cnt + 1`
  mail -s "db Attendance Update has been run successfully" $MAILTO < $v_path/db_db_log.log
 else
   mail -s "Alert : db Attendance Update has been failed" $MAILTO < $v_path/db_db_log.log
   exit
fi
1
mysql_config_editor set --login-path=storedPasswordKey --host=localhost --user=root --password

How do I execute a command line with a secure password?? use the config editor!!!

As of mysql 5.6.6 you can store the password in a config file and then execute cli commands like this....

mysql --login-path=storedPasswordKey ....

--login-path replaces variables... host, user AND password. excellent right!

1
0

automate code mysql in shell script

this code connection to mysql by shell

#!/bin/bash
sudo mysql -u root -p "--password=pass"

OR

In addition connect, show value one tabel

#!/bin/bash
sudo mysql -u root -p "--password=4310260985" "database" 
-e "select * from table order by id desc;"

Hint: -e =======> You can get this -Bse

Not the answer you're looking for? Browse other questions tagged or ask your own question.