1. Knowledge Base
  2. >
  3. Results
  4. >
  5. How to Import Your MySQL Database

How to Import Your MySQL Database

 

There are multiple ways by which you can import the contents of a MySQL database. This article will cover two methods done directly within the tools on our servers.

This article also discusses a list of common issues and recommended solutions, and some commonly asked questions.

Note: The cPanel has imposed an upload size limit of 50MB for databases. You may encounter a timeout prompt when importing more than 50MB. If you experience this time out, please contact us via phone or Live Chat so we can assist you.


How to Import Databases via phpMyAdmin

This video will visually walk you through importing a database using phpMyAdmin from cPanel.


To import databases via phpMyAdmin:

  1. Log in to the cPanel.
  2. Locate the Databases section, then click the phpMyAdmin icon.

    cPanel's phpMyAdmin

  3. If the database does not exist, please create a database first, a user, and give the user full privileges to the database via the MySQL® Databases. Note the username and password.

    You may want to refer to this article for instructions: How To Create or Delete a MySQL Database or User.

  4. Inside the phpMyAdmin, select the new database name from the left-hand menu.
    phpMyAdmin - Select Database
  5. Click the Import tab in the main area of phpMyAdmin.

    cPanel - phpMyadmin's Import Tab

  6. Browse for the .sql file on your computer using the Choose File button.
  7. Once the .sql file was attached, scroll down and click on the Go button.
  8. A confirmation prompt will appear, notifying you about the successful import.

How to Import Databases via Command Line (SSH)

Advanced users may wish to use SSH to access and make changes to their accounts, offering a more robust set of options with fewer limitations. These instructions will require a basic familiarity with SSH. If you are interested in learning to use SSH, please check the article below.

To import your database after you have logged in to your account with SSH:

Note: Your database file must be in .sql format. It cannot be compressed in a .zip or .tar.gz.
  1. Upload your database to your home directory (This may be done via cPanel File Manager, an FTP client, or SSH).
     
  2. If the database does not exist, please create a database, a user, and give the user full privileges to the database. Note the username and password.

    For assistance with creating a Database and Database user, please see the article below:

  3. Log in to your server via SSH.
     
  4. Use the ls -l command to verify that the file is in your current directory.
     
  5. If the file is not present, upload/move the file to the correct directory or navigate to the directory where the file is located.
     
  6. Enter the following command, replacing 'user_name', 'database_name, and 'file.sql' with the correct values for your database and file:

    mysql -p -u user_name database_name < file.sql
     
  7. You will be prompted for your database user password, and then your database will be imported.
Notes:
  • HostGator recommends using the database user with the database user's password.
  • Make sure your database name and username has your HostGator cPanel username prefix (e.g. 'cPanelUsername_databasename'; 'cPanelUsername_databaseUsername').

Common Issues

Your import file is too large!

The phpMyAdmin upload limit on HostGator shared and reseller hosting packages is 50MB. This is also the default on VPS and Dedicated servers.

Users on a VPS or Dedicated server may increase this value by increasing the cPanel PHP max upload size and the cPanel PHP max POST size from WHM under Tweak Settings.

To import a file larger than 50MB, upload the file to your server and contact us via phone or Live Chat with the name of the file and the name of the database to import it to.

Script Timeout Passed

This error is received when the database is taking too long for phpMyAdmin to import, causing the phpMyAdmin import process to be terminated.

If you experience this timeout, please upload the database backup file to your HostGator account and contact us via phone or Live Chat with the details. Our administrators will perform the import for you.

For security reasons, it's best to upload your database to the /home/USERNAME section of your account. (Replace USERNAME with your cPanel username.)


#1044 - Access denied for user 'username1'@'localhost' to database 'user2_wrdp9' 

The problem here is your import file contains an SQL query that attempts to create a database for the wrong username. Notice the user2 in 'user2_wrdp9' does not match the username1 in 'username1'@'localhost'. Someone must edit the import file and change the old user2 to your new username1. (If you do not feel comfortable editing your import file, we will be glad to help you do that.) Here is an example of what it looks like (notice 3 places where the username is outdated):

□□--□-- Database: `user2_wrdp9`□--□CREATE DATABASE `user2_wrdp9` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;□USE `user2_wrdp9`;□□--


#1044 - Access denied for user 'username1'@'localhost' to database 'wrdp9' 

Same as the previous issue, the correct username is missing. On a shared server, your database names must always look like something_something.


#1049 - Unknown database 'username1_wrdp9'

The problem is your import file does not have a query to create the database before importing the

data. Simply go to cPanel > MySQL Databases and create a database with that name (in my example, "wrdp9"). Then re-attempt your import.


#1007 - Can't create database 'username1_wrdp9'; database exists 

The problem here is your import file contains an SQL query that attempts to create a database that already exists. If the database is empty, simply go to cPanel > MySQL Databases and remove that empty database; then re-attempt your import. If the database is not empty, someone must edit the import file and remove the CREATE DATABASE query. (If you do not feel comfortable editing your import file, we will be glad to help you do that.) Here is an example of what it looks like:

□CREATE DATABASE `username1_wrdp9` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;


#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...

The file does not contain backup data for a database, or the file has been changed or corrupted.


ERROR 1044 (42000): Access denied for user 'username1'@'localhost' to database 'username1_wrdp9'

If you get this error, you do not have privileges on user_* to use Create. HostGator needs to correct this issue for you.

However, if you need to import now, and can't wait, here is a workaround.

The problem here is your import file contains at least one SQL query that attempts to create a database, and you do not have the privilege to do so. Someone must edit the import file and remove the CREATE DATABASE query. (If you do not feel comfortable editing your import file, we will be glad to help you do that.)

Here is an example of what it looks like:

□CREATE DATABASE `username1_wrdp9` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

After you remove this code, you must go to cPanel and click the MySQL Databases icon. Here you must create your database with the name which was removed from the import file (in my example, "wrdp9"). Then you can Import the modified import file, and it will work.


Frequently Asked Questions

How Do You Create a MySQL Backup via your phpMyAdmin?

It is always important to create a backup of your database so it can be restored in case of unforeseen events. 

You can either do this through the backup feature of your cPanel or the export tab of phpMyAdmin.

 

Do You Need to Create a Database Before You Import Another One?

There needs to be an existing database and database user in the server to attach the imported .sql file. One database is for one .sql file.

If you have multiple .sql files to be imported, you will need to create databases for each of them through your cPanel.

If you have no existing database, you will need to create a new one via cPanel's MySQL Databases section.

 

How Do You Add a MySQL Database and Database User to Your Account?

Please refer to the article, How to Create or Delete a MySQL Database or User, for creating a database and database user.

 

Do You Have to Remove the Old Database First Before You Import the Backup Database File, or Is That Done Automatically?

If you wish to use a different .sql file or import your backup file for your existing database, you first need to DROP all the database tables. Not dropping all tables will retain the tables that do not exist in the new .sql file and overwrite the existing ones. However, you can still retain some tables in that database if you wish. The tables in your new .sql file will overwrite the tables with the same table name existing in the database.