MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL Reset Root Password

If you've forgotten or lost the MySQL root password, you can reset it. The process can differ slightly depending on the operating system and MySQL version, but generally involves stopping the MySQL service, starting it in a special mode that allows privilege changes, changing the password, and then restarting the service normally.

Here's a basic tutorial for a Unix-like system:

Step 1: Stop MySQL Server

First, stop the currently running MySQL service. On many Unix-like operating systems, you can use the service or systemctl command to do this. Here's how you would do it with systemctl:

sudo systemctl stop mysql

Step 2: Start MySQL without a password

Next, start MySQL in safe mode, without granting privileges, which will allow you to log in without a password:

sudo mysqld_safe --skip-grant-tables &

Step 3: Connect to MySQL Server

Now, you can connect to the MySQL server as the root user without a password:

mysql -u root

Step 4: Set up a New Password

First, switch to the MySQL system database that contains the user information:

FLUSH PRIVILEGES;
USE mysql;

Then, set a new password for the root user:

UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';

Replace 'new_password' with your new password.

Step 5: Stop and Restart MySQL Server

Finally, you need to stop the MySQL server that is running with the --skip-grant-tables option, and then restart it normally.

Exit the MySQL shell:

EXIT;

Stop the MySQL service:

sudo mysqladmin -u root -p shutdown

You'll be prompted for the new MySQL root password. Enter the new password, and the MySQL service will stop.

Now, start the MySQL service normally:

sudo systemctl start mysql
  1. Code:
    # Step 1: Stop MySQL
    sudo service mysql stop
    
    # Step 2: Start MySQL with --skip-grant-tables
    sudo mysqld --skip-grant-tables --user=mysql --skip-external-locking &
    
    # Step 3: Connect to MySQL
    mysql -u root
    
    # Step 4: Update root password
    UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
    
    # Step 5: Restart MySQL without --skip-grant-tables
    sudo service mysql restart
    
  2. Changing root password in MySQL:

    • Description: Changing the root password for security reasons.
    • Code:
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
      
  3. Recover MySQL root password command:

    • Description: The recovery command involves updating the root password when it's forgotten.
    • Code:
      UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
      
  4. Code:
    # Step 1: Stop MySQL
    sudo service mysql stop
    
    # Step 2: Start MySQL in safe mode
    sudo mysqld_safe --skip-grant-tables &
    
    # Step 3: Connect to MySQL
    mysql -u root
    
    # Step 4: Update root password
    UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
    
    # Step 5: Restart MySQL normally
    sudo service mysql restart