如何使用BASH和EOF修复MySQL 8中引号内的双引号/单引号?

如何使用BASH和EOF修复MySQL 8中引号内的双引号/单引号?,mysql,shell,eof,Mysql,Shell,Eof,如何修复此错误: # cat <<EOF | mysql -uroot -p${MYSQL_PASSWD} > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}'; > grant all privileges on zabbix.* to '\'zabbix'\'@'\'lo

如何修复此错误:

 # cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to '\'zabbix'\'@'\'localhost'\' identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\''.
\cat将zabbix.*上的所有特权授予“\'zabbix'\'@”\'localhost'\'由“${zabbix\u PASSWD}”标识;
>同花顺特权;
>出口
>EOF
mysql:[警告]在命令行界面上使用密码可能不安全。
第2行出错:未知命令“\''。
或使用双引号:

 # cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to "\""zabbix\""@"\"localhost"\"" identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\"'.
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to "zabbix"@"localhost" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
#cat将zabbix.*上的所有特权授予“${zabbix_PASSWD}”标识的“\”“zabbix\@”“localhost”\”;
>同花顺特权;
>出口
>EOF
mysql:[警告]在命令行界面上使用密码可能不安全。
第2行出错:未知命令“\”。
无双引号/单引号:

 # cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to "\""zabbix\""@"\"localhost"\"" identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\"'.
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to "zabbix"@"localhost" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
#cat将zabbix.*上的所有权限授予zabbix@localhost由“${ZABBIX_PASSWD}”标识;
>同花顺特权;
>出口
>EOF
mysql:[警告]在命令行界面上使用密码可能不安全。
第2行出现错误1064(42000):您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解第1行“由“”标识”附近使用的正确语法
或者仅使用双引号/单引号:

 # cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to "\""zabbix\""@"\"localhost"\"" identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\"'.
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to "zabbix"@"localhost" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
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 'identified by ''' at line 1
#cat将zabbix.*上的所有特权授予“${zabbix_PASSWD}”标识的“zabbix”@“localhost”;
>同花顺特权;
>出口
>EOF
mysql:[警告]在命令行界面上使用密码可能不安全。
第2行出现错误1064(42000):您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解第1行“由“”标识”附近使用的正确语法

EOF外壳文件中的错误相同?

此处不需要反斜杠。您混淆了herdoc的命令行,反之亦然。您也混淆了mysql和命令行。每个命令行(命令行、herdoc和mysql)都有关于单引号和双引号的不同规则

  • Mysql需要用单引号括起来的字符串文字(但也可以用双引号括起来,但这并不标准)
  • bash显然有关于单引号和双引号的规则,但它们不适用于这里,因为这是一个heredoc
  • 你的herdeoc不在乎。你的herdeoc里面的东西被当作一个文件。单引号,双引号,随便什么。最酷的是bash仍然会交换变量,所以它就像一个超级文件,但它只是一个herdeoc
  • 类似于以下的操作应该可以正常工作:

    cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
    create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    flush privileges;
    exit
    EOF
    

    这里不需要反斜杠。你混淆了herdoc的命令行,反之亦然。你也混淆了mysql和命令行。每个命令行(命令行、herdoc和mysql)都有关于单引号和双引号的不同规则

  • Mysql需要用单引号括起来的字符串文字(但也可以用双引号括起来,但这并不标准)
  • bash显然有关于单引号和双引号的规则,但它们不适用于这里,因为这是一个heredoc
  • 你的herdeoc不在乎。你的herdeoc里面的东西被当作一个文件。单引号,双引号,随便什么。最酷的是bash仍然会交换变量,所以它就像一个超级文件,但它只是一个herdeoc
  • 类似于以下的操作应该可以正常工作:

    cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
    create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    flush privileges;
    exit
    EOF
    

    如果您能准确地告诉我们,在shell完成之后,
    mysql
    的文本输入应该是什么,这会有所帮助。顺便说一句,您通常不需要在herdeoc中引号前面加反斜杠;
    '
    在herdeoc中都不是特别的,因此,在它们之前添加反斜杠是没有意义的,除非您正在运行的程序需要这些反斜杠。这是必要的,因为MySQL 8需要这个双引号,但是当我输入EOF时,会出现这个错误。如果我删除了,会出现MySQL版本的语法错误。为什么在收到答案后删除了该问题?这使得创建答案的所有工作(以及你问问题的工作)对其他人来说都是无用的。如果你能准确地告诉我们,在shell完成之后,
    mysql
    的文字输入应该是什么,这会有所帮助。顺便说一句,您通常不需要在heredoc中引号字符前面加反斜杠;既不是
    也不是
    在herdoc中是非常特殊的,所以在它们之前添加反斜杠是没有意义的,除非您正在运行的程序需要这些反斜杠。这是必要的,因为MySQL 8需要这个双引号,但是当我输入EOF时,会出现这个错误。如果我删除了,会出现MySQL版本的语法错误。为什么在收到答案后删除了该问题?这使得你在创造答案时所做的所有工作(以及你在提出问题时所做的工作)对其他人来说都是无用的。