Access denied for user 'root'#'localhost' with certain passwords only - mysql

I just setup a fresh install of Ubuntu 14.04 with MySQL 5.6.19. After completing the installation I executed mysql_secure_installation which walked me through some security settings AND had me set a root password. My root password currently contains # and $ symbols. When I try and execute
mysql -u root -pAP#$$Here
I get the following error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Why am I getting this error?
If I run mysql_secure_installation on the command line again and change the root password by removing the # and $ symbols I am able to connect in just fine when I run
mysql -u root -pAPassHere
On related but separate issue, I also tried to connect by issuing the command:
mysql -u root
I was expecting to be prompted for the password, but instead I got the following related error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Why am I not being asked for a password?

Note that some symbols like $ have special meaning in the shell, and the shell expands variable into their values before passing the string to the program.
For example, try this:
$ echo AP#$$Here
AP#6282Here
The $$ is replaced with the process id of the bash shell from which I ran this command. If you want to suppress variable expansion, put the string in single-quotes:
$ echo 'AP#$$Here'
AP#$$Here
This works with the MySQL client too:
$ mysql -u root -p'AP#$$Here'
Note that you must still have no space between -p and the quoted password.
You can also get the MySQL client to prompt you interactively by using -p with no string following:
$ mysql -p
Enter password:
Finally, it may be convenient for you to enter your password in your $HOME/.my.cnf and then you won't have to type it. This also bypasses shell variable expansion, and takes the value from the config file literally.
$ cat >> $HOME/.my.cnf
[client]
user = root
password = AP#$$Here
^D

$$ will be interprepted by the shell as a variable ($$ will return the PID of the current process I think) so you'll want to surround your password in quotes like:
mysql -uroot -p 'foo$$bar'
to be prompted by the password (a more secure option as it won't be stored in your shell's history) connect specifying the password flag but with no value:
mysql -uroot -p
and then you'll be prompted for the password

you'll only get prompted for a password with the -p switch without anything following it. try wrapping your username and passwords with " and using something like this:
mysql --user=root --password="AP#$$Here"

Related

Using mysql_config_editor within shell script

I am having trouble understanding why this line works in command line:
mysql --login-path=local
I get teh typical mysql> _ prompt as expected.
However when I use the following in my shell script:
#!/bin/bash
mysql --login-path=local -e "SELECT contractor FROM contractor" | while read contractor; do
echo "Contractor: $contractor"
done
This is what happens when I try to run that small script:
zak#yserver:$ sh contractors.sh
ERROR 1045 (28000): Access denied for user 'zak'#'localhost' (using password: NO)
The problem is, when I set up local I used the root user (I know bad idea -- but this is just a proof of concept not production) like so:
mysql_config_editor set --login-path=local --host=localhost --user=root --password
From what I have read, I am using this correctly -- And it works on command line .. Just not in my shell script -- Is there a problem with my syntax?
After much thought -- I am having trouble understanding why 1) it's using the zak username -- And 2) it's using PASSWORD NO - When I clearly set the username as root and entered a password on the prompt.
update
it also fails if I use bash instead of sh -- IE
zak#yserver:$ bash contractors.sh
ERROR 1045 (28000): Access denied for user 'zak'#'localhost' (using password: NO)
I'm not sure why it works for you on the command line, but the mysql_config_editor credentials are stored in the system user's home directory.
So if you run:
mysql_config_editor set --login-path=local --host=localhost --user=root --password
...as user zak, then the .mylogin.cnf credentials file will be stored in /home/zak/ and should have permissions set to allow only zak to access it.
If you don't pass any username or password to mysql, then it uses your system user, which explains your error message (`Access denied for user 'zak'#'localhost').
If you set the credentials as root, then they will be saved in /root. You can copy them to /home/zak/, change permissions to 600 and ownership to zak:zak and then they should work for your user as well.
Or you can just run the mysql_config_editor command as zak.

How do I set mysql password?

I was getting an error trying to log in to mysql with mysql -u root -p , it always gave me the error "ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)". I'm on a Macbook Air running Yosemite.
So, I figured that I'd forgotten my password. It got so frustrating trying to reset it that I uninstalled mysql completely by sudo rming /usr/local/bin/mysql, and then reinstalling the dmg again from scratch. The installer gave me the gibberish temporary password, I tried logging in with it, and I got the same 1045 error that I've been getting all along. So maybe my first password wasn't wiped somehow . . . ?
I've tried this both with and without running mysqld in another terminal window, did not make a difference unfortunately. Any help on how I can just set the root password to whatever I want and log into mysql?
If you want to update a root password, then you need to use the following command:
$ mysqladmin -u root -p oldpassword newpassword
If you are trying to update the password for another user use:
$ mysqladmin -u user-name -p oldpassword newpassword
If you get a Access denied error it may be because you are not running the command as root.
To run a command as root add sudo to the beginning of the command.
For Example:
To update the password for the current, as root use:
$ sudo mysqladmin -u root -p oldpassword newpassword
To update the password for another user, as root use:
$ sudo mysqladmin -u user-name -p oldpassword newpassword
Also you might want to look at mysql_secure_installation, it will make your mysql installation more secure, it is recommended that you use it for production servers.
You first need to enter in your MySQL database : then copy and paste the following
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('SETYOURPASSWORDHERE');
Be sure your user is root

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.
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'
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

MYSQL error: 1045 (28000): Access denied for user 'root'#'localhost'

When I try to connect to the sql server and enter the following in command prompt:
shell> mysql --user=username--password=password db_name
I get error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: Y
ES)
What does this error mean?
Looks like a space is missing between username and --password
If you don't know the root password, with Debian or Ubuntu, there is an easy way to reset it :
First, get the exact version of your server using
sudo dpkg --get-selections | grep 'mysql-server-'
Then, just use sudo dpkg-reconfigure mysql-server-5.x
(btw, replace 5.x with you real version number)
On a fresh install, the default root password is blank, so should be able to log in using just
mysql -u root
You should obviously add a root password after installation
mysqladmin -u root password [newpassword]
In most cases, you should also set up dedicated accounts with limited rights before working with a DB.
On Windows -
Search for services
Stop the service named MySQL[#] (in my case it was MySQL80).
Start the service again.
Open Command Prompt and type:
mysql [database name] -u [user name] -p
It worked for me when no other solutions worked. Hope this solves your issue as well.
It means your password is wrong or the account "root" has no access to the database on host "localhost".
Look at the right side bar. There are multiple questions equivalent to yours.

mysql access denied for user 'odbc'#'localhost' to database

I installed MySql5.5 and set password during installation but when I try to use mysql from windows command prompt, I have get the error:
access denied for user 'odbc'#'localhost' to database password = 'YES'
I would like to change it back into "root#localhost" as well as to reset the password but I can't log in mysql.
How do I login to mysql with root?
You're trying to use the mysql interactive shell? You can specify usernames at the command line:
c:\> mysql -u root -p
where
-u = specify username
-p = prompt for password
I fixed this by implementing a little hacky solution. Downloaded hxd (hex editor) and searched for 'ODBC' (there should only be one match) and just changed it to 'root'.
You are logging into mysql with a default no-rights user, you have to login as root if you want to do everything:
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql.exe -u root -p
Enter Password: *****
If you never specified a root password it should be blank, if you did, you have to remember what it was, or find out how to reset the root password.