10

I have a MySQL installed on my linux server, I forgot it's password so I went and changed it using the methods I found on the web. What I did was as follows:

/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql --user root mysql
SELECT * FROM user; // I checked if I could access the user table or not
update user SET password = PASSWORD('new_pass') WHERE user = 'root';
flush privileges;
exit

The update query did change the password as it showed me the number of rows affected and Query OK etc.

Then I restarted mysql

/etc/init.d/mysql stop
/etc/init.d/mysql start

Now when I logged in with the new password

mysql -u root -p new_pass

it still gives me errors "ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Yes)"

Is there something that I am missing?

sarwar026
  • 3,821
  • 3
  • 26
  • 37
progrAmmar
  • 2,606
  • 4
  • 29
  • 58

10 Answers10

17

I was able to solve this problem by executing this statement

sudo dpkg-reconfigure mysql-server-5.5

Which will change the root password.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Divz
  • 1,393
  • 2
  • 8
  • 6
7

You have to reset the password! steps for mac osx(tested and working) and ubuntu

Stop MySQL

$ sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

$ sudo mysqld_safe --skip-grant-tables

(above line is the whole command)

This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:

$ mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

Start MySQL

sudo /usr/local/mysql/support-files/mysql.server start

your new password is 'password'.

tk_
  • 16,415
  • 8
  • 80
  • 90
  • 1
    Just two different things: To stop the server, I had to go to System Preferences -> MySQL; From MySQL 5.7 there is no password column in the users table - authentication_string is the new column ( http://stackoverflow.com/a/31122246/782874 ) – StinkyCat Sep 15 '16 at 11:00
4

I had the same problem. You have to write mysql -u root -p

NOT mysql or mysql -u root -p root_password

jvperrin
  • 3,368
  • 1
  • 23
  • 33
  • This worked for me, albeit i referred to http://askubuntu.com/questions/428243/mysql-access-denied-for-user-rootlocalhost – naivecitizen Jan 06 '17 at 05:05
3

I meet the same problem, @progrAmmar enlightened me, "took a closer look at the user table in mysql database".

My problem is not ssl_type, instead of the first field:Host. I changed the value by using

update user set Host='localhost' where User='root' and Host='%';

in mysqld_safe --skip-grant-tables model.

Then it works well.

Community
  • 1
  • 1
Belter
  • 3,573
  • 5
  • 42
  • 58
3

You may need to clear the plugin column for your root account. On my fresh install, all of the root user accounts had unix_socket set in the plugin column. This was causing the root sql account to be locked only to the root unix account, since only system root could login via socket.

If you update user set plugin='' where User='root';flush privileges;, you should now be able to login to the root account from any localhost unix account (with a password).

See this AskUbuntu question and answer for more details.

Community
  • 1
  • 1
Stephen S
  • 356
  • 1
  • 5
  • 16
3

On mac os, please follow below steps:

Stop MySQL

$ sudo /usr/local/mysql/support-files/mysql.server stop Start it in safe mode:

$ sudo mysqld_safe --skip-grant-tables (above line is the whole command)

This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:

$ mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root'; Start MySQL

sudo /usr/local/mysql/support-files/mysql.server start your new password is 'password'.

Yang Wang
  • 580
  • 4
  • 14
2

In my case:

  1. /etc/init.d/mysql stop
  2. mysqld_safe --skip-grant-tables &

(in new window)

  1. mysql -u root
  2. mysql> use mysql;
  3. mysql> update user set authentication_string=password('password') where user='root';
  4. mysql>flush privileges;
  5. mysql> quit
  6. /etc/init.d/mysql restart
Michal
  • 21
  • 2
0

Actually I took a closer look at the user table in mysql database, turns out someone prior to me edited the ssl_type field for user root to SSL.

I edited that field and restarted mysql and it worked like a charm.

Thanks.

progrAmmar
  • 2,606
  • 4
  • 29
  • 58
  • You will have to start MySql in safe mode. It does not require root password. Once you login, you will be in mysql database, you can then issue a select * from user and see the fields for yourself. – progrAmmar Nov 26 '13 at 23:58
0

Maybe the current user is not root,try sudo mysql -uroot -p ,I successed just now.

fade
  • 1
  • 2
-1

You can try this solution :-

To have mysql asking you for a password, you also need to specify the -p-option: (try with space between -p and password)

mysql -u root -p new_password

MySQLl access denied

In the Second link someone has commented the same problem.

Community
  • 1
  • 1
Sharad
  • 3,562
  • 6
  • 37
  • 59