How to write and execute a shell script to create a mysql databse and a table -


i created .sh file in ubuntu create , execute following commands , .sh files , following mysql scripts in same folder. manually created test database , created , ran scirpts individually.

content of papol.sh

mysql -h localhost -u root -padmin < apple.sql mysql -h localhost -u root -padmin < getdb.sql 

and when execute command bash -x papol.sh throws me following error

+ mysql -h localhost -u root -padmin : no such file or directoryapple.sql + mysql -h localhost -u root -padmin : no such file or directorygetdb.sql 

i not find reason this. grateful if can help

update

during next session, realized mysql wasn't recognized terminal. realized i'm dumb , doing things wrong. not use alias resolve binary folder.

example, not use alias mysql="/applications/mamp/library/bin/mysql"

instead

export path=$path:/applications/mamp/library/bin/

this allow use binaries appropriately via bash, , you'll gain access mysql binaries including mysqladmin etc terminal well. :) happy scripting!

example script appropriate environment variables:

#!/bin/bash  echo "" echo reloading database  echo "" echo using echo project_build_home: $project_build_home echo mysql: $(which mysql) echo php: $(which php) echo "" echo ""  mysqladmin -u root -pmypass drop projectbuild mysqladmin -u root -pmypass create projectbuild gunzip < $project_build_home/pack/projectbuild.sql.gz | mysql -u root -pmypass projectbuild 

old answer below

i had mysql setup in users .profile alias mysql="/path/to/mysql".

instead,

i used export mysql=/path/to/mysql

and in bash script, used $mysql.

example

cat /path/to/my/file | $mysql -h localhost -u root -padmin 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -