How can I restore a file created by mysqldump?


Suppose if we want to restore a file that has been created by mysqldump then we can restore in an existing database or in a new database after creating it. Then with the help of SOURCE statement, we can restore it. We can illustrate it by an example:

Example

In this example, we are restoring the table named student_info.sql which has been dumped. It was basically in the database name ‘query’. Now we will restore it into a database named ‘tutorials’.

mysql> Use Tutorials;
Database changed

mysql> SOURCE student_info.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.45 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.05 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 7 rows affected (0.05 sec)
Records: 7 Duplicates: 0 Warnings: 0

Now, with the help of the following statement, we can see that ‘student_info’ table is restored in a database named ‘Tutorials’.

mysql> Show tables;
+---------------------+
| Tables_in_tutorials |
+---------------------+
| rtgs                |
| student_info        |
+---------------------+
2 rows in set (0.00 sec)

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 133  | Mohan   | Delhi      | Computers  |
| 150  | Saurabh | NULL       | Literature |
+------+---------+------------+------------+
7 rows in set (0.00 sec)

Updated on: 20-Jun-2020

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements