1

The command I'm talking about is this:

mysqldump -u root -p DB | mysql -u root -p DB2

And here's the output:

Enter password: Enter password:
MYPASS

As far as I can tell, after I enter first password supposedly mysqldump turns on echo, and as a result the second password is echoed. Now that I think about it, how do they even manage to both read the password?

3
  • 1
    You can store password in ~/.my.cnf or you can use -pPassword ? stackoverflow.com/questions/9293042/… Commented Feb 6, 2014 at 6:48
  • 3
    @Rahul: -p MySecretPassword would show in the output of ps and the history, amongst other places. Commented Feb 6, 2014 at 7:11
  • Yes, storing password in options file is probably the best way out of this. Also, I'd like to confirm if I understand right what's happening with those commands. Commented Feb 6, 2014 at 7:41

1 Answer 1

1

I can answer the question of "how do they even manage to both read the password?"

A device file named /dev/tty exists. Linux (and more modern Unix) kernels arrange for /dev/tty to be different for every process that has a controlling TTY. That's all of the interactive processes, you'd have to go out of your way to write a program that doesn't have a controlling TTY. The "TTY" column of ps -l -p $$ will show you what your shell's TTY is. ps -el "TTY" column will show you the controlling TTY of all processes.

You can think of /dev/tty as a way to get what would ordinarily be stdin, but from the keyboard associated with the controlling TTY. You can try this out with the command cat < /dev/tty > spork in an Xterm window. Whatever you type, up to the terminating control-D, will end up in file "spork".

A lot of FTP and SSH clients use /dev/tty for password input, to avoid having a password show up in the environment or other crannies. Interactive clients can do open("/dev/tty", O_RDWR) no matter what the actual controlling TTY is, and get a file descriptor that in essence, is connected to the keyboard. Various system calls can be used to turn off echoing. You can test this by using a bash shell Xterm and running stty -echo. You'll have to type blind, but commands like ls, ps, etc clearly get to the shell and work. You can turn TTY echoing back on via stty echo.

It appears (as per Rahul Patil above), that you can set a password in a file, .my.cnf. That file could include these lines:

[client]
password="my_password"

or maybe something like:

[mysql]
password="my_password"
user=root

The .my.cnf file appears to be extremely flexible in specifying parameters and values common to many clients of MySQL.

2
  • In fact, I was thinking that if they both trying to read password simultaneously, how do they manage to read it correctly or why do I have to type it twice. But now I suppose the kernel simply blocks mysql's read call, until mysqldump's one gets executed. Also I believe if you add @Rahul Patil's information (~/.my.cnf and the link) to your question, I can accept it. Commented Feb 6, 2014 at 20:17
  • @x-yuri - how about now? Commented Feb 6, 2014 at 20:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.