LoginSignup
4
3

More than 5 years have passed since last update.

MySQL ShellでSQL実行時にUnexpected identifierが発生する

Posted at

環境

  • Windows 10
  • MySQL 5.7.23 on Docker
  • MySQL Shell 8.0.12 (MySQL Installerからインストール)

原因

JavaScriptモードでSQL文を実行(解釈)していたため、Unexpected identifierが発生していた。

対処方法

SQLモードへ変更する。インタラクティブモードで\sqlコマンドで変更する、もしくは、ショートカットのプロパティへSQLモードの引数--sqlをつける。

実行例

スタートメニューからMySQL Shellを起動し、DBへ接続、スキーマ選択

MySQL Shell 8.0.12

Copyright (c) 2016, 2018, 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 '\?' for help; '\quit' to exit.

MySQL Shell 8.0.12

Copyright (c) 2016, 2018, 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 '\?' for help; '\quit' to exit.


 MySQL  JS > \connect root@127.0.0.1
Creating a session to 'root@127.0.0.1'
Please provide the password for 'root@127.0.0.1': *****
Save password for 'root@127.0.0.1:3306'? [Y]es/[N]o/Ne[v]er (default No): Y
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 563
Server version: 5.7.23 MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.

 MySQL  127.0.0.1:3306 ssl  JS > \use test_db
Default schema set to `test_db`.

 

SQL実行・・・

 MySQL  127.0.0.1:3306 ssl  test_db  JS > select now();
SyntaxError: Unexpected identifier

 MySQL  127.0.0.1:3306 ssl  test_db  JS > \source now.sql
SyntaxError: Unexpected identifier

できない。

SQLモード、JavaScriptモード、Pythonモード それぞれでnow()を実行

 MySQL  127.0.0.1:3306 ssl  test_db  JS > \sql
Switching to SQL mode... Commands end with ;
Fetching table and column names from `test_db` for auto-completion... Press ^C to stop.

 MySQL  127.0.0.1:3306 ssl  test_db  SQL > select now();
+---------------------+
| now()               |
+---------------------+
| 2019-01-22 02:36:13 |
+---------------------+
1 row in set (0.0010 sec)

 MySQL  127.0.0.1:3306 ssl  test_db  SQL > \source now.sql
+---------------------+
| now()               |
+---------------------+
| 2019-01-22 02:36:15 |
+---------------------+
1 row in set (0.0010 sec)


 MySQL  127.0.0.1:3306 ssl  test_db  SQL > \js
Switching to JavaScript mode...

 MySQL  127.0.0.1:3306 ssl  test_db  JS > new Date();
2019-01-22 11:36:25.294000

 MySQL  127.0.0.1:3306 ssl  test_db  JS > \py
Switching to Python mode...

 MySQL  127.0.0.1:3306 ssl  test_db  Py > from datetime import datetime
 MySQL  127.0.0.1:3306 ssl  test_db  Py > datetime.now()
datetime.datetime(2019, 1, 22, 11, 36, 41, 919000)


 MySQL  127.0.0.1:3306 ssl  test_db  Py > \quit

ショートカットのプロパティでSQLモードを変更する

スタートメニュー → MySQL Shell 右クリック → その他 → ファイルの場所を開く

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\MySQL

MySQL Shellのショートカットを選択し、右クリック → プロパティ → リンク先

"C:\Program Files\MySQL\MySQL Shell 8.0\bin\mysqlsh.exe" --sql

mode抜粋

mysqlsh --help
  --sql                         Start in SQL mode.
  --sqlc                        Start in SQL mode using a classic session.
  --sqlx                        Start in SQL mode using Creating an X protocol session.
  --js, --javascript            Start in JavaScript mode.
  --py, --python                Start in Python mode.

感想

「プロンプト末尾のJSってなにかな?」とは思ってました。思ってましたが・・

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3