Is there a way to pass the DB user password into the command line tool mysqladmin?
Asked Answered
W

3

115

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
Wagtail answered 30/9, 2012 at 22:3 Comment(2)
Set MYSQL_PWD in the environment (export MYSQL_PWD=muhpassword) and execute your command without the -p. See MySQL Program Environment Variables. In spite of the manual's dire warnings, this is rather safe. Unless you start weird warez in the same shell that might hoover your environment and send it off to darkaspirator.cc.Haviland
[Warning] Using a password on the command line interface can be insecure. (password is also visible from the history along with the command).Carlina
W
219

Just found out the answer....

mysqladmin processlist -u root -pYOURPASSWORDHERE

No space between your password and the -p

Wagtail answered 30/9, 2012 at 22:6 Comment(8)
quotes are allowed too if password contains spaces, as in: -p'YOURPASSWORD HERE'Genocide
Wow... real intuitive... Space between -h localhost and -u root but not -pPASSWORD. Classic programmers making everything harder than necessary.Bogey
@KolobCanyon you can omit the space between -u and root, -uroot works finePanta
Warning: this is considered insecure: dev.mysql.com/doc/mysql-security-excerpt/5.7/en/…Dorman
what is the function of processlist?Armillary
@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…Aurilia
@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 wellBogey
@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.Binary
A
48

Try:

--password=PasswordTextHere 
Arvonio answered 30/9, 2012 at 22:6 Comment(2)
Useful when you need to pass a "blank" password (for testing purposes of course).Insobriety
this also seemt o be the only option for passing an empty password in the command line (on a sandbox instance for example)Predicable
C
14

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

Conservatory answered 17/2, 2019 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.