How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu Техника

Introduction

This tutorial will cover how to reset the root password for older and newer versions of MySQL and MariaDB.

MySQL 8 contains new feature and improvement , so does the technic how to reset if you forget you root password for some reason. There are 2 methods on how to reset and here is how.

Step 5 — Restart the Database Server Normally

For MySQL, use:

For MariaDB, use:

Then, restart the service using systemctl.

systemctl start mysql

systemctl start mariadb

Now you can confirm that the new password has been applied correctly by running:

mysql root

The command should now prompt for the newly assigned password. Enter it, and you should gain access to the database prompt as expected.

Step 3 — Restarting the Database Server Without Permission Checking

Start the database without loading the grant tables or enabling networking:

mysqld_safe —skip-grant-tables —skip-networking

The ampersand at the end of this command will make this process run in the background so you can continue to use your terminal.

You’ll immediately see a database shell prompt instead.

Now that you have root access, you can change the root password.

Start your VPS now with FREE $100 credit.

On this tutorial we will guide you how to reset MySQL root password on Fedora/RHEL/CentOS and Debian/Ubuntu server.

This guide should work well on MySQL 5.5, MySQL 5.6, MySQL 5.7 and later versions.

In certain conditions it may require us to reset the MySQL root password, for example we want a stronger MySQL root password or maybe you forgot your MySQL database server root password.

The main purpose clearly to strengthen our MySQL server database system, make it more secure.

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

Start MySQL in safe mode

$ mysqld_safe —skip-grant-tables &

Login to database

After MySQL safe mode is active, you can now login to the MySQL database server without a root password via the SSH terminal. This is very useful if you currently forget your MySQL root password.

Дополнительно:  Как узнать root пользователь или нет

$ mysql -u root mysql

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

Reset root password

Now let’s reset your MySQL root password.

For MySQL 5.5 and MySQL 5.6

For MySQL 5.7+

Note: Please replace newrootpassword with the actual new root password for your MySQL database server.

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

Flush privileges

Run the flush privileges command and exit from the MySQL database server.

$ FLUSH PRIVILEGES;
$ quit

Exit from safe mode

Now we need to exit from MySQL safe mode, so we need to shutdown the MySQL database server.

Start in normal mode

The next step is we need to start your MySQL database server in normal mode as before.

* For Debian/Ubuntu server

$ service mysql start or /etc/init.d/mysql start

$ service mysqld start or /etc/init.d/mysqld start

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

As for Debian 8+, Ubuntu 14.04+ and CentOS 7 up to the latest version, you can use the systemctl command to start/stop/restart your database server. For example:

On Debian 8/9/10 and Ubuntu 14.04/16.04/18.04/19.04 LTS

$ systemctl restart mysql

On CentOS 7+

$ systemctl restart mysqld.service

We are a bunch of people who are still continue to learn Linux servers. Only high passion keeps pushing us to learn everything.

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

Have you forgotten your MySQL root password? (I know, i know 😂😂😂 it’s a secret.)

If you do, and trying to find out how to do it, then you are in the right path my friend.

Let’s start and let’s code pare.

Quick Note

If ever you don’t have a choice, make sure to change it late night or when there is not much traffic in your server.

STEP 1

First thing we need to do is login in your server and to stop MySQL service.

service mysql stop

sudo /etc/init.d/mysql stop

sudo service mysql stop

Once you run this code, it will stop your mysql services and no one can connect to your database temporary.

( Make sure to notify your co-developer and the other teams involve or else you will receive a new Ticket Error 😂 )

STEP 2

We need to start our mysql in a safe mode.
What is mysqld_safe command means?

mysqld_safe is the recommended way to start a mysqld server on Unix. mysqld_safe adds some safety features such as restarting the server when an error occurs and logging runtime information to an error log. A description of error logging is given later in this section.

Дополнительно:  Что делать, если не включается ноутбук

Run this code below, when running mysqld_safe with —skip-grant-tables it enables anyone to connect without a password and with all privileges. Together with the ampersand «&» to run the command in the background.

mysqld_safe —skip-grant-tables &

sudo mysqld_safe —skip-grant-tables &

We’re almost there pare, just keep going.

Login in our mysql by running this command, you should see the mysql monitor welcome screen.

How To Reset MySQL 5.7 Root Password In Centos Or Ubuntu

STEP 4

Type this code, to use mysql table and Press enter

Then the code to change your root password is the command below.
Make sure to change the «EnterYourSuperCoolPasswordHere» before running it.

Take note, the authentication_string is for mysql 5.7 up version.. incase you have a lower version of mysql you can use password column instead.

Reload the grant tables to take effect using the command below.

Then exit from mysql command window using

Start your mysql server

sudo /etc/init.d/mysql start

sudo service mysql start

mysql -u root -p

If everything works, then you will see a welcome message in mysql monitor command screen.

SOLUTION:
Login as mysql root then type this command:

Mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists.

sudo mkdir /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

sudo pkill -9 mysqld

Then try to stop or start again mysql.

SOLUTION:
Login as mysql root then type this command:

It means you’re using a low version of code for mysql 5.6 below. password column was removed from mysql 5.7 above, As i mention in the tutorial above you need to use authentication_string

Voila

I hope i help you solving your problem. Let me know incase you encounter a different error.

Thanks pare. 👊 👊 👊

Explore our security tips for safety server and process.
Explore our security tips for safety server and process

Voila!!!

I hope you enjoy our tutorial, Let me know incase you encounter any error I would love to answer that. Don’t forget to subscribe to my Youtube Channel at Let’s Code Pare — Youtube Channel

Share Now

Most modern Linux distributions ship with either MySQL or MariaDB, a popular drop-in replacement which is fully compatible with MySQL. Depending on the database used and its version, you’ll need to use different commands to recover the root password.

Дополнительно:  Dingdong root для android на русском скачать бесплатно на телефон

You’ll see some output like this with MySQL:

mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper

Or output like this for MariaDB:

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

Make note of which database and which version you’re running, as you’ll use them later. Next, you need to stop the database so you can access it manually.

Small Note

Please login to MySQL passoword using your root account and grant the root with GRANT OPTION command

Don’t forget to flush priviliges;

Step 2 — Stopping the Database Server

To change the root password, you have to shut down the database server beforehand.

You can do that for MySQL with:

systemctl stop mysql

And for MariaDB wtih:

systemctl stop mariadb

After the database server is stopped, you’ll access it manually to reset the root password.

Prerequisites

To recover your root MySQL/MariaDB password, you will need:

Step 4 — Changing the Root Password

Let’s tell the database server to reload the grant tables by issuing the FLUSH PRIVILEGES command.

Now we can actually change the root password.

For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:

Make sure to replace new_password with your new password of choice.

Remember to reload the grant tables after this.

In either case, you should see confirmation that the command has been successfully executed.

Query OK, 0 rows affected (0.00 sec)

The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.

Using –skip-grant-tables

shutdown mysql service if the service is running

systemctl stop mysqld.services

Now, connect to MySQL server using this simple command

Now stop MySQL service again and start it normally.

systemctl stop mysqld.service

systemctl start mysqld.service

Using Init file

Now create init file

The command above will start MySQL service and will also execute init file you have created to update the root password in the process.

Оцените статью
Master Hi-technology
Добавить комментарий