6

Even after I reset the root password with the following command I cannot log into MySQL. (Other commands listed to provide additional info.)

    # sudo dpkg-reconfigure mysql-server-5.1

    # mysql -u root -p
    Enter password:
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    # telnet 127.0.0.1 3306
      Trying 127.0.0.1...
      telnet: Unable to connect to remote host: Connection refused
    # ps -Aw |grep mysql
      26522 ?        00:00:00 mysqld

    # /etc/init.d/mysql start
    Rather than invoking init scripts through /etc/init.d, use the service(8)
    utility, e.g. service mysql start

    Since the script you are attempting to invoke has been converted to an
    Upstart job, you may also use the start(8) utility, e.g. start mysql

Also,

     # sudo mysqladmin -u root password 123
     mysqladmin: connect to server at 'localhost' failed

It seems MySQL is not running properly.

1
  • 1
    reza, did any of the proposed solutions work for you? If not, which problems remain?
    – tohuwawohu
    Apr 11, 2012 at 9:12

5 Answers 5

5

This works whenever I need to do this ...

Stop the MySQL Server:

sudo /etc/init.d/mysql stop

Start the mysqld configuration.

sudo mysqld --skip-grant-tables &

Login to MySQL as root.

mysql -u root mysql

Replace YOURNEWPASSWORD with your new password!

UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;
2
  • This looked very promising, but it did not work for Ubuntu 13.10 Server. The same ERROR 1045 was encountered during mysql -u root mysql.
    – user207039
    Dec 30, 2013 at 3:56
  • doesn't work still getting an error
    – aero
    Mar 14, 2016 at 22:37
3

There should be another admin user, debian-sys-maint. Check the file /etc/mysql/debian.cnf for its password (it isn't encrypted).

Using this password, log into your mysql server:

mysql -u debian-sys-maint -p

and set a new password for user root:

mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit;

You may check first if mysql is running and listening for connections:

sudo netstat -atpn | grep mysql

It should give you something like

tcp 0  0 127.0.0.1:3306     0.0.0.0:*       LISTEN      1306/mysqld

If mysql doesn't run, you should check /var/log/mysql.err and /var/log/mysql.log for details.

6
  • tohuwawohu - this is the best answer I've seen. With it, I was able to log into the database. Unfortunately, when the command is executed, it displays "Query OK, 0 Rows Affected, Rows Matched: 4, Rows Changed: 0, Warnings: 0".
    – user207039
    Dec 30, 2013 at 4:35
  • @noloader: Did you encounter the very same issue as described in the question? Did you try to modify the root password as described? If your issue isn't exactly the same as described in the question, you should better open a new question.
    – tohuwawohu
    Dec 30, 2013 at 9:36
  • 1
    @noloader: 0 Rows Affected is the usual result if the new and the old password are identical - in this case, mysql doesn't modify the password entry at all.
    – tohuwawohu
    Dec 30, 2013 at 10:05
  • Thanks tohuwawohu. "... mysql doesn't modify the password entry" - that's good to know.
    – user207039
    Dec 30, 2013 at 15:55
  • tohuwawohu: "Did you encounter the very same issue as described in the question" - yes, I originally had the same problem (even after running dkpg re-configure). tohuwawohu: "Did you try to modify the root password as described" - yes. But there's some other goodness present here (namely, I was trying to finalize an ownCoud installation through its web interface; and ownCloud needed to connect through localhost, and not the FQDN, even though they are the same machine).
    – user207039
    Dec 30, 2013 at 15:58
1

You can try as sudo

sudo mysql -uuser -p

I've installed mysql to an ubuntu server and couldn't login anymore. Then I tried with sudo and it worked...

0

Run the following commands:

mysql -uroot -p -hlocalhost
sudo service mysql restart
mysql -u root -p

That should work.

0

I faced similar prob at the end, i was able to log into this way (replace "user" with actual user)

mysql -h 127.0.0.1 -u "user"

This was because i had to specify the host and had not set a password of the "user" account.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .