9

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.

jmitchel3
  • 391
  • 3
  • 4
  • 17

4 Answers4

22

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.

Drew
  • 24,851
  • 10
  • 43
  • 78
  • 12
    `$ sudo service mysql stop` returns `sudo: service: command not found` – jmitchel3 Nov 20 '12 at 22:11
  • running the daemon in sudo mysqld_safe --skip-grant-tables mode may not be really required, i tried without it and still it solved my problem (this was on mac 10.9) – Basav May 06 '14 at 08:14
  • this works for me on OS X Mavericks 10.9 after having a lot of Access Denied when typing in 'mysql -u root -p', thanks! – mcn Jun 01 '14 at 10:45
  • 1
    Please refer to this if you encountered "Unknown column 'password' in 'field list'": http://stackoverflow.com/a/31122246/1371949 – felixwcf Jan 12 '16 at 14:04
  • yes, the column name is `authentication_string` in version 5.7 – Drew Aug 14 '16 at 17:56
  • There is no service called mysqld in Ubuntu, where the correct command would be `service mysql stop` – user1283068 Aug 29 '16 at 11:31
  • In mysql 14.14 with Ubuntu Xenial, the above doesn't work. Meaning that the original error persists even after the described sequence of commands. – user1283068 Aug 29 '16 at 11:33
  • @user1283068 mysqld? Look it up for your distro? – Drew Aug 29 '16 at 12:07
  • Look what up for my distro? Your comment makes no sense. – user1283068 Aug 31 '16 at 10:58
  • @user1283068 look up how you start and stop services for your distro. I am sorry it makes no sense to you. I cannot tutor everyone new to stuff – Drew Aug 31 '16 at 12:01
  • @Drew I wasn't asking what the name of the service was, I was just pointing out an error in the reply. As you can see I specifically mention the correct name of the service. – user1283068 Sep 02 '16 at 10:02
  • If in case someone gets service command not found error: use this instead: To stop: sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist To start: sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist It is recommended to first check the status of your mysql server first. These are the steps for mysql 5.7 – saran3h Jul 05 '18 at 09:34
6

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
remkohdev
  • 250
  • 3
  • 13
  • I dont have any database named `mysql`, I have two databases: `information_schema` and `test`. I connected through this command: ` mysql -h127.0.0.1` as via just `mysql` I was getting this err: `ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)` – Saurabh Nov 07 '16 at 08:38
2

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
Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
1

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.

user1283068
  • 1,694
  • 4
  • 15
  • 25
  • I'm running 5.7* on OS X 10.12.3 and can no longer use the `mysql stop/start` alias, getting the error of the question. Your above answer looks promising, but gives: `ERROR 1054 (42S22): Unknown column 'mysql.user' in 'where clause'`? – Dave Everitt May 25 '17 at 12:00