114

I currently use the following but it ALWAYS prompts me to manually type the password. Is there any way to pass it in on the command line when launching the executable?

mysqladmin processlist -u root -p
codeforester
  • 39,467
  • 16
  • 112
  • 140
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • 4
    Set MYSQL_PWD in the environment (`export MYSQL_PWD=muhpassword`) and execute your command without the `-p`. See [MySQL Program Environment Variables](https://dev.mysql.com/doc/refman/5.7/en/environment-variables.html). In spite of the manual's [dire warnings](https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html), this is [rather safe](https://security.stackexchange.com/questions/20282/is-passing-sensitive-data-through-the-process-environment-secure). Unless you start weird warez in the same shell that might hoover your environment and send it off to darkaspirator.cc. – David Tonhofer Mar 01 '18 at 17:51
  • **[Warning]** Using a password on the command line interface can be insecure. (password is also visible from the history along with the command). – hafiz031 Sep 15 '22 at 04:19

3 Answers3

219

Just found out the answer....

mysqladmin processlist -u root -pYOURPASSWORDHERE

No space between your password and the -p

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • 17
    quotes are allowed too if password contains spaces, as in: `-p'YOURPASSWORD HERE'` – Vigintas Labakojis Jul 16 '15 at 10:10
  • 53
    Wow... real intuitive... Space between `-h localhost` and `-u root` but not `-pPASSWORD`. Classic programmers making everything harder than necessary. – Kellen Stuart Mar 05 '16 at 22:32
  • 4
    @KolobCanyon you can omit the space between -u and root, `-uroot` works fine – Peter Ajtai Apr 18 '16 at 22:10
  • 13
    Warning: this is considered insecure: http://dev.mysql.com/doc/mysql-security-excerpt/5.7/en/password-security-user.html – neverendingqs Jul 26 '16 at 20:22
  • what is the function of `processlist`? – Smith Sep 03 '16 at 09:21
  • 3
    @KolobCanyon: What if your password starts with a space ? ;) The programmer can’t decide if the space separate the option and its value or is the first character of the password… – Stéphane Nov 15 '17 at 05:52
  • 1
    @Stéphane I'd say the best option would be to force the user to surround in `"password"`. That's how powershell does it and it works quite well – Kellen Stuart Nov 15 '17 at 15:31
  • @KolobCanyon I agree. Usually this kind of decisions are taken when the effort to implement goes over the estimated benefit. This could be actual or a mind automation by some dev not willing to struggle on it. Also, answering Stéphane, coherence in passing params would prevent the risk of interpreting the starting space as part of the password, as the starting space would just be required by syntax, always. – Niki Romagnoli Dec 09 '20 at 12:30
48

Try:

--password=PasswordTextHere 
codeforester
  • 39,467
  • 16
  • 112
  • 140
ge0man
  • 546
  • 3
  • 4
13

This should work: mysql -uroot -p'password'

If you're trying to automate mysql solution, you can use password from variable:

mysql_pass=$(sudo grep -oP "temporary password is generated for root@localhost: \K(.*)" /var/log/mysqld.log)

mysql -uroot -p"$mysql_pass" < somescript.sql

3lvinaz
  • 113
  • 3
  • 12