How to import data in mysql database from a .sql script file under UNIX or Linux ?

The command syntax is as follows to import the data created by mysqldump command:

mysql –u user_name -p database_name <  script-file.sql

Command syntax if MySQL server is on a remote computer:

mysql -u user_name -h mysql-server-host-name -p database_name < script-file.sql

MySQL command to import data from books-script.sql file:

mysql -u root  -p books_db  < books-script.sql

where book_db is the database in which we want to import the data.


Where books-script.sql is as below:

CREATE TABLE books(title CHAR(50) NOT NULL, author CHAR(30) NOT NULL, publisher CHAR(30), topic CHAR(20), comment CHAR(100), price FLOAT);
INSERT INTO books VALUES ("Beginning XML", "David Hunter", "Wrox", "XML", "Well recommended, fairly comprehensive", 39.99);
INSERT INTO books VALUES ("MySQL", "Paul DuBois", "New Riders", "MySQL", "Excellent book, but doesn't cover Java API", 49.99);
INSERT INTO books VALUES ("Thinking in Java", "Bruce Eckel", "Prentice Hall", "Java","Well written, free on the web", 0.00);
INSERT INTO books VALUES ("Java How to Program", "Paul Deitel", "Prentice Hall", "Java","Good textbook, extremely detailed", 68.00);
INSERT INTO books VALUES ("Programming Perl", "Larry Wall", "O'Reilly", "Perl", "Usually considered THE reference", 44.95);
INSERT INTO books VALUES ("The Java Programming Language", "Ken Arnold", "Addison Wesley", "Java", "Considered to be from the source", 37.95);
INSERT INTO books VALUES ("Learning Perl", "Randal Schwartz", "O'Reilly", "Perl","Not a bad start", 29.95);
INSERT INTO books VALUES ("Effective Perl Programming", "Joseph Hall", "Addison Wesley", "Perl","Great tips, not for beginners", 34.95);

If database books_db does not exist, first create it and then import it as follows:

mysql -u root -p -e 'create database books_db'

mysql -u root -p books_db < books_script.sql