Question

How to import sql file into MySQL?

Posted March 8, 2014 46.8k views
You answered my question about phpmyadmin but I can't find it nor the command lines. I know how to SFTP my .sql file to the new droplet, but the mysql command to import it into the database is gone. Seems like this entire site changed overnight and I just started!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
4 answers
Let's say you want to import a mysql dump into a new database with a new user.
Where your mysqldump is source.sql

# Enter into MySQL using "root" user identified by password
mysql -u root -p

#Create the database
mysql> CREATE DATABASE NEWDB;

#Create the user
mysql> CREATE USER 'newuser'@'localhsot' IDENTIFIED BY 'newpass';

#Grant ALL privileges onthe new database to the new User
mysql> GRANT ALL PRIVILEGES ON NEWDB.* TO 'newuser'@'localhost' IDENTIFIED BY 'newpass';

#Switch to the New Database
mysql> USE NEWDB;

#Insert data from source.sql
mysql> SOURCE source.sql
mysql db_name < script.sql
If you are using phpMyAdmin, you can simply create a database and then go to the import tab and upload there, or you can SSH to your server and run 'mysql -u username -ppassword database_name < FILE.sql'.
Make sure -ppassword has your password after it with no spaces.
Thanks for all of those ways to import into mysql. I got that working. Yay.
Submit an Answer