Bagaimana cara menjalankan beberapa query mysql multiline dengan skrip shell?

3

Saya ingin menjalankan beberapa permintaan MySQL dari skrip shell.

Saya menggunakan kode berikut:

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;

CREATE TABLE `virtual_users` (
`id` INT NOT NULL AUTO_INCREMENT,
`domain_id` INT NOT NULL,
`password` VARCHAR(106) NOT NULL,
`email` VARCHAR(120) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `virtual_aliases` (
`id` INT NOT NULL AUTO_INCREMENT,
`domain_id` INT NOT NULL,
`source` varchar(100) NOT NULL,
`destination` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

EOF

Tapi saya terus menerima pesan kesalahan ini:

virtual_domains: command not found
No command 'name' found, did you mean:
 Command 'named' from package 'bind9' (main)
 Command 'namei' from package 'util-linux' (main)
 Command 'lame' from package 'lame' (universe)
 Command 'uname' from package 'coreutils' (main)
 Command 'nama' from package 'nama' (universe)
 Command 'mame' from package 'mame' (multiverse)
 Command 'nam' from package 'nam' (universe)
name: command not found
virtual_users: command not found
domain_id: command not found
password: command not found
No command 'email' found, did you mean:
 Command 'mail' from package 'mailutils' (universe)
 Command 'dmail' from package 'uw-mailutils' (universe)
 Command 'kmail' from package 'kmail' (main)
 Command 'cmail' from package 'xboard' (universe)
 Command 'tmail' from package 'uw-mailutils' (universe)
 Command 'rmail' from package 'rmail' (universe)
 Command 'rmail' from package 'exim4-daemon-heavy' (main)
 Command 'rmail' from package 'courier-mta' (universe)
 Command 'rmail' from package 'masqmail' (universe)
 Command 'rmail' from package 'postfix' (main)
 Command 'rmail' from package 'exim4-daemon-light' (main)
 Command 'wmail' from package 'wmail' (universe)
 Command 'emil' from package 'emil' (universe)
email: command not found
No command 'email' found, did you mean:
 Command 'mail' from package 'mailutils' (universe)
 Command 'dmail' from package 'uw-mailutils' (universe)
 Command 'kmail' from package 'kmail' (main)
 Command 'cmail' from package 'xboard' (universe)
 Command 'tmail' from package 'uw-mailutils' (universe)
 Command 'rmail' from package 'rmail' (universe)
 Command 'rmail' from package 'exim4-daemon-heavy' (main)
 Command 'rmail' from package 'courier-mta' (universe)
 Command 'rmail' from package 'masqmail' (universe)
 Command 'rmail' from package 'postfix' (main)
 Command 'rmail' from package 'exim4-daemon-light' (main)
 Command 'wmail' from package 'wmail' (universe)
 Command 'emil' from package 'emil' (universe)
email: command not found
No command 'email' found, did you mean:
 Command 'mail' from package 'mailutils' (universe)
 Command 'dmail' from package 'uw-mailutils' (universe)
 Command 'kmail' from package 'kmail' (main)
 Command 'cmail' from package 'xboard' (universe)
 Command 'tmail' from package 'uw-mailutils' (universe)
 Command 'rmail' from package 'rmail' (universe)
 Command 'rmail' from package 'exim4-daemon-heavy' (main)
 Command 'rmail' from package 'courier-mta' (universe)
 Command 'rmail' from package 'masqmail' (universe)
 Command 'rmail' from package 'postfix' (main)
 Command 'rmail' from package 'exim4-daemon-light' (main)
 Command 'wmail' from package 'wmail' (universe)
 Command 'emil' from package 'emil' (universe)
email: command not found
virtual_aliases: command not found
domain_id: command not found
-bash: source: filename argument required
source: usage: source filename [arguments]
destination: command not found
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(
uid=0(root) gid=0(root) groups=0(root)  INT NOT NULL AUTO_INCREMENT,
 VARCHAR(' at line 1

Ketika saya menjalankan perintah satu per satu di dalamnya mysqlmereka bekerja.

Apa yang harus saya coba selanjutnya?

narzero
sumber
Catatan: kutipan `dalam bash shell dimaksudkan untuk mengevaluasi string di antara mereka. Itulah sebabnya itu mengeluh tentang perintah yang tidak diketahui, itu mencoba mengeksekusi konten setiap kali ia menemukan string antara tanda kutip tunggal terbalik (`)
Marcelo
Terima kasih. Apa yang akan Anda sarankan agar saya coba?
narzero

Jawaban:

2

Di sini mendokumentasikan ekspansi variabel dan substitusi perintah dilakukan kecuali pembatas akhir dikutip. Dalam kasus Anda, string antara backtick like virtual_domainsdijalankan sebagai perintah oleh shell.

Untuk hasil yang ingin Anda dapatkan, Anda harus mengutip pembatas akhir EOFantara kutipan tunggal atau ganda seperti di bawah ini. Ini akan mencegah ekspansi dan penggantian.

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;

Lihat dokumen Di Sini - Kerang Unix , referensi Bash - Di sini dokumen

pabouk
sumber
5

Saya akan meletakkan pernyataan SQL dalam file katakanlah sqldump.txtdan kemudian jalankan impor seperti di bawah ini. Buat database kosong the_databasesebelumnya jika tidak ada.

mysql -u auser -p apassword the_database< sqldump.txt
mestia
sumber