Mysql cli; password works when prompted, but not when passed as a command argument

Can't figure it out and all my searches have failed.

When I login to mysql and I login

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

      

it works. the next thing I see is the command line mysql>

. However, if I enter

$ mysql -u database_user -pmy_password

      

or

$ mysql -u database_user --password=my_password

      

Both of them, according to the documentation , should allow me access. I am getting an access denied response.

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

      

I've tried many options defining the database or host:

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

      

But I keep getting Access Denied error.

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

Does anyone else run into this issue or knows why, if I am prompted for a password, I'm fine, but if I pass it as an argument, I can't login.

+3


source to share


2 answers


Your password doesn't have "$" for example pa$$word

? Or another character that might mean something in the shell?

In this case, you need to attach your password with single quotes '



$ mysql -u database_user -p'my_password'

      

+1


source


While I'm not sure why you have this problem, I highly recommend not passing the password on the command line. The best practice is to enter the password in a secure file and use the option --defaults-extra-file

. Therefore, the password does not appear in clear text in the process table.

In your example, you can create a file /etc/mysql_login.cnf

and resolve it so that it can be read and then put into a file:

[client]
user=database_user
password=my_password

      



Then call mysql cli like this:

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

      

+1


source







All Articles