Monday 28 May 2018

How to Remove MySQL Completely from Linux System


Some time we face issues with MySQL installation on Linux machine. If we simply remove MySQL packages and re-install doesn’t fixes the issue, in that case old settings may still exists on server which again affects new install. In that case first uninstall MySQL completely from system and erase all settings of old install. To do the same follow the below settings.
Note: Please do not use below steps if MySQL have any running databases.
Step 1: Uninstall MySQL Packages
First uninstall all the MySQL packages installed on your server
 # yum remove mysql mysql-server
Step 2: Romove MySQL Directory
Now we need to remove MySQL data directory from system which by default exists at /var/lib/mysql. If you didn’t find this, It may be changed to some other place, which you can find in my.cnf file with variable datadir. Delete the /var/lib/mysql directory from system but we prefer to rename it to keep a backup of existing files.
 # mv /var/lib/mysql /var/lib/mysql_old_backup
Step 3: Install MySQL Packages Again
After removing MySQL completely, install it again using yum package manager, It will re create mysql directory under /var/lib/.
# yum install mysql mysql-server
After completing above three steps, now you have a fresh MySQL install on your system with new settings.

How to Backup and Restore a MySQL Database


MySQL is a database server for storing data permanently. If you are using MySQL server for an production use, then its necessary to create database backup for recovering from any crash. MySQL provides an utility mysqldump for taking backups. In this article you will learn, to taken databases backup in .sql format for archive format. Also we will explain various options for it.
Options for Creating MySQL Databases Backup
You have many options for creating databases backups. read few options below. For this example we have using database name “mydb”.
1. Full Database Backup in Plain .sql File
 # mysqldump -u root -p mydb > mydb.sql
2. Full Database Backup in Archive .sql.gz File
 # mysqldump -u root -p mydb | gzip > mydb.sql.gz
3. Backup Single Table Only
 # mysqldump -u root -p mydb tbl_student > tbl_student.sql
4. Backup Multiple Databases
# mysqldump -u root -p --databases mydb1 mydb2 mydb3 > mydb1-mydb2-mydb3.sql
5. Backup All Databases
 # mysqldump -u root -p --all-databases > all-db-backup.sql
6. Backup Database Structure Only (no data)
 # mysqldump -u root -p --no-data mydb > mydb.sql
7. Backup Database Data Only (no table structure)
 # mysqldump -u root -p --no-create-info mydb > mydb.sql
8. Backup MySQL Database in XML Format
 # mysqldump -u root -p --xml mydb > mydb.xml
How to Restore MySQL Backup
For restoring databases from backup is quite simple. We use mysql command for it. For example following command will restore all backup from mydb.sql to mydb database.
# mysql -u root -p mydb < mydb.sql

Friday 25 May 2018

How to Reset MySQL root Password in Linux


How to Reset MySQL root Password in Linux
How to reset MySQL root password in Linux? How do I recover MySQL root password? I forgot MySQL root password, steps to change MySQL root password? How to reset MySQL root password using the command line. How to reset forgotten MySQL root password on Linux? How to reset MySQL 5.7 root password? 
 
MySQL is an open source database software widely used for data storage. Sometimes we forgot MySQL root password. So don’t be panic, This tutorial will help you to reset MySQL root password with simple steps.
Step 1 – Start MySQL in Safemode
First of all, you are required to stop running MySQL server. Use one of the following commands to stop MySQL server on your Linux system.
# service mysql stop             
# systemctl stop mysql.service   
Now start MySQL server in safe mode using withe the --skip-grant-tables option. Use the following command to start MySQL in safe mode. In safe mode, MySQL does not prompt for login password.
# mysqld_safe --skip-grant-tables &
Step 2 – Reset MySQL root Password
Now login to MySQL server as root user and change password using the following set of commands. This will reset MySQL root password on your system.
For MySQL 5.6 or Below
# mysql -u root

mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEW-PASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
For MySQL 5.7 or Above
# mysql -u root

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("NEW-PASSWORD");
mysql> FLUSH PRIVILEGES;
mysql> quit
Step 3 – Restart MySQL Server
After changing password stop the MySQL (running in safe mode) service and start it again with the commands below.

# service mysql stop
# service mysql start


# systemctl stop mysql.service
# systemctl start mysql.service
Step 4 – Verify New Password
After resetting MySQL root account password and restarting, just verify new password by login.
# mysql -u root -p

Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.5.57 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>