How do i create a .sql file in mysql?

To export or import with MySQL, begin by logging into your server, preferably with sudo (root) access.

Exporting from MySQL

The best tool for exporting a MySQL database to a text file is mysqldump.

To use mysqldump, you will need to know the login credentials of an appropriate MySQL user that has the necessary privileges to export the database in question.

With that information in hand, enter the mysqldump command with the appropriate flags and options:

$ mysqldump -u my_username -p database_name > output_file_path

The options in use are:

  • The -u flag indicates that the MySQL username will follow.
  • The -p flag indicates we should be prompted for the password associated with the above username.
  • database_name is of course the exact name of the database to export.
  • The > symbol is a Unix directive for STDOUT, which allows Unix commands to output the text results of the issued command to another location. In this case, that output location is a file path, specified by output_file_path.

Note: It is generally advisable to input the fully qualified path and filename for the output_file_path, so the resulting file is generated exactly where you want it.

For example, to export the books database as the book_admin user to the ~/backup/database directory, we might use the following command:

$ mysqldump -u book_admin -p books > ~/backup/database/books.sql
Enter password:

After entering our password when prompted above, this command then creates our backup file with a .sql suffix (which is completely optional but advisable) in the appropriate directory.

By default, mysqldump will not save commands which attempt to modify the existence of the actual database. Instead, by default, only actual tables (and their respective data) are saved and thus will be prepared for later import using this file. If you need the ability to export (and later recreate) one more more databases, read up on the --databases flag in the official documentation.

Importing Into MySQL

Now that you’ve learned how to export a backup of a MySQL database, we’ll explore how to reverse the process and import the backup into an existing database.

As you might imagine, to compliment the mysqldump command used for exporting, there is a similar mysqlimport command for importing.

In most cases, importing is just a matter of passing virtually identical options to the mysqlimport command. To import our saved books.sql file created earlier, we’d use many of the same flags and much the same syntax.

$ mysqlimport -u book_admin -p books_production ~/backup/database/books.sql
Enter password:

As before, the -u and -p flags are required for authentication, which is then followed by the name of the database to import into (in this case, we’re using different, production database), then lastly specifying the fully-qualified path to the SQL dump file that contains our import data, ~/backup/database/books.sql. It’s also worth noting that mysqlimport doesn’t require the < or > (STDIN/STDOUT) symbols that were used with mysqldump.

With that, you’ve learned how to both export/backup an existing database, and how to then import/restore that data into the same or even a different database.

This article describes how to import MySQL databases and export MySQL databases. You can import and export databases for a variety of scenarios, including:

  • Transferring a MySQL database from one web hosting account or provider to another.
  • Importing a third-party MySQL database.
  • Backing up a MySQL database.

Table of Contents

  • How to export a MySQL database
    • Method #1: Use phpMyAdmin
    • Method #2: Use the mysqldump table program
  • Creating a new MySQL database and assigning a user
  • How to import a MySQL database
    • Method #1: Use phpMyAdmin
    • Method #2: Use the mysql program
    • Troubleshooting a MySQL database import
  • More Information

How to export a MySQL database

You can export a MySQL database to a file by using phpMyAdmin or the mysqldump table/database command line program.

Method #1: Use phpMyAdmin

You can export a MySQL database using the phpMyAdmin web interface. To do this, follow these steps:

  1. Log in to cPanel.

    If you do not know how to log in to your cPanel account, please see this article.

  2. In the DATABASES section of the cPanel home screen, click phpMyAdmin:

    How do i create a .sql file in mysql?

    The phpMyAdmin administration page appears in a new window.

  3. In the left pane of the phpMyAdmin page, click the database that you want to export.
  4. Click the Export tab.
  5. Under Export method, confirm that Quick is selected.

    If you are using an older version of phpMyAdmin that does not have the Quick option, follow these steps instead:

    • In the Export section, click Select All.
    • Select the Save as file checkbox, and then click Go. The export process runs.

  6. Under Format, confirm that SQL is selected.
  7. Click Go.
  8. In the Save File dialog box, type the filename and select the directory where you want to save the exported database on your local computer.
  9. Click Save. The export process runs.
Method #2: Use the mysqldump table program

You can export a MySQL database from the command line using the mysqldump table/database program. To do this, follow these steps:

  1. Access the command line on the computer where the database is stored. For example, if the database is on another web hosting account or with another web hosting provider, log in to the account using SSH. If you have physical access to the computer, you can open a DOS or terminal window to access the command line.
  2. Type the following command, and then press Enter. Replace username with your username, and dbname with the name of the database that you want to export:
    mysqldump --routines -u username -p dbname > dbexport.sql
    

    This example uses the dbexport.sql filename for the exported database, but you can name the file whatever you want.

  3. Type your password at the Enter password prompt.
  4. The dbexport.sql file now contains all of the data for the dbname database. If the dbexport.sql file is on a remote computer, download the file to your local computer.

Creating a new MySQL database and assigning a user

Before you can import the database, you must create a new database in cPanel and assign a user to it. To do this, follow these steps:

  1. Log in to cPanel.

    If you do not know how to log in to your cPanel account, please see this article.

  2. In the DATABASES section of the cPanel home screen, click MySQL® Databases:

    How do i create a .sql file in mysql?

  3. Under Create New Database, in the New Database text box, type the name of the database.
  4. Click Create Database. cPanel creates the database.
  5. When the database is created, click Go Back.
  6. Under Add User to Database, in the User list box, select the user that you want to add.
  7. In the Database list box, select the new database.
  8. Click Add.
  9. Select the check boxes to grant the user specific privileges, or select the ALL PRIVILEGES check box to grant the user all permissions to the database.
  10. Click Make Changes. cPanel adds the user to the database.

How to import a MySQL database

After you have created a new database in cPanel, you can import the database's contents by using phpMyAdmin or the mysql command line program.

If the exported database file contains any CREATE DATABASE statements, you must remove them or comment them out. Otherwise, the import process will fail.

Method #1: Use phpMyAdmin

You can import a MySQL database using the phpMyAdmin web interface. To do this, follow these steps:

  1. Log in to cPanel.

    If you do not know how to log in to your cPanel account, please see this article.

  2. In the DATABASES section of the cPanel home screen, click phpMyAdmin:

    How do i create a .sql file in mysql?

    The phpMyAdmin administration page appears in a new window.

  3. In the left pane of the phpMyAdmin page, click the database that you want to import the data into.
  4. Click the Import tab.
  5. Under File to Import, click Browse, and then select the dbexport.sql file on your local computer.
  6. Click Go. The import process runs.
  7. The database should now contain the data that is in the dbexport.sql file.
Method #2: Use the mysql program

You can import a MySQL database from the command line using the mysql program. To do this, follow these steps:

  1. Transfer the dbexport.sql file to your A2 Hosting account using SCP, SFTP, or FTP.
  2. Log in to your A2 Hosting account using SSH.
  3. Change to the directory where you uploaded the dbexport.sql file. For example, if you uploaded the dbexport.sql file to your home directory, type cd ~.
  4. Type the following command, and then press Enter. Replace username with your username and dbname with the name of the database that you want to import the data into:
    mysql -u username -p dbname < dbexport.sql
    
  5. The dbname database should now contain the data that is in the dbexport.sql file.
Troubleshooting a MySQL database import

You may receive one of the following error messages when you try to import a MySQL database using either phpMyAdmin or the mysql program:

  • ERROR 1044: Access denied for user 'username1'@'localhost' to database 'username2_database'
    This error message occurs when the import file contains an SQL statement that attempts to access a database for the wrong username. Note in this example that username2 in username2_database does not match username1 in 'username1'@'localhost'. You must edit the import file and change username2 to your new username1.
  • ERROR 1049: Unknown database 'username_database'
    This error message occurs when the target database does not exist. Make sure you create the database first as described above, and then try to it import again.
  • ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'username_database' at line x.
    This error message occurs when the import file does not contain backup data for a database or there is a MySQL syntax error in the file. Alternatively, the import file may be altered, corrupt, or in an unsupported format. (Import files must contain SQL statements; other file formats such as CSV do not work with the mysql program.) Try exporting the database again, and then try to import it.
  • ERROR 1227: Access denied; you need (at least one of) the SUPER privilege(s) for this operation
    This error message occurs when the import file contains one or more SQL statements that require superuser privileges (such as SET GLOBAL or CREATE DEFINER statements). In some cases, you can just delete these statements from the .sql file and rerun the import process. For example, CREATE DATABASE statements can be safely removed, because you should have already created the database in cPanel. If you require MySQL superuser access, however, you should consider migrating your account to a VPS or Dedicated server, where you have complete control over the environment.

    For more information about MySQL user privileges on shared servers, please see this article.

More Information

  • For more information about the mysqldump table/database command line program, please visit http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html.
  • For more information about the mysql command line program, please visit http://dev.mysql.com/doc/refman/5.1/en/mysql.html.
  • For more information about phpMyAdmin, please visit http://www.phpmyadmin.net.

How do I create a new SQL file in MySQL?

If you want to create a new database for the SQL file, you can do it with the following command: mysql> CREATE DATABASE DatabaseName; To create a MySQL user and assign a new password to it, run the following command: mysql> CREATE USER 'DatabaseUser'@'localhost' IDENTIFIED BY 'password';

How do I create a .SQL file?

Creating a SQL File.
In the Navigator, select the project..
Choose File | New to open the New Gallery..
In the Categories tree, expand Database Tier and select Database Files..
In the Items list, double-click SQL File..
In the New SQL File dialog, provide the details to describe the new file. ... .
Click OK..

How do I save a .SQL file in MySQL?

Export a MySQL database.
Log into your server via SSH..
Use the command cd to navigate to a directory where your user has write access. ... .
Export the database by executing the following command: mysqldump --add-drop-table -h internal-db.s00000.gridserver.com -u username -p dbname > dbname.sql..

How do I save a .SQL file?

Enter and then execute the SQL statement. In the Save Results dialog box, specify the following settings: Save In: Select a directory in which to save the file. File Name: Type a name for the file.