How to execute mysql commands in sh

I try to write a script, which create database and tables in it.

For example, my bash file create attempt:

#!/bin/bash
mysql -u root -p
use dbname;
create......
.......
exit;

Is executed joining into mysql console and that all. Script not create any table in database and not executed exit from mysql console.

How to correct create the script for that task?

1 Answer

Couple of different ways to do this. The easiest may be

mysql -u root -p <<EOF
use dbname;
create ....
....
exit;
EOF

This feeds everything between the command line and the EOF into mysql as input.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like