How to execute multiple multiline mysql queries with a shell script?
You are using a "here document" introduced by << EOF
. In this construct variable expansion and command substitution is performed unless the end delimiter (EOF
in your case) is quoted. In your example the text between backticks like `virtual_domains`
is executed as a command by the shell.
To get the expected result you have to quote the end delimiter EOF
using single or double quotes like shown below. This will prevent the expansion and substitution of the shell.
mysql -u <redacted> -p<redacted> servermail << "EOF"
CREATE TABLE `virtual_domains` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
See Here documents - Unix shells, Bash reference - Here documents
I would put the SQL statements in a file let's say sqldump.txt
and then run the import like below. Create empty database the_database
before if it doesn't exist.
mysql -u auser -p apassword the_database< sqldump.txt