How to recover mysql root password in MacOS

[亡魂溺海] 提交于 2019-12-02 10:29:59

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!