Billy20
Billy20

Reputation: 33

mysql cli; password works when prompted, but not when passes as command argument

Can’t figure this out, and all of my searching hasn’t helped.

When I login to mysql and I enter

$ mysql -u database_user -p
Enter password:  ******* #here I enter my_password

this works. the next thing I see is the mysql> command prompt. However, if I enter

$ mysql -u database_user -pmy_password

or

$ mysql -u database_user --password=my_password

Both of which, according to the documentation, should be allow me access. I get the access denied response.

ERROR 1045 (28000): Access denied for user 'database_user'@'localhost' (using password: YES)

I've tried a lot of variations specifying database or host:

$ mysql -u database_user -pmy_password -d database_xxx -h localhost

But I keep getting the Access Denied error.

I want to pass my password as a command line argument so I can write some scripts to automate some tasks.

Has anyone else run into this issue or know why if I'm prompted for a password, I'm good, but if I pass it in as an argument, I cannot login.

Upvotes: 3

Views: 106

Answers (2)

JTG
JTG

Reputation: 8836

Your password doesn't happen to have a '$' in it, like pa$$word? Or another character that might mean something to the shell?

In that case you will need to enclose your password with single quotes '

$ mysql -u database_user -p'my_password'

Upvotes: 1

Ike Walker
Ike Walker

Reputation: 65567

While I'm not sure exactly why you are having that problem, I strongly recommend against passing the password on the command line. The best practice is put the password in a secure file and use the --defaults-extra-file option. That way the password is not displayed in plaintext in the process table.

For your example, you could create a /etc/mysql_login.cnf file and permission it such that only you can read it, then put this in the file:

[client]
user=database_user
password=my_password

Then call the mysql cli like this:

mysql --defaults-extra-file=/etc/mysql_login.cnf

Upvotes: 1

Related Questions