Technology Encyclopedia Home >How to import local SQL files into MySQL database?

How to import local SQL files into MySQL database?

To import local SQL files into a MySQL database, follow these steps:

  1. Open Command Line or Terminal:
    Access your system's command-line interface (CLI).

  2. Navigate to the SQL File Directory:
    Use the cd command to go to the folder where your SQL file is stored. For example:

    cd /path/to/your/sql/file
    
  3. Run the MySQL Import Command:
    Use the mysql command to import the file. The basic syntax is:

    mysql -u [username] -p [database_name] < [file_name.sql]
    

    Replace [username] with your MySQL username, [database_name] with the target database, and [file_name.sql] with your SQL file name.

    Example:

    mysql -u root -p my_database < backup.sql
    

    You’ll be prompted to enter your MySQL password.

  4. Alternative: Import via MySQL Workbench (GUI):

    • Open MySQL Workbench and connect to your database.
    • Go to File > Open SQL Script and select your .sql file.
    • Click the lightning bolt icon (or press Ctrl+Shift+Enter) to execute the script.

Using Tencent Cloud Services:
If you're using Tencent Cloud Database MySQL, you can import SQL files via:

  • Tencent Cloud Console: Navigate to the Database MySQL service, select your instance, and use the Data Import feature under Backup & Restore.
  • Command Line: Use the mysql command as shown above, ensuring your Tencent Cloud MySQL instance is accessible (check firewall rules and endpoints).

For large files, consider using Tencent Cloud Data Transmission Service (DTS) for efficient migration.