{"id":3176,"date":"2025-08-30T09:32:01","date_gmt":"2025-08-30T09:32:01","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=3176"},"modified":"2025-08-30T11:08:39","modified_gmt":"2025-08-30T11:08:39","slug":"how-to-import-an-sql-file-through-command-line","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-to-import-an-sql-file-through-command-line\/","title":{"rendered":"How to Import an SQL File Through Command Line"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2025\/08\/image-4-1024x683.png\" alt=\"\" class=\"wp-image-3177\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2025\/08\/image-4-1024x683.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2025\/08\/image-4-300x200.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2025\/08\/image-4-768x512.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2025\/08\/image-4.png 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Importing an SQL file through the command line is a useful skill when working with MySQL databases. Whether you&#8217;re migrating data, setting up a fresh database, or restoring a backup, the command line method is quick and effective. This guide will walk you through the process of importing an SQL file into a MySQL database using the Command Prompt (CMD).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Prerequisites:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>MySQL installed<\/strong>: Ensure that MySQL is installed on your machine. If you are using a local development environment like XAMPP or WAMP, MySQL will already be included.<\/li>\n\n\n\n<li><strong>Access to MySQL<\/strong>: You must have access to MySQL with sufficient privileges (i.e., a user with the right to import data).<\/li>\n\n\n\n<li><strong>SQL file<\/strong>: The SQL file that you want to import should be available on your system.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step-by-Step Guide to Import SQL File:<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Open Command Prompt<\/h4>\n\n\n\n<p>First, open the <strong>Command Prompt<\/strong> on your system.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>On <strong>Windows<\/strong>: Press <code>Windows + R<\/code>, type <code>cmd<\/code>, and hit Enter.<\/li>\n\n\n\n<li>On <strong>macOS\/Linux<\/strong>: Open the Terminal application.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Navigate to MySQL Bin Directory<\/h4>\n\n\n\n<p>To interact with MySQL, you need to navigate to the <code>bin<\/code> directory of MySQL. This is where the <code>mysql<\/code> command-line tool is located.<\/p>\n\n\n\n<p>For example, if you&#8217;re using <strong>XAMPP<\/strong>, the MySQL bin directory is located at:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd C:\\xampp\\mysql\\bin\n<\/code><\/pre>\n\n\n\n<p>If you have a different MySQL installation, the path might be different, so adjust accordingly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Log into MySQL<\/h4>\n\n\n\n<p>Now that you are in the MySQL bin directory, log into MySQL using the <code>mysql<\/code> command. You will need your MySQL username and password.<\/p>\n\n\n\n<p>Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u root -p\n<\/code><\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-u root<\/code>: Specifies the username (in this case, <code>root<\/code>).<\/li>\n\n\n\n<li><code>-p<\/code>: Prompts you for the MySQL password.<\/li>\n<\/ul>\n\n\n\n<p>After entering your password, you will be logged into the MySQL shell.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Create or Select a Database<\/h4>\n\n\n\n<p>Before importing the SQL file, ensure you\u2019re in the correct database. You can either <strong>select an existing database<\/strong> or <strong>create a new one<\/strong>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>To select an existing database<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>USE database_name;\n<\/code><\/pre>\n\n\n\n<p>Replace <code>database_name<\/code> with the name of your database.<\/p>\n\n\n\n<p><strong>To create a new database<\/strong> (if it doesn&#8217;t already exist):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE database_name;\n<\/code><\/pre>\n\n\n\n<p>Then, use the <code>USE<\/code> command to switch to the new database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE database_name;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Import the SQL File<\/h4>\n\n\n\n<p>Now that you&#8217;re in the correct database, it&#8217;s time to import the SQL file.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Exit MySQL shell<\/strong> by typing <code>exit<\/code> and hitting Enter.<\/li>\n\n\n\n<li><strong>Return to the Command Prompt<\/strong> (if you\u2019ve exited MySQL shell) and use the <code>mysql<\/code> command to import the SQL file directly.<\/li>\n<\/ol>\n\n\n\n<p>Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u root -p database_name &lt; \"C:\\path\\to\\your\\file.sql\"\n<\/code><\/pre>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-u root<\/code>: MySQL username (replace <code>root<\/code> if you&#8217;re using a different username).<\/li>\n\n\n\n<li><code>-p<\/code>: Prompts you to enter your password.<\/li>\n\n\n\n<li><code>database_name<\/code>: The name of the database you want to import the SQL file into.<\/li>\n\n\n\n<li><code>\"C:\\path\\to\\your\\file.sql\"<\/code>: The full path to your SQL file. Use <strong>forward slashes<\/strong> or <strong>double-backslashes<\/strong> for Windows paths.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Step 6: Verify the Import<\/h4>\n\n\n\n<p>Once the import is complete, you can verify that the tables have been imported correctly:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Log back into MySQL<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql -u root -p\n<\/code><\/pre>\n\n\n\n<p><strong>Select the database<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USE database_name;\n<\/code><\/pre>\n\n\n\n<p><strong>Show the imported tables<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW TABLES;\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>This will display all the tables that were imported from the SQL file.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Troubleshooting:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Invalid File Path<\/strong>: If you receive an error regarding the file path, ensure the path to your SQL file is correct and uses the right slashes. On Windows, use double-backslashes (<code>\\\\<\/code>) or forward slashes (<code>\/<\/code>).<\/li>\n\n\n\n<li><strong>Database Already Exists<\/strong>: If you are trying to import an SQL file into an existing database and it already has tables, ensure you\u2019re not overwriting or conflicting with any existing data.<\/li>\n\n\n\n<li><strong>Permissions<\/strong>: If you encounter permission errors, ensure your MySQL user has the correct privileges to import data.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Importing an SQL file through the command line is a useful skill when working with MySQL databases. Whether you&#8217;re migrating data, setting up a fresh database, or&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1183,2726,1181,1187,965,2770,2766,100,2769,2767,2772,567,2768,2771,60],"class_list":["post-3176","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-command-line-2","tag-command-prompt","tag-data-migration","tag-database-backup","tag-database-management","tag-database-restore","tag-import-sql-file","tag-mysql","tag-mysql-commands","tag-mysql-import","tag-mysql-tips","tag-sql","tag-sql-commands","tag-sql-file-import","tag-xampp"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/3176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=3176"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/3176\/revisions"}],"predecessor-version":[{"id":3178,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/3176\/revisions\/3178"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=3176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=3176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=3176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}