Unable to reset the MySQL root password on a Mac - mysql

I just reinstalled MySQL using brew on my mac.
As far as I can see everything works fine, but I cannot change my root password.
When trying I'm getting this error message:
mysql -u root
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
The common way to solve this seems to use (several answers on stack overflow suggest using this):
sudo mysqld_safe --skip-grant-tables
2017-05-19T11:54:35.6NZ mysqld_safe Logging to '/usr/local/var/mysql/mac.err'.
2017-05-19T11:54:35.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
2017-05-19T11:54:35.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/mac.pid ended
I stopped the server before executing the command. But this solution does not work for me.
There is also no entry in the error log:
/usr/local/var/mysql/mac.err
I read also about using mysql_secure_installation but there I get just the same error message as when using mysql -u root.
As I mentioned above, I tried already to just uninstall and reinstall it again, but obviously some data remained. So is there maybe any way to manually delete or reset these files?

Solution by OP.
It turned out that:
mysql.server start --skip-grant-tables
(not sure if sudo is necessary)
works for me.

The access denied is because you're not specifying the -p switch
If you know the old password then maybe login to MySql and change it as:
1).login to MySql 2).change to the correct db 3) change the passwd and flush
mysql -u root -p
mysql> use mysql;
mysql> update user set password=PASSWORD("newpass") where User='ENTER-USER-NAME-HERE';
mysql> flush privileges;
mysql> quit
Also, the following works in Linux/Unix systems but i haven't tested it on Mac (may work if you have the clients installed)
mysqladmin -u root -p oldpassword newpass
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
//If you don't remember the password, to recover it check this out(bottom).
https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords
https://coolestguidesontheplanet.com/how-to-change-the-mysql-root-password/

Related

Can't connect to mysql, get this error: ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)

I enter mysql -u root -p and put in my password and get this error, I have uninstalled and reinstalled mysql and changing the password for root but still get this message.
I have looked and looked for solutions but nothing seems to work.
I am trying to enter mysql through my terminal, instead I get get the error above and can't get into mysql even after uninstalling and reinstalling it.
Usually this means that you are using the wrong credentials to connect, try to consult consult the general log file /var/log/mysqld.log to copy the initialization password.
if that doesn't work you can try this approach:
check if MySQL is installed and running on your system sudo service mysql status
Reset the root password:
stop the server: sudo /etc/init.d/mysql stop
start the server in safe mode: sudo mysqld_safe --skip-grant-tables &
try to connect: mysql -u root
reset the root password: UPDATE mysql.user SET authentication_string=PASSWORD('newpassword'), password_expired='N' WHERE User='root';
flush privileges: FLUSH PRIVILEGES;
quit the server: quit
stop the safe mode server: sudo /etc/init.d/mysql stop
now you can start the MySQL server again: sudo /etc/init.d/mysql start
Use the correct credentials to connect (with the new password)
check out previous answers to this problem here and here.

Cannot login to MySQL using sudo as root

I'm having a weird problem where I cannot login to my MySQL server with sudo as root on a ubuntu 19.10 server. I want to be able to reset my root password.
The error message I got was:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Anyone had similar problems? Thanks!
Ok - so I solved this problem myself.
The correct way of resetting the root password for mysql is to:
Stop all mysql server sessions.
Start mysqld manually using - this basically tells MySQL to skip any credentials check and allow anyone to login, hence you want to skip-networking to avoid network users.
sudo mysqld --skip-grant-tables --skip-networking
Use this to login as root:
sudo mysql -u root
In the mysql prompt, do:
FLUSH PRIVILEGES;
SET PASSWORD FOR root#'localhost' = PASSWORD('password');
FLUSH PRIVILEGES;
Stop mysqld and restart mysql normally (you may need to kill the mysqld process).
In my case, there was some problems with the installation and the /var/run/mysqld folder was not correctly set up. It may be a bug with MySQL and it was not properly reported.
After creating the folder with the correct setmod (user:group is mysql:mysql) mysqld runs without any problem.

Resetting mysql root password

I understand that this is the safest way to change or reset mysql root password. I'm doing it because 8 times out of 10 the installation program won't prompt me for a root password.
So I run commands:
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysql -u root
use mysql;
Then, depending on your version of mysql or maria you're using you type:
update user set password=PASSWORD("mynewpassword") where User='root';
or
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
In my case I typed the first one. Then I refresh privileges by:
flush privileges; and then I exit out of mysql by typing: quit.
At this point everything went well. Then I restart mysql server by:
sudo /etc/init.d/mysql start
I get "OK" from the terminal. However, when I attempt to log in as root
I get pleasant error message saying:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
I'm 100% confident that I'm typing correct password. Why is this happening ?
Problem resolved by backing up all important data and reinstalling the operating system. As turned out I had some broken packages that affected how mysql authentication worked. Standard steps to fix broken packages was no use so I wiped out HDD and started over.
MySQL was installed without any hiccups and was even prompted for a root password to be set. Now MySQL authentication works like a charm.
Try this out :- It Works for me..!!
Stop the MySQL server process.
service mysql stop
Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password.
mysqld_safe --skip-grant-tables > /dev/null 2>&1 &
Connect to mysql server as the root user. And Setup new mysql root account password.
mysql -u root -e "use mysql; update user set password=PASSWORD('NEW-PASSWORD') where User='root'; flush privileges;"
Exit and restart the MySQL server.
service mysql restart
Note: You may need to wait after mysqld_safe command, before you can run subsequent mysql command.
Now run :- mysql -u root -p

How can I successfully login with root after running mysql_secure_installation?

I am currently unable to login as root on mysql and I am not quite sure about what's going on. Here's what happened:
I ran mysql_secure_installation as recommended, to secure my mysql installation. Afterwards, I typed the default root password that was asked and then I entered:
No to setting a password for root
Yes on removing anonymous users
Yes on disallowing remote root login
Yes on removing the test database and access to it
Yes on reloading privilege tables
After completing this process, I tried accessing mysql with mysql -u root -p (entered the default password) and received this message:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Do you have any ideas on what might have went wrong?
I also tried resetting the root password by starting mysql with --skip-grant-tables, but I am still not able to login.
I am using Ubuntu 14.04 and mysql 14.14 (LAMP stack).
I know this is an old post but the main answer is outdated and did not solve my issue.
Below my steps for future reference if anybody is having similar problems.
Stop mysql if it's running
$ sudo service mysql stop
Start mysql in safe mode
$ sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
If you get the error
"mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists"
Just create that folder:
$ mkdir -p /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld
Open a new terminal window and log into mysql service + select mysql database:
$ mysql -u root
mysql> use mysql;
Set new password for root user:
mysql> update user set authentication_string=password('new-password') where user='root';
Flush privileges and exit mysql:
mysql> FLUSH PRIVILEGES;
mysql> exit;
Stop the safemode mysql (from the second terminal, you will see it stop in the first terminal)
$ mysqladmin -u root -p shutdown
Restart mysql
$ sudo service mysql start
You should be able to use the root user with password now to login to mysql/phpmyadmin
CentOS/Redhat:
From what I read in docs, when you run mysql_secure_installation, a temporary root password is generated and is stored in some log file.
sudo grep 'temporary password' /var/log/mysqld.log
Debian/Ubuntu:
During the packages installation, you get a prompt asking for the root password. If you don’t set it up, MySQL’s root user is created without a password. We can read the following line in package installation output:
Shell
2016-05-16T07:27:21.532619Z 1 [Warning] root#localhost is created with
an empty password ! Please consider switching off the
--initialize-insecure option.
but it is configured with the auth_socket plugin. You will only be able to connect using the UNIX socket, therefore any attempt to connect using your local IP or the network fails. Later on, you can change the password to allow connections from the network (as explained in this blog post).
Source
All we can do now is to see the root password. Lets change the root password since you cannot understand hashed password even if we can see it:
sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
then run mysql in a new terminal
mysql -u root
and run the following query, after changing the password
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;
quit the mysql safe mode and start mysql service by
mysqladmin shutdown
sudo service mysql start
just run this script by root , you need custormize you password
mysqlpassword=password
/usr/bin/mysqladmin -u root password "$mysqlpassword"
#configure mysql login privileges
echo "grant all privileges on *.* to root#\"localhost\" identified by \"$mysqlpassword\";show databases;" |mysql -u root -p$mysqlpassword

Access Denied for MYSQL ERROR 1045

I just got a new macbook pro (OS X 10.8.2) and am attempting to get mysql set up on it. So far I've been able to get it installed but I cannot get my root user access (or any user for that matter). I plan on using this for Python, on my other computer I only use MYSQL (no MAMP) and I prefer to keep it that way.
For reference, I did the following:
$ alias mysql=/usr/local/mysql/bin/mysql
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin
When i enter mysql or mysql -u root -p it gives me this:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
or
ERROR 1045 (28000): Access denied for user 'jmitch'#'localhost' (using password: NO)
Depending on which phrasing I use
MYSQL is running in my system preferences. Thank you for your help.
Maybe updating the package the updater overwrote the root password.
To restore it:
Stop mysqld deamons.
$ sudo service mysqld stop
Go to mysql/bin directory
$ cd /usr/bin
Start a mysql deamon with this option:
$ sudo mysqld_safe --skip-grant-tables
Open another terminal and open a mysql session to execute this:
$ mysql
mysql> use mysql;
see Note1 below for next line.
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
Now kill the mysqld_safe process and restart mysqld normally:
$ sudo service mysqld start
Note1: password is the column name in table mysql.user prior to version 5.7. After which it became authentication_string. Change your update statement accordingly.
on Mac OSX 10.9 Mavericks I used the 'mysql.server' script in the support-files directory instead of the mysqld_safe and service script.
$sudo ./mysql.server stop
$sudo ./mysql.server start --skip-grant-tables
$ mysql
mysql> use mysql;
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
$sudo ./mysql.server stop
$sudo ./mysql.server start
I was having a similar issue trying to access MAMP's MySQL through the terminal on Mountain Lion.
The --no-defaults flag solved it for me.
/Applications/MAMP/Library/bin/mysql --no-defaults -u root -proot -h localhost
I want to add that for MySQL 5.7 simply changing the authentication_string column doesn't work. This is because MySQL never actually uses those values for root authentication, it uses a plugin. As far as I can tell this plugin verifies that you are also root on the host account (so you have to sudo mysql -u root).
The only way I was able to get this to work was to run this:
UPDATE mysql.user
SET authentication_string=PASSWORD(''), plugin=''
WHERE mysql.user = 'root';
It should also be noted that the official MySQL documentation for 5.7 never mentions this. Following this documentation to the letter gets you nowhere at all.