Shah9il
Shah9il

Reputation: 61

Inserting data in mysql with shell script

I have a problem with a shell script:

I am trying to inject data from a source file containing MySQL queries into a data base. Here are the relevant lines of my shell script:

mysql -u root
source /usr/local/insert.sql;
quit;

For example I am running the file as ./insertfile and it is running smoothly but when it comes to data insertion in MySQL it is logging into MySQL using the mysql -u root command but the remaining operations (source /usr/local/insert.sql; and quit;) are not being executed. When I quit MySQL manually it tries to execute the rest of the command from my insert.sql file.

So please help me use the right shell script so that I can insert the queries from the source file.

Upvotes: 1

Views: 2971

Answers (3)

Shah9il
Shah9il

Reputation: 61

Thanks for your help. I have tried adding your line in my script and it was primarily giving some errors then I changed the command like below -

mysql -u root --execute="source /usr/local/insert.sql; \q"

Above line helped me to execute my command.

Thanks to all for being this much helpful.

Regards, Shah9il

Upvotes: 0

Up_One
Up_One

Reputation: 5271

It seems that your import hangs !
Check for lock on your database.

show processlist;

Run FLUSH TABLES to release any possible locks and then run your import command.
if source command hangs again :

  • Enter your myslq server
  • drop database insert;
  • create database insert
  • exit the mysql server and run:

    mysqldump -u -p database-name < dump.sql

Upvotes: 0

zbrunson
zbrunson

Reputation: 1757

One way to do that would be

mysql -u root --execute="source /usr/local/insert.sql; quit;"

Upvotes: 1

Related Questions