71

Basically, I need to setup a database from a bash script. I have a script db.sql that does all this. Now how do I run this script from bash?

Database system is mysql

2 Answers 2

122

You simply need to start mysql and feed it with the content of db.sql:

mysql -u user -p < db.sql

or if you want to select the database right from the command line

mysql -u user -p database_name < db.sql
Sign up to request clarification or add additional context in comments.

1 Comment

small thing you need to add the dbscrpit path here.
75

If you want to run a script to a database:

mysql -u user -p data_base_name_here < db.sql

5 Comments

Some doubts: 1) I need to pass the password in command line ? 2) It is possible capture some kind of output from sql file to check a log file for example ?
@MCunha98 never put a sensitive information like a password in a command. Else it can be found later by going back in the history of previous commands used.
To output results to a file: mysql -u user -p data_base_name_here < db.sql > /tmp/output.txt
@MCunha98 despite the insecurities, you would simply do mysql -u user -p"password" data_base_name_here < db.sql.
Can we achieve what @MCunha98 asked this using a config file containing passwords?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.