Arijit Panda
Arijit Panda

Reputation: 1665

How to change MySQL root password via a file?

How do I change the MySQL root password in ubuntu server via a file? The file may be any shell script or normal text file.

My try:

Normally the below command, in the terminal, changes the password for newly installed mysql.

mysqladmin -u root password 'newpass'

But while doing it via file it's showing the bellow error.

mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

So can someone help me to do so.

Upvotes: 1

Views: 384

Answers (3)

parmod
parmod

Reputation: 35

sudo mysql -u root

USE mysql;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

FLUSH PRIVILEGES;

EXIT;

test password

mysql -u root -p

sudo systemctl restart mysql

Upvotes: 0

Deep Kakkar
Deep Kakkar

Reputation: 6305

  1. Stop the MySQL Server: sudo /etc/init.d/mysql stop

  2. Start the mysqld configuration: sudo mysqld --skip-grant-tables

  3. Login to MySQL as root: mysql -u root mysql

  4. Replace YOURNEWPASSWORD with your new password:

    UPDATE
      mysql.user
    SET
      Password = PASSWORD('YOURNEWPASSWORD')
    WHERE
      User = 'root';
    FLUSH PRIVILEGES;
    exit;
    

Note: This method is not regarded as the most secure way of resetting the password, however, it works.

Upvotes: 1

A.D.
A.D.

Reputation: 2372

If you have never assigned a root password for MySQL, the server does not require a password at all for connecting as root follow : Resetting Permissions

Upvotes: 0

Related Questions