Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to log in to a database by running a SQL file from a UNIX script without supplying username and password (as root). Any command to do so?

share|improve this question

closed as too localized by Marco Ceppi Dec 6 '11 at 12:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
That depends on a few things. Can you tell us what sort of database you're using? is it on the local machine? –  Jeremy Kerr Mar 1 '11 at 6:38
1  
This question is considered abandoned, there are many sorts of databases and a lot more ways to log in to them, without more information this would be like shooting bats in the dark. I have flagged this for closure since it has no accepted answers or further activity, if you feel that this issue still affects you it is possible to contact a moderator to re-open it or if you want you can drop a comment asking to re-open it. –  Bruno Pereira Dec 6 '11 at 9:40

2 Answers 2

You can store the username and password do use in ~/.my.cnf

[mysql]
user=foo
password=bar

But this file is unencrypted so beware of that.

share|improve this answer
    
that's why you chmod 700/500 this file ;) –  Rinzwind Jul 7 '11 at 19:29

Many client softwares - including mysql - can get passwords from command line. Try

mysql -u root -pROOT_PASSWORD DATABASE_NAME

Where ROOT_PASSWORD is password for mysql root account. You can also use certificates for logins.

As there is some confusion, at least recent versions of mysql are masking your password from process listing. You can check this by running above mentioned command and then in another terminal

ps auxw | grep mysql

If you see something like

mysql -u root -px xxxxx

(where -px xxxxx is exactly this, not your password), then your password is not viewable in process listing.


Security considerations:

  • As @Lekensteyn mentioned in comments, beware that commands you run will be saved to history file (readable only by user and root). You can prevent this by for example unsetting HISTFILE variable.
  • Your script should be protected with correct permissions (for example 700, which is all permissions for owner, and nothing for others).
share|improve this answer
    
Oh yeah, ps aux|grep mysql. Lovely. –  Lekensteyn Mar 1 '11 at 9:47
    
@Lekensteyn: which shows "mysql -u root -px xxxxx" (where 'xxxxx' is not my password; it's masked automatically). I don't see the problem here. Lovely. –  Olli Mar 1 '11 at 11:27
    
I see, did not know that, thanks. Beware for the command history, either put a space before the command or clear the HISTFILE= environment variable. –  Lekensteyn Mar 1 '11 at 15:15
    
@Lekensteyn: good point (history file while testing), when running it from script that's not important, because commands inside script are not logged to history file. –  Olli Mar 1 '11 at 16:26