【问题标题】:MySQL user defined function in shell scriptshell脚本中的MySQL用户定义函数
【发布时间】:2017-05-21 07:10:33
【问题描述】:

我正在运行这个 shell 脚本:

root@user:/#sh myscript.sh

它包含以下代码:

#!/bin/bash -x
mysql -u username -password base1 << EOF
UPDATE table1 SET `trans` = translit (dn);

在输出中,我的用户定义函数translit 出现错误。所有其他 MySQL 命令均已成功完成。在 phpMyAdmin 下,上述命令运行正常。函数translit 定义正确。

【问题讨论】:

  • 试试:UPDATE table1 SET `trans` = translit(dn);。见9.2.4 Function Name Parsing and Resolution
  • 刚试过.. nope ( ERROR 1064 (42000) at line 23: You have an error in your SQL syntax;检查与您的 MySQL 服务器版本相对应的手册以获取在 ' 附近使用的正确语法= translit(dn)' 在第 1 行
  • 以这种方式工作 UPDATE table1 SET table1.trans=translit(table1.dn);
  • 您的 shell 脚本是否将反引号解释为命令替换?

标签: mysql function shell sh


【解决方案1】:

此处文档中的反引号被解释为命令替换,并在 mysql 运行之前由 shell 处理。引用分隔符以保护 here 文档的内容不被 shell 处理。

#!/bin/bash -x
mysql -u username -password base1 << 'EOF'
UPDATE table1 SET `trans` = translit (dn);
EOF

(注意:我对 MySQL 不够熟悉,不知道反引号是否应该是单引号,或者这里是否需要任何引用。)

【讨论】:

    【解决方案2】:

    试试:

    文件:myscript.sh

    #!/bin/bash -x
    mysql -u username -p -s << 'EOF'
    USE `base1`;
    SELECT `dn`, `trans` 
    FROM `table1`;
    
    UPDATE `table1`
    SET `trans` = `translit`(`dn`);
    
    SELECT CONCAT(REPEAT('*', 15), ' UPDATE ', REPEAT('*', 15));
    
    SELECT `dn`, `trans`
    FROM `table1`;
    EOF
    
    $ mysql -u username -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.7.11
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> DROP DATABASE IF EXISTS `base1`;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> CREATE DATABASE IF NOT EXISTS `base1`;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> USE `base1`;
    Database changed
    
    mysql> DROP FUNCTION IF EXISTS `translit`;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> DROP TABLE IF EXISTS `table1`;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> CREATE TABLE IF NOT EXISTS `table1` (
        ->   `dn` VARCHAR(30) NOT NULL,
        ->   `trans` VARCHAR(30)
        -> );
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> INSERT INTO `table1`
        ->   (`dn`)
        -> VALUES
        ->   ('Lorem Ipsum is simply dummy'),
        ->   ('text of the printing'),
        ->   ('and typesetting industry');
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> CREATE FUNCTION `translit`(`_dn` VARCHAR(30))
        -> RETURNS VARCHAR(30) DETERMINISTIC
        -> RETURN REVERSE(`_dn`);
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> \! ./myscript.sh
    + mysql -u root -p -s
    Enter password: 
    Lorem Ipsum is simply dummy NULL
    text of the printing    NULL
    and typesetting industry    NULL
    *************** UPDATE ***************
    Lorem Ipsum is simply dummy ymmud ylpmis si muspI meroL
    text of the printing    gnitnirp eht fo txet
    and typesetting industry    yrtsudni gnittesepyt dna
    mysql>
    

    请尝试4.6.6 mysql_config_editor — MySQL Configuration Utility,以免泄露敏感信息。

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2012-10-03
      • 2018-03-23
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      相关资源
      最近更新 更多