DeveloperBreeze

Prerequisites:

  • You need root (superuser) access to your VPS.
  • You must be able to access your server via SSH.

Step 1: Stop the MySQL Service

Before proceeding, you need to stop the MySQL service to prevent any conflicts while resetting the password.

Run this command to stop MySQL:

sudo systemctl stop mysql

Step 2: Bypass Authentication by Editing the MySQL Configuration

To reset the root password, you need to bypass the MySQL authentication mechanism by allowing access without a password. To do this, we will temporarily disable the password authentication by editing the MySQL configuration file.

  1. Open the MySQL configuration file (mysqld.cnf or my.cnf):
   sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Add the following line under the [mysqld] section to skip the grant tables:
   skip-grant-tables

This setting allows you to access MySQL without requiring a password, but only for this temporary process.

  1. Save the file and exit (Ctrl + X, then press Y to confirm).

Step 3: Start MySQL in Safe Mode

Now, start MySQL again, which will now run without enforcing password restrictions.

sudo systemctl start mysql

Step 4: Log in to MySQL Without a Password

Now that MySQL is running in "safe mode" without requiring passwords, you can log in without being prompted for the root password.

mysql -u root

Step 5: Drop and Recreate the Root User

Once logged in to the MySQL shell, follow these steps to drop and recreate the root user:

  1. Drop the existing root user:
   DROP USER 'root'@'localhost';

This removes the existing root user, including any potential issues related to password or privileges.

  1. Recreate the root user with a new password:

Now, you can create a new root user and assign a new password to it.

   CREATE USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';

Replace 'your_new_password' with the actual password you want to set for the root user.

  1. Grant all privileges to the new root user:
   GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

This grants the new root user full access to all databases and tables, as well as the ability to grant permissions to other users.

  1. Flush privileges to apply the changes:
   FLUSH PRIVILEGES;

This ensures that all changes you've made to users and permissions take effect immediately.

Step 6: Exit MySQL and Remove skip-grant-tables

After resetting the password and recreating the root user, you need to revert the changes made to the MySQL configuration.

  1. Exit the MySQL shell:
   exit;
  1. Remove or comment out the skip-grant-tables line:

Open the MySQL configuration file again:

   sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Remove the line:

   skip-grant-tables

Or comment it out by adding a # at the beginning:

   #skip-grant-tables
  1. Save the file and exit (Ctrl + X, then press Y to confirm).

Step 7: Restart MySQL Service

Now, restart the MySQL service so that it starts with the new configuration (with password authentication enabled again).

sudo systemctl restart mysql

Step 8: Log in with the New Root Password

Now that MySQL has restarted and is enforcing password authentication, you can log in to MySQL using the new root password you set earlier.

mysql -u root -p

You will be prompted to enter the password. Enter the new password you set in Step 5.


Troubleshooting

  • If you can't start MySQL after removing skip-grant-tables, double-check the configuration file (mysqld.cnf) for typos or errors.
  • Make sure MySQL is running by checking its status:
  sudo systemctl status mysql

Conclusion

This tutorial provided a step-by-step guide on resetting your MySQL root password using the DROP USER method. By dropping the existing root user and creating a new one with full privileges, you can resolve common issues related to root access and password management. Remember to always remove the skip-grant-tables option after completing the password reset to keep your MySQL server secure.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
bash

How to Grant MySQL Root Privileges for 127.0.0.1

When prompted, enter the new password you set earlier.

If you're working with a Laravel or any other application that uses MySQL, update your .env file or configuration file to use 127.0.0.1 as the DB_HOST instead of localhost:

Oct 03, 2024
Read More
Tutorial
mysql

How to Resolve the "#1038 - Out of Sort Memory" Error in MySQL

Open the configuration file with your preferred text editor and find or add the following line:

[mysqld]
sort_buffer_size = 4M

Aug 26, 2024
Read More
Cheatsheet
json

JSON Operations in MySQL: Examples and Use Cases

You can remove specific keys from a JSON object using the JSON_REMOVE function.

UPDATE users
SET preferences = JSON_REMOVE(preferences, '$.notifications.sms')
WHERE name = 'John Doe';

Aug 21, 2024
Read More
Tutorial
mysql

Understanding and Using MySQL Indexes

SHOW INDEX FROM users;

Indexes work by creating a separate data structure that holds the indexed columns and a pointer to the actual data in the table. This allows MySQL to quickly locate and retrieve the requested data without scanning every row in the table.

Aug 12, 2024
Read More
Tutorial
mysql

How to Monitor MySQL Database Performance

MySQL Workbench provides a graphical interface for monitoring database performance. It includes tools for visualizing server status and performance metrics.

The Performance Dashboard includes reports on:

Aug 12, 2024
Read More
Tutorial
mysql

How to Optimize MySQL Queries for Better Performance

-- Subquery
SELECT * FROM users WHERE user_id IN (SELECT user_id FROM orders);

-- Optimized with JOIN
SELECT users.* FROM users
JOIN orders ON users.user_id = orders.user_id;

Regularly review query performance and make adjustments as necessary. Use tools like MySQL Workbench, Percona Toolkit, or performance_schema for ongoing monitoring and optimization.

Aug 12, 2024
Read More
Tutorial
mysql

Managing Transactions and Concurrency in MySQL

In this example, two operations are performed within a transaction: inserting a new account and updating the balance. If any operation fails, you can use ROLLBACK to undo the changes.

MySQL provides different isolation levels to control how transactions interact with each other. Each level offers a different balance between data consistency and performance:

Aug 12, 2024
Read More
Tutorial
mysql

Viewing the Database Size and Identifying the Largest Table in MySQL

This query will output the size of the specified database in megabytes (MB).

To identify the largest table in terms of size within a specific database, use a similar query but without the grouping. Instead, order by size and limit the result to find the largest table:

Aug 12, 2024
Read More
Code
bash

How to view free space on a Linux server

  df -h /home
  • Inodes: To check free inodes instead of disk space, use the -i option:

Aug 11, 2024
Read More
Tutorial
bash

Finding the Top 10 Biggest Files on an Ubuntu Server

After executing the command, you'll see a list of the top 10 largest files, along with their sizes. For example:

1.2G    /var/log/largefile.log
900M    /home/user/bigdata.dat
850M    /opt/application/hugefile.bin
...

Aug 11, 2024
Read More
Tutorial
bash mysql

How to Allow MySQL Port and Restrict Access to Specific IPs

sudo ufw delete allow 3306/tcp

Replace <your_ip> with the IP address you want to allow:

Aug 04, 2024
Read More
Tutorial
python bash

Deploying a Flask Application on a VPS Using Gunicorn and Nginx

pipreqs . --force

Then, install the dependencies:

Aug 03, 2024
Read More
Code
php bash

Laravel Artisan Commands Cheatsheet

  php artisan migrate:fresh
  • Run Specific Seeders

Aug 03, 2024
Read More
Tutorial
sql

Optimizing SQL Queries: Indexing and Query Optimization Techniques

Split large tables for better targeting and faster reads.

  • Index frequently queried columns.
  • Remove unused indexes.
  • Regularly analyze and maintain indexes.

Aug 03, 2024
Read More
Code
javascript

Simple WebSocket Server using 'ws' library

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!