
Why is MySQL (MariaDB) asking for a password even when it is specified with the “-p” option (SOLVED)
August 21, 2023
If you are trying to connect to the MySQL (MariaDB) server on the command line, then you may have encountered the problem that the MySQL (MariaDB) client does not see the password.
For example, when connecting to MariaDB using the command (where “USER” is the actual username and “PASSWORD” is the actual password):
mariadb -u USER -p PASSWORD
Instead of the expected connection, a password is requested:
Enter password:
If you just press Enter, you will receive an error message:
ERROR 1045 (28000): Access denied for user 'USER'@'localhost' (using password: YES)
The reason for the error is that the value of the options is incorrectly interpreted in a command of the form:
mariadb -u USER -p PASSWORD
In this command, “PASSWORD” is understood to be the name of the database to be used after connecting. As for the “-p” option, it is interpreted as “connect using a password”, but since no password is specified after it, this password is requested to connect to the MySQL (MariaDB) server.
There is a way out of this situation – indeed the password can be specified in the command line so that the MySQL client (MariaDB) correctly interprets it and connects. But before moving on to an example of proper commands for connecting to MySQL (MariaDB), it should be noted that it is bad practice to include passwords on the command line. The fact is that shells by default save command history. If anyone gains access to the history of the commands you run, then your MySQL (MariaDB) server password will be compromised.
If I have not convinced you, and you want to specify the password in the command line, then this can be done in two ways.
If you prefer the “-p” option, then specify the password after it, but do not separate it with a space from the option. By the way, other shorthand option values can also be written without spaces with their values:
mariadb -uUSER -pPASSWORD
Another way to specify a password is to use the long version of the “--password” option, followed by an equal sign and the password:
mariadb -uUSER --password=PASSWORD
Note that there must be no spaces between the option name, the equal sign, and the password.
So, you can specify a password in the MySQL (MariaDB) connection command line, but this is not recommended.
It is recommended to specify only the “-p” option instead of a password, this will cause the client to ask for a password before each connection to the DBMS. With this method of entering a password, the password is not saved in the history of entered commands.
Related articles:
- ERROR 1044 (42000): Access denied for user 'mial'@'localhost' to database 'TestDB'. Can't create MySQL database (SOLVED) (100%)
- Error when using ‘USE’ statement in MySQL (Mariadb): “ERROR 1044 (42000): Access denied for user” (SOLVED) (100%)
- Why “mysql -h” doesn't show help. Error “option '-h' requires an argument” (SOLVED) (100%)
- Error “ERROR 1143 (42000)”: command denied to user for column in table in MySQL (MariaDB) (SOLVED) (100%)
- Error “ERROR 1142 (42000)”: command denied to user for table in MySQL / MariaDB (SOLVED) (100%)
- Error “net::ERR_CERT_COMMON_NAME_INVALID” for self-signed certificate (SOLVED) (RANDOM - 3.2%)