batch file to create mysql database

Asked

Viewed 1,775 times

1

I’m trying to create a batch file. bat to create the database and then recover formations via sql file, follows file procedure . bat:

@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 it or do otherwise?

2 answers

3


The syntax for using an SQL command 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 simply edit your BD.sql and by the create database within it, eliminating the first call to mysql.exe)

More details on official documentation en.


Note: don’t forget to put the login parameters (-u USER -pSENHA) in the second command, as it is a totally independent session (unless you are logging into the used . sql, of course)

  • Thank you! I will do this to get all in one . SQL.

3

To reply by @Bacco already dealt with your immediate problems, I’ll leave an agile way to deal with the issue:

Mysql in batch mode

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

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

So there’s no need to keep calling:

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

In short, within the ficheiro-batch you can have ALL commands to execute and with one line solve the question.

Note:

Never store the password in this type of files, if you do not indicate it, when executing what I suggest above, it will be requested! So you don’t take security risks.

  • thanks answers are never enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.