batch file to create mysql database

1

I'm trying to create a .bat batch file to create the database and then retrieve inaccuracies via sql file, follow the .bat file procedure:

@echo off
cls
@echo.
@echo Instalando DB
cd C:\Program Files\MySQL\MySQL Server 5.6\bin\
mysql.exe -u root -proot CREATE DATABASE BDNAME
mysql.exe BDNAME < C:\Users\BD.sql
pause

The problem is that the code only logs into MYSQL in the shell and does not perform the other procedures, how can I fix this or do it otherwise?

    
asked by anonymous 28.03.2015 / 23:18

2 answers

3

The syntax for using a SQL statement on the same line is this:

mysql  --execute="statement"
mysql  -e "statement"

In your case:

mysql.exe -u root -proot -e "CREATE DATABASE BDNAME"

(or you can just edit your BD.sql and the create database within it by deleting the 1st call to mysql.exe )

More details on official documentation < sup> in .


Note: do not forget to put the login parameters ( -u USER -pSENHA ) in the second command, since it is a totally independent session (unless you are logging in .sql, of course)

    
29.03.2015 / 01:45
3

The @Bacco answer already deals with your immediate problems, I'll leave a nimble way to deal with the issue:

MySQL in batch mode

MySQL can be run in batch mode, which essentially allows us to pass as a parameter a file containing all the commands to execute:

mysql -h host -u user -p < ficheiro-batch

So you do not have to be constantly calling:

 mysql -h host -u user -p -e "comando"

In short, within ficheiro-batch you can have ALL commands executed and with a single line solve the question.

Note:

Never save the password in this type of file, if you do not indicate it, when you perform what I suggest above, it will be requested! That way you do not run security risks.

    
29.03.2015 / 03:25