23

I have this command:

mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql

But I get the following error:

Error
unknow command '\U'
unknow command '\m'
unknow command '\D'
unknow command '\L'
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Mamadou
  • 2,177
  • 5
  • 31
  • 43

17 Answers17

43

For those using a Windows OS, I was able to import a large mysqldump file into my local XAMPP installation using this command in cmd.exe:

C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql

Also, I just wrote a more detailed answer to another question on MySQL imports, if this is what you're after.

joelhaus
  • 903
  • 1
  • 10
  • 18
  • with the latest version of xampp i had to use mysql.exe – O V Nov 22 '15 at 11:26
  • I use the command above, substituting my own user and password and file name, and I get this output:mysql Ver 15.1 Distrib 10.1.16-MariaDB, for Win32 (AMD64) Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Usage: mysql [OPTIONS] [database] Default options are read from the following files in the given order: C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\xampp\mysql\my.ini C: \xampp\mysql\my.cnf C:\xampp\mysql\bin\my.ini C:\xampp\mysql\bin\my.cnf The following groups are read: mysql client client-server client-mariadb – James Jan 18 '17 at 14:58
  • 1
    Works. Just a clarification: this needs to be done in the windows cmd prompt. Didn't work for me in Git Bash prompt. – Ozh Apr 23 '20 at 09:46
26

Go to the xampp shell command line and run

mysql -h localhost -u username databasename < dump.sql
sth
  • 222,467
  • 53
  • 283
  • 367
bittu
  • 261
  • 3
  • 2
  • 2
    Execute the command above from within `C:\xampp\mysql\bin\` and have the `dump.sql` in the same folder. Tip for big imports that take a long while: During the import process you can refresh your phpmyadmin site and see if tables are created. – Avatar Nov 29 '16 at 18:23
10

You may also need to specify the host. From your statement, it looks like you run on Windows. Try this:

mysql -h localhost -u #myusername -p #mypasswd MYBASE
      < c:/user/folderss/myscript.sql

Or

mysql -h localhost -u #myusername -p #mypasswd MYBASE
       < "c:\user\folderss\myscript.sql"
Andreas Krueger
  • 1,497
  • 13
  • 16
  • Ok, tx. I have now access denied problem. I can access some DBs and not the one i created via phpmyadmin. I put all privileges for a user. – Mamadou May 18 '11 at 09:15
  • am connected as odbc user how to swith to root ? – Mamadou May 18 '11 at 09:46
  • You can't 'switch to root', once you're logged on. There's nothing like a 'su' or 'sudo' command as in Unix. You need to log on as root right away: - mysql -u root -h localhost ... – Andreas Krueger May 18 '11 at 11:28
6
mysql -h localhost -u username databasename < dump.sql

It works, try it. If password is blank no need to add the -p args.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
niral
  • 61
  • 1
  • 1
5

/opt/lampp/bin/./mysql -u dbusername -p dbname < sqlfilepath.sql

4

It works:

mysql -u root -p db_name < "C:\folder_name\db_name.sql"

C:\ is for example

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
3

Copy Database Dump File at (Windows PC) D:\xampp\mysql\bin

mysql -h localhost -u root Databasename <@data11.sql
matinict
  • 2,411
  • 2
  • 26
  • 35
3

this command posted by Daniel works like charm

C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql

just put the db username and db name without those backets

**NOTE: Make sure your database file is reside inside the htdocs folder, else u'll get an Access denied error

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
Shonubi Korede
  • 541
  • 6
  • 4
2

No Doubt It does the trick:

C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql

But some time you many find "MySql Server has Gone a way" For that you need to change mysql/bin/my.ini max_allowed_packet = 64M to my.ini

user3172663
  • 312
  • 3
  • 14
1

I think the commands are OK, issue is with the spaces. Please observe the spaces between -u and #myusername also between -p and #mypasswd.

mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql

Can also be written as:

mysql -u#myusername -p#mypasswd MYBASE < c:\user\folderss\myscript.sql

Also, you need not add -p and the time of command, if your password is blank, it will not ask you for the password. If you have password to your database, it will ask for the password.

Thanks

Pupil
  • 23,834
  • 6
  • 44
  • 66
1

The process is simple-

  1. Move the database to the desired folder e.g. /xampp/mysql folder.

  2. Open cmd and navigate to the above location

  3. Use command to import the db - "mysql -u root -p dbpassword newDbName < dbtoimport.sql"

  4. Check for the tables for verification.

Avinash Thombre
  • 194
  • 1
  • 5
1

In Windows, place the MySQL file (i.e. example.sql) in C:\xampp\mysql\bin.

Open the XAMPP Control Panel, and launch Shell:

cd c:\xampp\mysql\bin
mysql --default-character-set=utf8 -h localhost -u username databasename < example.sql

Note: Using --default-character-set=utf8 can help to prevent encoding issues with special characters if you intend on working with UTF-8.

Afterwards, remove the MySQL file from C:\xampp\mysql\bin.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
0

I think the OP's problem was not escaping the slashes. on windows a path like: "c:\user\folderss\myscript.sql" should in the command line be written like either:

 c:\\\user\\\folders\\\myscript.sql

or:

c:/user/folders/myscript.sql

therefore "\u" from "c:\user" was interpreted as a command.

see: http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html for more info

jny
  • 8,007
  • 3
  • 37
  • 56
Rop
  • 1
0

here is my try to import database from cmd in xampp server the correct command for me is this you can change it with your requirements.and i also attached Errors that i face first time importing db from command line.you can see last command that i use and i paste it here also its working fro me.

mysql -h localhost -u root ovxsolut_windhs < C:\Users\eee\Downloads\ovxsolut_windhs.sql

d enter image description here

Darkcoder
  • 802
  • 1
  • 9
  • 17
0

Do your trouble shooting in controlled steps:

(1) Does the script looks ok?

DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
type ansi.sql
show databases

(2) Can you connect to your database (even without specified the host)?

DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51b-community-nt MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

(3) Can you source the script? (Hoping for more/better error info)

mysql> source ansi.sql
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ...                |
| test               |
+--------------------+
7 rows in set (0.01 sec)
mysql> quit
Bye

(4) Why does it (still not) work?

DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql < ansi.sql
Enter password: ********
Database
information_schema
...
test

I suspected that the encoding of the script could be the culprit, but I got syntax errors for UTF8 or UTF16 encoded files:

ERROR 1064 (42000) at line 1: 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 '´╗┐
show databases' at line 1

This could be a version thing; so I think you should make sure of the encoding of your script.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/filename.sql

mysql -u root -p dbname< "C:\Users\sdkca\Desktop\mydb.sql"

Dhiren Biren
  • 472
  • 4
  • 7
-1
mysql -h localhost -u username -p databasename < dump.sql
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Sandeep
  • 11