Question
How do you import or restore a database via SSH?
Answer
Before you import or restore a database using mysqldump, you must ensure you have already created a MySQL database and MySQL database user. You can create both by accessing "cPanel / MySQL Databases."
Before importing a database, you want to make sure that your SQL dumps are complete. A quick method to check the SQL file integrity is to use the following command to view the starting and ending lines of the dump.
Note: You must replace "/$PATH/$TO/$DUMP.sql" with the path to the .sql dump file that you're trying to import.
# (head;tail) < /$PATH/$TO/$DUMP.sql
A working example should look like this. If your dump file is not properly terminated or has a proper header, it may be incomplete or corrupted.
# [root@the ~]$ (head;tail) < /root/test.sql
-- MySQL dump 10.13 Distrib 5.7.34, for Linux (x86_64)
--
-- Host: localhost Database: cptest_test
-- ------------------------------------------------------
-- Server version 5.7.34
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2021-05-19 21:47:45
Once you've verified your SQL dump file is valid and you are ready to import, you can import the database using the following command:
Note: You will need to replace "database_username" with the database username, "database_name" with the database name, and "databasebackupfilename.sql" with the .sql file that you're trying to import.
# mysql -u database_username -p database_name < databasebackupfilename.sql
Additional Resources
Comments
0 comments
Article is closed for comments.