How to convert .sql file to tables in mysql db

2019-03-02 04:48发布

问题:

I have .sql files that I'm guessing is auto-generated. I did not generate this file, but anyway it includes the database name and all tables with fields. I'd like to add the db and tables to my mysql localhost and would like to know how to do this. I have tried uploading the file but keep getting errors about my sql syntax. The syntax looks all correct to me so perhaps the .sql file needs to be changed?

回答1:

From command-line:

 mysql -u root -p databaseName < file.sql

Where databaseName is an already created empty database and file.sql is the .sql file you have, you must be in the same folder as the file when you run the command. This also assumes using root as the user and that it is password protected. Modify as needed for your own setup.

Additionally, you can do the reverse by flipping the angled bracket to create a database dump to a file. Like below.

 mysql -u root -p databaseName > file.sql


回答2:

To IMPORT:

ype the following command to import sql data file:

$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

In this example, import 'data.sql' file into 'blog' database using vivek as username:

$ mysql -u sat -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:

$ mysql -u username -p -h 202.54.1.10 databasename < data.sql

OR use hostname such as mysql.cyberciti.biz

$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql

If you do not know the database name or database name is included in sql dump you can try out something as follows:

$ mysql -u username -p -h 202.54.1.10 < data.sql

Refer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html

If you want a GUI tool then you could probably use SQLyog or navicat for this.



回答3:

You also can use a GUI tool - dbForge Studio for MySQL to execute the script. The SQL syntax check option will highlight syntax errors automatically, on file opening.