How to recover mysql root password in MacOS - mysql

I start my MAMP and try to connect to my MySQL server via localhost or 127.0.0.1, I tried all the possible password that I think it is correct, but for some reasons it still didn't like it, so I think I forgot it somehow. I need help.
Try #1
Enter all the possible passwords
MySQL -u root -h localhost -p
Enter password: <---I've tried almost 10 possible passwords
I got
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Try #2
Enter possible passwords
MySQL -u root -h localhost
I got
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Try #3
MySQL.server stop
I got
ERROR! MySQL server PID file could not be found!
I'm trying to run this
mysqld_safe --skip-grant-tables &
I got
Try #4
mysqld_safe --skip-grant-tables &
I got
[1] 72194
🌈 ~ 2017-10-04T12:13:42.6NZ mysqld_safe Logging to '/usr/local/var/mysql/BH-MacBook-Pro-15-512GB.local.err'.
2017-10-04T12:13:42.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
2017-10-04T12:13:44.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/BH-MacBook-Pro-15-512GB.local.pid ended
Open a new tab
MySQL -u root
I still get
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Try #5
Close my MAMP application, and run this
mysqld_safe --skip-grant-tables &
I got
[1] 72194
🌈 ~ 2017-10-04T12:13:42.6NZ mysqld_safe Logging to '/usr/local/var/mysql/BH-MacBook-Pro-15-512GB.local.err'.
2017-10-04T12:13:42.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
2017-10-04T12:13:44.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/BH-MacBook-Pro-15-512GB.local.pid ended
Open a new tab
MySQL -u root
I still get
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
I don't what else to try anymore.
I can't really set the root password because I can't even get in.
This is the process of my MySQL
ps aux | grep MySQL
bheng 95850 0.2 1.0 2869440 175404 ?? S 8:48AM 0:00.28 /usr/local/opt/mysql/bin/mysqld --basedir=/usr/local/opt/mysql --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mysql/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/bh-mbp-15-512gb.ad.benu.net.err --pid-file=/usr/local/var/mysql/bh-mbp-15-512gb.ad.benu.net.pid
_mysql 124 0.0 0.0 2864848 4040 ?? Ss 22Sep17 0:32.26 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid --keyring-file-data=/usr/local/mysql/keyring/keyring --early-plugin-load=keyring_file=keyring_file.so
bheng 95863 0.0 0.0 2423384 396 s009 R+ 8:48AM 0:00.00 grep mysql
bheng 95749 0.0 0.0 2448756 2496 ?? S 8:48AM 0:00.03 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql
bheng 66379 0.0 0.1 2696936 9304 ?? S 8:06AM 0:00.51 /Applications/MAMP/Library/bin/mysqld --basedir=/Applications/MAMP/Library --datadir=/Applications/MAMP/db/mysql --plugin-dir=/Applications/MAMP/Library/lib/plugin --log-error=/Applications/MAMP/logs/mysql_error_log.err --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --socket=/Applications/MAMP/tmpmysql/mysql.sock --port=8889
bheng 66263 0.0 0.0 2446708 2100 ?? S 8:06AM 0:00.02 /bin/sh /Applications/MAMP/Library/bin/mysqld_safe --port=8889 --socket=/Applications/MAMP/tmp/mysql/mysql.sock --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --log-error=/Applications/MAMP/logs/mysql_error_log
🌈 ~

This is how you can do it in macOS Sierra, probably you install it via pkg and in the system preferences have an icon for MySQL, something like this:
If is up and running open a terminal and run this command:
pgrep -fl mysql
That will help you to find the path of mysqld and the current command used to start the server, the output could be something like this:
6283 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
Next step is to stop MySQL, you can do this by clicking on the System Preferences menu and click on the Stop MySQL Server button, Once is stoped within a terminal you will need to start again the server but with some extra options, something like this:
sudo /usr/local/mysql/bin/mysqld \
--skip-grant-tables \
--skip-networking \
--user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
It is indeed the same command from the output of pgrep -fl mysql but in this case, you just need to add:
--skip-grant-tables --skip-networking
After doing that you will notice that the menu from the System Preferences is Green again and MySQL should be up and running, now you can try to login and change password:
mysql -uroot
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR root#'localhost' = PASSWORD('secret');
mysql> FLUSH PRIVILEGES;
mysql> exit
Stop and start again MySQL from the System Preference panel and it should be working now with the new password.

Please follow the steps in same sequence.
Make sure, your MySQL server is down. Execute below command :
mysql.server stop
Then, start MySQL server in safe mode
mysql.server start --skip-grant-tables --skip-networking
Then, connect to MySQL using root user.
mysql -u root
Change the database from none to mysql
MariaDB [(none)]> use mysql;
Database changed
Update both authentication_string and password column with same password as shown below (if you get column not found error then remove one )
MariaDB [mysql]> update user set authentication_string=PASSWORD('password'), password=PASSWORD('password') where user='root';
MariaDB [mysql]> FLUSH PRIVILEGES;
MariaDB [mysql]> quit;
Now, stop everything including MySQL server and MAMP application as well.
And, search for config.inc.php file inside your MAMP folder (you could use command + space and search this file).
You may not have privilege to change this file. After updating the privilege of the file you need to look for $cfg['Servers'][$i]['password'] and update the password value as shown below.
$cfg['Servers'][$i]['password'] = 'password';
Save this file and restart everything in normal mode. And try connecting root user with new password as shown below :
mysql -uroot -ppassword

You have to reset the MySQL root password as follow :
/etc/init.d/mysql stop
/usr/sbin/mysqld --skip-grant-tables --skip-networking &
mysql -u root
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR root#'localhost' = PASSWORD('new_password');
mysql> FLUSH PRIVILEGES;
mysql> exit;
service mysql restart
And then, you will be able to connect using :
mysql -u root -p
Your password

Follow those instructions and you should be fine:
https://svendeswan.wordpress.com/2016/01/26/mysql-reset-root-password-without-knowing-it-mac-mamp/
which is based on this one:
https://www.tech-otaku.com/local-server/resetting-mamp-mysql-root-user-password/

Related

How to reset password for root in MariaDB 10.5.5-MariaDB on CentOS 8?

I tried the suggestions in Resetting root password for mariadb does not work
ERROR 1698 (28000): Access denied for user 'root'#'localhost' only talks about Ubuntu, not centos 8
Was able to login with mysql -u root but there was no user root in mysql table!!
So, the set password statement executes with 0 affected rows.
Did not work even after server restart.
Login to centos server from ssh. Then stop mysqld service and run mysqld_safe with —skip-grant-tables option to be in insecure mode and to reset the password. Then login to mysql server using mysql command without any -u or -p parameter as you are in insecure mode. Then update the root password flush privilage then exit from msyql console. Then use mysqladmin to stop mysql server which will ask for password give recently saved new password. Then finally start the mysqld service.
$ systemctl stop mysqld
$ mysqld_safe --skip-grant-tables &
$ mysql
mysql> UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ mysqladmin -u root -p shutdown
$ systemctl start mysqld
Edit: ---------------------------------------
$ sudo systemctl stop mariadb
$ sudo mysqld_safe --skip-grant-tables --skip-networking &
$ mysql -u root
MariaDB [(none)]> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit;
$ sudo kill `/var/run/mariadb/mariadb.pid`
$ sudo systemctl start mariadb

Error even after starting mysql server with --skip-grant-tables

I have forgotten the password for mysql server. Even after starting the server with --skip-grant-tables I get the below error.
ER
ROR 1045 (28000): Access denied for user 'ODBC'#'localhost' (using
password: NO)
This is for windows.
Also I get the error even for mysql -u root
What could be the issue?
Here is what you need to do
First ensure that mysqld is not running.
ps aux | grep mysqld
if it is running. Stop it. Then,
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
mysql -u root
FLUSH PRIVILEGES;
SET PASSWORD FOR root#'localhost' = PASSWORD('password');

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.

MySQL general SET-UP

I am new to MySQL, and I got some general problems and thanks for any help:
My OS is MAC SnowLeopard, and when I type 'mysql' in terminal, I could successfully go into the mysql system, but, I have no rights to create databases:
mysql> create database A;
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'a'
I tried command line 'mysql -u root', I got the following:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
I was thinking that I may set up the username for the database, which made me not possible enter mysql using root as username?
Then I tried 'localhost' as username, which I guess or remember I set it or not, using the command 'mysql -u localhost', I could successfully logged in to mysql, but STILL, cannot create database,
mysql> create database a;
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'a'
I am not sure:
why root not working;
weather I set up the username and password myself before...sorry...really have no memory now.
If I have set up the username as 'localhost' before, I cannot remember the password, therefore I have no rights to create database?
What should I do to recover the password, in the case of no root access?
If not possible, how to uninstall it? The PATH in .bash_profile for mysql is /usr/local/mysql/bin
I got a check: sudo port list installed | grep mysql
mysql5 #5.1.65 databases/mysql5
mysql5 #5.1.65 databases/mysql5
py26-mysql #1.2.3 python/py-mysql
does the mysql in /usr/local/mysql/bin refer to this one listed here?
Edit
The result of running ps -ef | grep mysql:
0 107 1 0 0:00.01 ?? 0:00.02 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/XXX-MacBook-Pro.local.pid
74 245 107 0 0:03.51 ?? 0:18.35 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/mysql/plugin --user=mysql --log-error=/usr/local/mysql/data/XXX-MacBook-Pro.local.err --pid-file=/usr/local/mysql/data/XXX-MacBook-Pro.local.pid
501 3790 417 0 0:00.00 ttys000 0:00.00 grep mysql
501 3337 1271 0 0:00.00 ttys001 0:00.01 mysql -u localhost
You'll need to reset your root password. To do this, follow those simple steps:
Stop MySQL: sudo port unload mysql5-server
Start MySQL using /opt/local/lib/mysql5/bin/mysqld_safe --skip-grant-tables &
Connect to MySQL: mysql -uroot
Reset your MySQL password:
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Stop MySQL again sudo killall -9 mysqld_safe
Restart MySQL normally sudo port load mysql5-server
Connect to MySQL: mysql -uroot -p
EDIT
Added commands to stop/start MySQL.