Mark
Mark

Reputation: 123

execute MYSQL commands from a file in terminal

I've been searching to understanding the following MySQL command:

framework_mariadb.sql | mysql -u username -p password -h 127.0.0.1 -P 26257 target

My guess was the sql statements within the sql file get executed by mysql for the given target/database. But then I came across the source command in MySQL, ie

\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql

So my question is, does the first command and the second command essentially do the same thing? My apologies if this has already been asked, I haven't been able to find details for the first SQL command.

Upvotes: 0

Views: 190

Answers (1)

This is more about Linux shell capabilities than it is about MySQL.

The second form runs the mysql client, and uses the < symbol to tell it to take its input from the specified file.

The first form does essentially the same thing, but uses the pipe character | to indicate that the output of the first command should be sent to the input of the second command.

However, for the first form I'd expect the line to start with cat (as in cat framework_mariadb.sql | mysql ...) because the SQL script won't normally run as a shell command. cat is a command that reads one or more files and send s them to its output.

It is possible to set the SQL script up to run like this, but that requires a specific line (#! /bin/cat or similar) to be present at the top of the file, and the file must have the execution bit set. (At least, that's how I'd do it. There might be some other bash magic I'm not aware of. bash is not my forté)

There are many resources on the web that can teach the fundamentals of the Linux shell. You could try Microsoft's Introduction to bash, but there are many others.

Upvotes: 1

Related Questions