How to reset MySQL root password on Linux


You forgot or you unknow your root MYSQL password! How to reset MariaDb root password on Linux?

Step 1: stop mysqld

service mysqld stop

Result:

[root@tutorialspots ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

Step 2:

sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
use mysql;
update user set password=PASSWORD("new-password") where User='root';
flush privileges;
exit;

Result:

[root@tutorialspots ~]# sudo mysqld_safe --skip-grant-tables --skip-networking &
[1] 14033
[root@tutorialspots ~]# 190118 07:30:14 mysqld_safe Logging to '/var/log/mysqld.log'.
190118 07:30:14 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.36 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=PASSWORD("new-password") where User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

Step 3: stop and start Mariadb service

[root@tutorialspots ~]# service mysqld stop
190118 07:33:19 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld:                                           [  OK  ]
[1]+  Done                    sudo mysqld_safe --skip-grant-tables --skip-networking
[root@tutorialspots ~]# service mysql start
Starting mysqld:                                           [  OK  ]

Note 1: You can’t use command service mysqld restart

Bonus: 06/24/2020
Note 2: if you can start service mysqld or mariadb, you can check status

[root@tutorialspots ~]# service mariadb status -l
Redirecting to /bin/systemctl status  -l mariadb.service
● mariadb.service - MariaDB 10.1 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: activating (start) since Wed 2020-06-24 17:45:25 CEST; 1min 56s ago
  Process: 1456 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 2057 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=0/SUCCESS)
  Process: 2031 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2094 (mysqld)
   Status: "InnoDB: Error: Unable to lock ./ibdata1, error: 11"
   CGroup: /system.slice/mariadb.service
           └─2094 /usr/libexec/mysqld --basedir=/usr

Jun 24 17:45:25 tutorialspots systemd[1]: Starting MariaDB 10.1 database server...
Jun 24 17:45:25 tutorialspots mysql-check-socket[2031]: Socket file /var/lib/mysql/mysql.sock exists.
Jun 24 17:45:25 tutorialspots mysql-check-socket[2031]: No process is using /var/lib/mysql/mysql.sock, which means it is a garbage, so it will be removed automatically.
Jun 24 17:45:26 tutorialspots mysql-prepare-db-dir[2057]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Jun 24 17:45:26 tutorialspots mysql-prepare-db-dir[2057]: If this is not the case, make sure the /var/lib/mysql is empty before running mysql-prepare-db-dir.
Jun 24 17:45:26 tutorialspots mysqld[2094]: 2020-06-24 17:45:26 139909173926144 [Note] /usr/libexec/mysqld (mysqld 10.1.41-MariaDB) starting as process 2094 ...
Jun 24 17:45:26 tutorialspots mysqld[2094]: 2020-06-24 17:45:26 139909173926144 [Warning] Could not increase number of max_open_files to more than 1024 (request: 4190)

Now, you must stop mysqld_safe and mysqld

[root@tutorialspots ~]# ps aux|grep safe
root      1822  0.0  0.0 241300  4628 pts/0    S    17:44   0:00 sudo mysqld_safe --skip-grant-tables --skip-networking
root      1823  0.0  0.0 113420  1640 pts/0    S    17:44   0:00 /bin/sh /bin/mysqld_safe --skip-grant-tables --skip-networking
root      2160  0.0  0.0 112812   968 pts/0    S+   17:46   0:00 grep --color=auto safe
[root@tutorialspots ~]# kill -9 1822
[root@tutorialspots ~]# kill -9 1823
[1]+  Killed                  sudo mysqld_safe --skip-grant-tables --skip-networking
[root@tutorialspots ~]# ps aux|grep mysqld
mysql     1918  0.1  0.4 815216 148996 pts/0   Sl   17:44   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/var/log/mariadb/mariadb.log --pid-file=/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      2243  0.0  0.0 112812   968 pts/0    S+   17:47   0:00 grep --color=auto mysqld
[root@tutorialspots ~]# kill -9 1918

Done, now you can start mysqld/mariadb service.

Leave a Reply