6

I'm new to MySQL. I am interested how to do this via command line; I've tried but I get NO logging in the console output and the character set is not utf-8.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nam G VU
  • 33,193
  • 69
  • 233
  • 372

3 Answers3

12
mysql -e "source /path-to-backup/backup-file.sql" db_name

or

mysql db_name < backup-file.sql

You probably need -u username -p, too in your command.

mysqldump doc

Jacob
  • 41,721
  • 6
  • 79
  • 81
  • I've tried `mysql db_name < backup-file.sql` but utf-8 data are lost – Nam G VU Jul 18 '11 at 08:23
  • Did you try both, is the correct data in the SQL file to begin with? – Jacob Jul 18 '11 at 08:24
  • Unicode? UTF-8? What's it going to be? Anyway, how does the sql file start? Files created with `mysqldump` contain all the relevant parameters to set the necessary encoding options, I believe; is your file a straight dump file? – Kerrek SB Jul 18 '11 at 12:42
  • @Kerrek This is my sample script in utf8 http://db.tt/IyNbFAj. Have tried successful using `Navicat execute sql file`but running from commandline failed – Nam G VU Jul 19 '11 at 10:51
  • 1
    Ah, your script is missing any character set configuration commands... (take a look at the result of `mysqldump` to see what I mean). You could try passing `--default-character-set=UTF8` to `mysql` maybe. – Kerrek SB Jul 19 '11 at 10:59
  • @Kerrek Adding the `--default-character-set=UTF8` to the command line solve it. Please add it as a separate answer so as I'll pick it up as accepted answer. Thanks a lot! – Nam G VU Jul 20 '11 at 04:52
9

Since your SQL script doesn't contain any character set configuration directives, mysql just runs in its default character set Latin1. To change the default character set, start mysql like this:

mysql -e "source /path-to-backup/backup-file.sql" db_name --default-character-set=UTF8
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0
C:\<installation_path>>\bin>mysql -u<<user>> -p  database_name < your_sql_file.sql

could be used if you are getting access issues like ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

hope that helps !!

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43