Complete Guide to Installing and Uninstalling MySQL on Ubuntu and Windows
Uninstalling MySQL on Ubuntu
First, remove MySQL packages:
sudo apt-get remove mysql-*
Then clean up residual configuration files:
dpkg -l | grep ^rc | awk '{print $2}' | sudo xargs dpkg -P
A dialog may appear; select "Yes" to confirm.
Installing MySQL on Ubuntu
You can install MySQL using either of the following commands:
sudo apt-get install mysql-client mysql-server
Or specify a version (e.g., 5.6):
sudo apt-get install mysql-server-5.6
During enstallation, you will be prompted to set a root password. If you did not purge residual data before installation, this prompt may not appear.
Checking MySQL Status
Verify if MySQL is running:
sudo service mysql status
Typically, MySQL starts automatically after installation. If not, start it manually:
sudo service mysql start
If it fails to start, you might encounter the error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'.
Setting the MySQL Root Password
Use the mysqladmin command to set a password:
mysqladmin -u root password 'new-password'
Example:
/usr/bin/mysqladmin -u root password 123456
Resetting a Forgotten MySQL Root Password
- Stop the currently runing MySQL process:
/etc/init.d/mysql stop
- Start MySQL in safe mode and skip privilege validation:
/usr/bin/mysqld_safe --skip-grant-tables
- Open a new terminal and log into MySQL as root:
mysql -u root
- Update the root password:
USE mysql;
UPDATE user SET Password = PASSWORD('root') WHERE User = 'root';
If you get error ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE, refer to the solution.
Query OK, 3 rows affected (0.00 sec)
EXIT;
- Restart MySQL in normal mode:
/etc/init.d/mysql restart
- Verify the new password:
SHOW GRANTS FOR 'root'@'127.0.0.1';
FLUSH PRIVILEGES;
QUIT;
Installing MySQL on Windows (Binary Format)
Refer to the Baidu guide for installation.
Setting the Root Password on Windows
Refer to this CSDN article for setting the password.
Alternatively, use the GRANT command:
GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456';
Uninstalling MySQL on Windows
Refer to this CSDN article for uninstallation steps.
Installing MySQL 8
Refer to this guide for installing MySQL 8.