AKZN Notes

Archives for My Lazy and Forgetful Mind

Import Database into XAMPP MYSQL via Windows Shell / Command Prompt

Import Database into XAMPP MySQL via Windows Shell / Command Prompt

When working with MySQL in XAMPP, importing a database via the command prompt is a quick and efficient option. This tutorial explains how to use the Windows Shell to import a database into XAMPP MySQL.


Prerequisites

  1. XAMPP Installed: Ensure XAMPP is installed on your system.
  2. MySQL Service Running: Start the MySQL service in the XAMPP Control Panel.
  3. Database Dump File: You need an existing .sql file to import.
  4. Command Prompt Access: Administrator rights may be required for certain commands.

Steps to Import Database

Step 1: Open Command Prompt

  1. Press Win + R, type cmd, and press Enter.
  2. Navigate to the MySQL bin directory inside the XAMPP folder. Replace D with the appropriate drive letter if XAMPP is on a different drive:
    cd D:\xampp\mysql\bin

Step 2: Run the Import Command

Use the following syntax to import the database:

"[drive]:\xampp\mysql\bin\mysql" -u root -p [dbname] < "folder/path/mysqldumpfile"
  • Replace placeholders:
    • [drive]: Drive where XAMPP is installed (e.g., D).
    • [dbname]: Name of the database to import into.
    • folder/path/mysqldumpfile: Full path to the .sql file.

Example Command

If XAMPP is on D drive and the dump file is located in D:\backups\database.sql, and the database name is test_db, the command would be:

"D:\xampp\mysql\bin\mysql" -u root -p test_db < "D:\backups\database.sql"

Step 3: Enter the MySQL Password

  • After executing the command, you will be prompted for the MySQL root password. Press Enter if no password is set.

Step 4: Verify the Import

  1. Open phpMyAdmin in your browser: http://localhost/phpmyadmin
  2. Select the imported database from the left sidebar.
  3. Verify that the tables and data have been imported correctly.

Common Issues and Solutions

1. Database Does Not Exist

  • Create the database manually in phpMyAdmin or using this command before importing:
     "D:\xampp\mysql\bin\mysqladmin" -u root -p create [dbname]

2. File Not Found

  • Ensure the path to the .sql file is correct and properly quoted if it contains spaces.

3. Access Denied

  • Check if the MySQL root password is set and use the correct credentials.

Summary

By using the Windows Shell, importing databases into XAMPP MySQL is straightforward and efficient. This method is particularly useful for large database files that may exceed phpMyAdmin's upload limit.

Happy coding!

Leave a Reply

Your email address will not be published.