AlwaysALearner
AlwaysALearner

Reputation: 6690

Validate MySQL username and password

Is it possible to validate the given MySQL username and password using shell script without attempting to login to the MySQL service? If so, How? I haven't found any useful solutions.

Upvotes: 0

Views: 594

Answers (2)

Mayur Nagekar
Mayur Nagekar

Reputation: 811

Try this. It works.

mysql -u$USER_NAME  -p$PASSWORD -eexit

If you provide correct login credentials, the exit code will be 0 else it will be 1.

Example:

miyurz@my-host:~$ mysql -uroot  -pabc123 -eexit

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

miyurz@my-host:~$ echo $?
1
---------------------------------------------------

miyurz@my-host:~$ mysql -uroot  -pabc12 -eexit

miyurz@my-host:~$ echo $?
0

Explanation:

-e, --execute=name  Execute command and quit.

Upvotes: 2

Dan
Dan

Reputation: 2093

No, I don't believe this is possible.

You would need to attempt to connect to MySQL in order to validate if the authentication details are correct. Even to query the MySQL user table directly (where credentials are stored) would require making a connection to the MySQL server.

Perhaps you can share why you would need to do this?

Upvotes: 3

Related Questions