Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fresh MYSQL install, Access denied for user 'root'

I had an old lamp server that I wanted to move to a new machine, so I did a mysqldump, installed Ubuntu Server 13.10 on a new machine, installed lamp during installation, then imported my old mysql databases from the old lamp server. Everything seemed to work perfect right after the mysql import, to my surprise.

Then I tried setting up a cron job to mysqldump all databases every hour to a backup server. Just to make sure it worked, I tried manually running mysqldump on the new server just to make sure it worked (instead of waiting for the cron job to run). Anyways, the mysqldump function would not work, and for some reason now I can't access mysql at all. I tried:

mysql -u root -p

and get "ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)"

Also, none of my PHP scripts can access mysql databases as well. So I am locked out.

I don't know why running mysqldump (or crontab) would lock me out, so I'm thinking it has to do with importing all the databases from my old lamp server (which was running an older version of mysql).

I'm still a linux newbie for the most part so any help would be appreciated!

like image 714
user1564018 Avatar asked Feb 10 '14 03:02

user1564018


People also ask

How do I fix access denied in MySQL?

To resolve the error, you must create a user with the following command: mysql> GRANT ALL ON *. * to user_name@localhost IDENTIFIED BY 'password'; Replace user_name with the user's username and password with the user's password.

How do I fix MySQL error Access denied for user root localhost?

Use the ALTER USER command and change the authentication method to log into MySQL as root: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password'; This command changes the password for the user root and sets the authentication method to mysql_native_password.

How do I fix root access denied?

Solution 1: Sudo then Change Password If you get the “access denied” error, one way to solve it is by using sudo to log in to mysql and change the root password. Step 1: Open the command line on your system. Step 3: Enter the password for this account.

How do I enable root access in MySQL?

Configuring a default root password for MySQL/MariaDB Use the following procedure to set a root password. To change the root password, type the following at the MySQL/MariaDB command prompt: ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd'; flush privileges; exit; Store the new password in a secure location.


3 Answers

I still don't know why I got locked out, but to resolve the issue I had to reset the mysql root password, which I did following the instructions on this site (but I modified them for Ubuntu 13.10): https://help.ubuntu.com/community/MysqlPasswordReset

Stop the mysql daemon process using this command :

sudo pkill mysqld

Start the mysqld daemon process using the --skip-grant-tables option with this command

sudo /usr/sbin/mysqld --skip-grant-tables &

start the mysql client process using this command

mysql -u root

from the mysql prompt execute this command to be able to change any password

FLUSH PRIVILEGES;

Then reset/update your password

SET PASSWORD FOR root@'localhost' = PASSWORD('password');

If you have a mysql root account that can connect from everywhere, you should also do:

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

Once you have received a message indicating a successful query (one or more rows affected), flush privileges:

FLUSH PRIVILEGES;

Then stop the mysqld process and relaunch it with the classical way:

sudo pkill mysqld
sudo service mysql restart

Some of those steps might be unnecessary but that's how I successfully reset the mysql root user password on Ubuntu Server 13.10 after importing a mysqldump file from an old lamp server

like image 150
user1564018 Avatar answered Oct 10 '22 01:10

user1564018


On mine the problem was that I can only access mysql from CLI being logged in as root.

like image 40
Jakub Mróz Avatar answered Oct 10 '22 02:10

Jakub Mróz


Once you kill the mysqld process, you need to create a /var/run/mysqld directory to be used by the MySQL process to store and access a socket file:

$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld

This is missing in a lot of answers. You can then start the mysql process with the --skip-grant-tables options successfully. You can run the command:

$ jobs

To confirm that the process is running before proceeding.

N/B This was on Ubuntu 18.04

like image 6
Joram Okwaro Avatar answered Oct 10 '22 01:10

Joram Okwaro