How to execute Mysql command from a shell script?

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.

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" 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" 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.

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. – Vaibhav Nov 8 '11 at 19:33 + mysql -h dbservername -u user-name -ppassword dbname : No such file or directoryids.

Sql + $'\r' : command not found2: This is the error message I got – Vaibhav Nov 8 '11 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.

– Bill Karwin Nov 8 '11 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 don't 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?

– Vaibhav Nov 8 '11 at 21:43.

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" 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.

You missed the "automated" part of the question, and temporarily removing the password is a Really Bad Idea. – PointedEars Nov 8 '11 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. – Grok Nov 8 '11 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. – Vaibhav Nov 8 '11 at 21:15 1 Vaibav: if you could put the actual shell script inside your question, I may be able to help further.

– Grok Nov 8 '11 at 21:24 perl fb_apps_frm_fb. Pl perl fb_new_spider. Pl ds_fbids.

Txt ds_fbids. Sql` mysql -h dbservername -u username -ppassword dbname.

You forgot -p or --password= (the latter is better readable): mysql -h "$server_name" "--user=$user" "--password=$password" "--database=$database_name" So follow Bill's advice about my.cnf.

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 -u -p database -e \ "SELECT * FROM blah WHERE foo='bar.

May be I was not clear about my problem – Vaibhav Nov 8 '11 at 21:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions