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 mysql
mereka bekerja.
Apa yang harus saya coba selanjutnya?
Jawaban:
Di sini mendokumentasikan ekspansi variabel dan substitusi perintah dilakukan kecuali pembatas akhir dikutip. Dalam kasus Anda, string antara backtick like
virtual_domains
dijalankan sebagai perintah oleh shell.Untuk hasil yang ingin Anda dapatkan, Anda harus mengutip pembatas akhir
EOF
antara kutipan tunggal atau ganda seperti di bawah ini. Ini akan mencegah ekspansi dan penggantian.Lihat dokumen Di Sini - Kerang Unix , referensi Bash - Di sini dokumen
sumber
Saya akan meletakkan pernyataan SQL dalam file katakanlah
sqldump.txt
dan kemudian jalankan impor seperti di bawah ini. Buat database kosongthe_database
sebelumnya jika tidak ada.sumber