0
sudo mysqld_safe --skip-grant-tables;

mysql -u root

I have tested the above code, but was unable to enter the mysql. still in the (base)~

jarlh
  • 42,561
  • 8
  • 45
  • 63

1 Answers1

0

Step 1: Log in as the MySQL User When you boot into your Linux installation, make sure you’re logged in as the same user that normally runs MySQL. Although you can log in as root, once you start the MySQL server, make sure you start it with the --user=mysql option.

Otherwise, the system may create files owned by the root user, which can cause problems.

Step 2: Find the .pid File for the MySQL Service The next step is to find the .pid file for the MySQL service.

Most systems store them in /var/lib/mysql/, /var/run/mysqld/, or /usr/local/mysql/data/ path. The filename usually starts with mysqld (or your system’s hostname) and ends with the .pid extension.

Step 3: Kill the mysqld Process Before you create a new root password, stop the MySQL server. To kill the mysqld process, open a command line, and run the following:

kill cat /mysql-data-directory/host_name.pid

Replace mysql-data-directory/host_name.pid with the filename you found in the previous step. Ensure to specify the whole path to the file. Also, make sure to use the back-tick key (usually above the tab key) and not a single-quote mark in the beginning of the command.

Step 4: Create the Password File

  1. Open your favorite text editor. In this example, we use vim:

sudo vim

  1. Next, add the following line in the file:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword'; like this

create a password file for mysql Bear in mind to include the single-quote marks and the semicolon. Replace NewPassword with the password you want to use. Finally, make sure to use a strong secure password, like these examples.

The command will work for the machine you’re currently using. If you’re connecting to a different system, replace localhost with the appropriate hostname.

  1. Save the file to home/me/mysql-init.

Step 5: Restart the MySQL Server and Apply the New Password To apply the changes to the password, restart the MySQL server by running the following command in the terminal:

mysqld --init-file=/home/me/mysql-init &

This launches MySQL, and apply the text-file password change. Depending on how you start your server, you may need to add other options (such as --defaults-file before the init command.)

Step 6: Cleaning Up Lastly, log into your MySQL server using the root account, and verify the new password works. Then, delete the file you created in Step 4.