Skip to Content

How to set MySQL root password

SET MYSQL ROOT PASSWORD


What is Mysql default  Password?

The default user for MySQL is the root; by default, it has no password. If you set a password for MySQL and you can’t recall it, you can always reset it and choose another one.

 

How to set Mysql root Password?

Login to MySQL > Assign a password using Alter command > Flush the privileges to reload the grant tables in the database.

root@ubuntu ~]# mysql

mysql> Alter user 'root'@'localhost' identified by 'KAvghytfJA{3ab';

mysql> flush privileges;

mysql> exit;


  • How to Reset/Recover Mysql root Password (8v)

1. Stop Mysql Service.

root@ubuntu ~]# systemctl stop mysql

2. Ensure mysqld dir exists and set the owner.

root@ubuntu ~]# mkdir /var/run/mysqld

root@ubuntu ~]# chown mysql /var/run/mysqld

3. Start MySQL with --skip grant-tables& option

root@ubuntu ~]# mysqld_safe --skip-grant-tables &

When the --skip-grant-tables option is used, anyone can connect to the database server without a password and with all privileges granted. 

4. Login without a Password.

root@ubuntu ~]# mysql -u root

5. Reset the Root Password

mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
mysql> flush privileges;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password_here';
mysql> flush privileges;

6. After Resetting make sure all MYSQL processes are stopped before starting the service again.

root@ubuntu ~]# killall -u mysql

7. Start Mysql service

root@ubuntu ~]# systemctl start mysql


ERROR: After mysql/MariaDB installation on the Linux server, followed by the  launch of mysql_secure_installation script, we may encounter this error:


For MySQL 5.7.6 and newer versions / For MariaDB 10.1.20 and newer versions, use the following command.

 

1. Log in to MySQL and run the below command to set/change a password for the root user.

  • Follow the steps to set mysql root password in Ubuntu/RHEL/Debian/Cent OS (MYSQL):

root@ubuntu ~]# mysql

mysql> Alter user 'root'@'localhost' identified by 'KAvghytfJA{3ab';

mysql> flush privileges;

mysql> exit;

mysql  Ver 8.0.31-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

For MySQL 5.7.4 and older versions / MariaDB 10.1.20 and older versions, use the following command:

  • Follow the steps to set mysql root password in Ubuntu/RHEL/Debian/Cent OS (MariaDB):

MariaDB [(none)]>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('KAvghytfJA{3ab');

MariaDB [(none)]>flush privileges;

MariaDB [(none)]> exit;

mysql  Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1

2. Restart the MYSQL server and continue installing Ezeelogin

root@ubuntu ~]# systemctl restart MySQL


Related Information

ERROR creating DB user in MySQL 8.0