82

I just installed MySQL on my computer and when I try to create a database from the MySQL shell, I get this error:

MySQL  JS > CREATE DATABASE databasename;
SyntaxError: Unexpected identifier

Does anyone know why this is happening? Is there a problem with the installation of MySQL?

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
Slaknation
  • 2,124
  • 3
  • 23
  • 42

7 Answers7

171

By looking at this thread here, it looks like you might be using the shell in js mode, you might be able to toggle it off by using the switch

\sql 

https://forums.mysql.com/read.php?10,661192,661203#msg-661203

Max Carroll
  • 4,441
  • 2
  • 31
  • 31
  • 8
    And just in case it isn't painfully obvious you can tell whether you are in JS mode or SQL mode as there will be a JS or SQL immediately following the initial MySqQL prompt – Austin737 Jun 02 '19 at 05:09
  • 11
    but why is the JS mode the default? why is there even a JS mode to begin with? – Cpt. Senkfuss Jun 16 '20 at 16:04
  • 2
    That is a good question! In the documentation here https://downloads.mysql.com/docs/mysql-shell-8.0-en.pdf it says that javascript is the default mode, but it doesn't say why – Max Carroll Jun 17 '20 at 00:02
  • Wow, this was more obscure than expected. Kinda weird that you can't get any help command from JS – AlexKalopsia Apr 23 '21 at 08:33
  • 2
    It's stupid that mySQL8 starts in JS mode, where you can't run SQL commands. – Andrew Koper May 21 '21 at 21:46
  • Thanks for the info! I just downloaded mysql and wanted to do a very quick test and color me surprised when not even a SELECT 1 worked and instead everything were throwing syntax errors. Who in their right mind decided JS was good default ??? – Alex. S. Aug 17 '21 at 23:46
  • I get the error when invoking a script from cmd with mac like `mysqlsh -u .. -p .. -h amazon.. < script.sql`. Seems like `mysqlsh` reads the script in `js` mode.. – Timo May 17 '22 at 16:06
69

You can type:

Mysql JS>  \sql
Mysql SQL>  \connect root@localhost

and provide your password.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rahul Prasad
  • 691
  • 5
  • 4
7

Logging in as follows did work as shown by Rahul in another answer:

Mysql JS>  \sql
Mysql SQL>  \connect root@localhost

However, I was able to get Mysql> instead of Mysql JS> or Mysql SQL> by opening the Windows command prompt and entering the following:

mysql -u root -p
Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
1

try using connect root@localhost and then try \sql to switch to sql mode if the default mode is JS.

Hari Ravi
  • 17
  • 5
1

JS > shows it was started in JavaScript mode.

You can start mysqlsh with --sql to start in SQL mode:

mysqlsh --sql mysql://username:password@hostname:port/database

https://dev.mysql.com/doc/mysql-shell/8.0/en/mysqlsh.html#option_mysqlsh_sql

runwuf
  • 1,701
  • 1
  • 8
  • 15
1

Hope you would have done the complete User Setup by providing appropriate username and password. Now to initialize to interact with the sql database you need to follow below steps:

1.Using cmd or powershell,first get into sql directory by using \sql 2.Now use \connect root@localhost to connect the server to SQL MODE. It will ask you to provide password and then save it.

Now you are good to go!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '23 at 16:34
0

After using \connect root@localhost code, I am still getting the following error, even after putting in my password: MySQL Error 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/31012301) – Simas Joneliunas Feb 11 '22 at 06:21