Skip to content

Backup & Restore MySQL DB linux

This document contains steps to perform backup and restore operations of AIV Database with MySQL database in linux platform.

References

Links usefull for backup and restore data are listed here,

  1. https://devopsarticle.com/install-create-backup-restore-mysql-on-ubuntu-20-04-lts-6-easy-steps/

  2. https://phoenixnap.com/kb/how-to-backup-restore-a-mysql-database

  3. https://www.digitalocean.com/community/tutorials/how-to-backup-mysql-databases-on-an-ubuntu-vps

  4. https://linuxize.com/post/how-to-create-a-mysql-database/

  5. https://www.softwaretestinghelp.com/mysql-docker/

  6. https://hub.docker.com/_/mysql

  7. https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04

There are 2 ways we can perform this,

  1. Using terminal

  2. MySQL Workbench application

1. Using terminal

Backup

List of commands used to perform backup and restore operation are listed below,

  1. Install MySQL by executing this command: sudo apt install mysql-client

    Image

  2. Check Installed MySQL version by running this command: mysql -V

    Image

  3. Install MySQL server by executing this command sudo apt install mysql-server

    Image

  4. In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This plugin requires that the name of the operating system user that invokes the MySQL client matches the name of the MySQL user specified in the command, so you must invoke mysql with sudo privileges to gain access to the root MySQL user: sudo mysql

    Image

  5. Once you have access to the MySQL prompt, you can create a new user with a CREATE USER statement. These follow this general syntax: CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';

    For example, CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'Password@123';

    Image

  6. After creating your new user, you can grant them the appropriate privileges. The general syntax for granting user privileges is as follows:

    GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

    Image

  7. Following this, it’s good practice to run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements: FLUSH PRIVILEGES;

    Image

  8. Exit from mysql login: exit

    Image

  9. Start MySQL server by running this command: sudo service mysql start

    Image

  10. Check MySQL server version by executing below command,

    sudo mysqladmin -p -u sammy version

    Enter password you have applied for created user sammy in Step No 5.

    Image

  11. Connect with MySQL user, created in Step No 5 by running this command:mysql -u sammy -p

    Enter password you have applied for created user sammy in Step No 5.

    Image

  12. Once connected, execute this command to create database: CREATE DATABASE sample_data;

Image

  1. Switch to sample database created by executing this command: USE sample_data;

Image

  1. Execute this query to create sample table,

    CREATE TABLE Persons (
        PersonID int,
        LastName varchar(255),
        FirstName varchar(255),
        Address varchar(255),
        City varchar(255)
    );

    Image

  2. Insert sample data in to this created table by writing below query,

    INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
    VALUES ('2267', 'Erichsen', 'Jhon', 'Stavanger','Norway'),('2268', 'west', 'Allen', 'Central City','New York');
    

    Image

  3. Disconnect from mysql user by writing : exit command

Image

  1. Execute this command to take backup of sample_data database : mysqldump -u root -p sample_data > backup.sql

  2. Check backup.sql file created in your system execute ls command

    Image

Restore

Follow this steps to restore backup file (.sql) file in MySQL database.

  1. To restore data in new database, first let us create new database. login to mysql,

    mysql -P 3306 --protocol=tcp -u root -p

    Enter password for root user and hit enter.

    Image

  2. create new backup database CREATE DATABASE restore_data;. here restore_data is name of database.

    Image

  3. Exit from database by writing exit

  4. Execute this command to restore backup.sql file in restore_data database.

    mysql -u root -p restore_data < backup.sql

    Enter password for root user and hit enter

    Image

  5. To check backup file restored or not, Connect with MySQL database: mysql -u root -p

    Enter password for root user and hit enter

    Image

  6. Change database to restore_data : Use restore_data;

    Image

  7. Run this query to fetch data from table,

    select * from Persons;

    Image

2. MySQL Workbench application

For Backup and restore database using self-contained file [.sql] please follow steps provided in below link,

Click Here..

For Docker User

  1. Pull MySQL image in docker by executing this command in powershell: docker pull mysql/mysql-server:latest or direclty fetch mysql image and run using command given in step 2.

  2. Run MySQL image by executing this command :

    docker run --name backup-mysql -e MYSQL_ROOT_PASSWORD=1234 -d mysql:latest

    Image

  3. Get the list if docker images for MySQL docker ps you will see container ID as gievn in below code,

    Image

  4. Connect with MySQL containerID docker exec -it 3680d544408d bash

    Image

  5. Connect with MySQL database by executing this command : mysql -P 3306 --protocol=tcp -u root -p

    Provide password which you have applied in step 2 command and hit enter. Now you’re connected to MySQL running in your container

    Image

  6. Check the list of database available by default in MySQL by executing this command: SHOW DATABASES;

    Image

  7. Create new database in order to perform backup and restore operation : CREATE DATABASE sample_data;

    Image

  8. Connect with created database to create sample tables inside it. execute this command: USE sample_data;

    Image

  9. Now you are connected with sample_data database. to create a sample table execute this query,

    CREATE TABLE Persons (
        PersonID int,
        LastName varchar(255),
        FirstName varchar(255),
        Address varchar(255),
        City varchar(255)
    );

    Image

  10. Once table is created, add a sample data in to this table bby executing below query,

    INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
    VALUES ('2267', 'Erichsen', 'Jhon', 'Stavanger','Norway'),('2268', 'west', 'Allen', 'Central City','New York');

    Image

  11. Once table is created, let’s execute command to take backup of database. to do that you need to exit from mysql by executing exit command

Image

  1. execute this command from root user: mysqldump -u root -p sample_data > backup.sql

Image

  1. On successfull completion of above command check backup file is created or not execute this command: ls

    Image

  2. To backup data in new database, first let us create new database. login to mysql using mysql -P 3306 --protocol=tcp -u root -p

    Enter password and hit enter

    Image

  3. Create new backup database :CREATE DATABASE restore_data;

Image

  1. Disconnect from MySQL database execute: exit

Image

  1. Restore from backup file execute this command: mysql -u root -p restore_data < backup.sql. Enter password and it will work

Image

  1. To check database is restored or not login to MySQL: mysql -u root -p

Image

  1. Connect with restore_data database Use: Use restore_data;

Image

  1. Execute this query to get the table data: select * from Persons;

Image

If your database is restored, you will get table data as output. you have successfully backup and restored MySQL database.