import a sql file using mysql command line tools - mysql

here is my command to import a sql file into mysql using command line
mysql -p eshop < c:\xampp\mysql\eshop.sql
But there is error message
Error:
Unknown command '\x'
Error:
Unknown command '\m'
Error:
Unknown command '\e'
Anyone knows what's wrong with the command?

Try this, I think this may help you.
mysql -u username -p database_name < file.sql
check mysql Options. Don't use \ (slash) use / (slash) to mention the file path.

On Windows, the pathname separator character is
‘\’, but MySQL treats the backslash as the escape character in strings. To deal with this
issue, write separators in Windows pathnames either as ‘/’ or as ‘\\’. To load a file named
C:\mydata\data.txt, specify the filename as shown in either of the following statements:
mysql <database_name> < ‘C:/mydata/data.txt’
mysql <database_name> < ‘C:\\mydata\\data.txt’

Related

Convert and import sql file into postgresql

I want to migrate database from mysql to postgresql. So, first did the mysqldump with this command
mysqldump -u root -p --compatible=postgresql --default-character-set=utf8 databasename > output.sql
Then i upload the output.sql to host and import the sql file using below command
psql -U root -d databasename -f /home/test/output.sql
But i got error when i try to import
ERROR: syntax error at or near "KEY"
ERROR: syntax error at or near "UNLOCK"
ERROR: syntax error at or near "("
invalid command \n
invalid command \'></script>
invalid command \";s:2:
invalid command \',
invalid command \']
Query buffer reset (cleared).
The size of database that I dumped is 7.5gb.
The SQL used in MySQL and that used in Postgres are not one-to-one inter-compatible. You need a converter program for anything non-trivial.
From the documentation for --compatible:
This option does not guarantee compatibility with other servers.
In other words, it's only slightly more compatible.
There are commercial products like Navicat which can help do this for you automatically, or you can dump out all your MySQL data in a neutral format like XML, JSON or CSV and read it back in using some other tool.

Mysql ERROR: ASCII '\0' while importing sql file on linux server

I am getting following error while importing sql file
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode.
Set --binary-mode to 1 if ASCII '\0' is expected. Query: ''.
How do I fix this?
Try something like :
mysql -u root -p -h localhost -D database --binary-mode -o < dump.sql
and make sure your sql file is not zipped.
I encountered this problem,the sql file was in a valid ISCII format, I solved as the following:
1- in shell use file command to detect type of data contained in the dump file:
file db.sql
got output like following:
db.sql: Little-endian UTF-16 Unicode text, with very long lines, with CRLF line terminators
2- convert the existing dump file to UTF8 (ASCII) using iconv:
iconv -f utf-16 -t utf-8 db.sql > db_utf8.sql
then import the new file.
I just had this issue because the file was gzipped. I unzipped it and had no further issue.
Adding to it:
Path to db also should be configured correctly. Considering you are running find command on mac then use sudo and unzipped data file name could be put in below code at the place of [some_name_of_data.sql]
sudo find / -name [some_name_of_data.sql] -type d
or if you don't have sudo access as working in some company system then use below command:
find / -name [some_name_of_data.sql]
And something like this (eg.
fin sqli /System/Volumes/Data/Users/clinto.abraham/projects/sme/Data/react-dev-2022-08-02.sql ) could be used to set the data in a docksal environment.
fin sqli [some_path_to_db]

How to execute a SQL file using MySQL command line tool?

I'm using symfony, putty and other SQL commands seem to work fine but when I try to run an .sql file (of which there are many), the command line throws an error:
You have an error in your SQL syntax; check the manual ...
This is the command I'm trying to execute:
mysql < data/MySQLFile.sql;
I already used the use command on the proper database and tried using the full path, relative path, etc. Not sure what the problem is.
Here's the statement:
ALTER TABLE tablename
ADD COLUMN `blahblah` varchar(10) NULL;
ALTER TABLE tablename
ADD COLUMN `ggggggg` varchar(50) NULL;
if you want to execute through command line
mysql -u user -p < file.sql
and if you want to execute once logged in to mysql database. use any of the below commands.
source file.sql
or
\. file.sql
or
mysql db_name <file.sql
Ok if you are in the mysql interface already type in:
\. /full path/to/file.sql
What I meant with full path is that you need to specify according to the current OS file system the location of the file. Just by putting data/ is not enough. For example if you are using MAC OS it would be:
From the terminal and assuming is in Documents:
mysql < /Users/<yourusername>/Documents/file.sql
If you are already logged in mysql then type in:
\. /Users/<yourusername>/Documents/file.sql
If my tips don't help you, I will recommend you to go to this page:
http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
To Find out more about this.
I think you just need to type 'source' before the path to the sql file. So you would have
mysql < source [fullpath]/MySQLFile.sql;

Copying a mysql database generates "ERROR: unknown command" when importing

I'm on a japanese system using xampp. This is the line I use to dump my database.
c:\xampp\mysql\bin>mysqldump.exe -uroot wp_newsja > dump.sql
Then I create a database on another server.
c:\xampp\mysql\bin>mysqladmin -uroot create db
But when I try to execute the sql...
c:\xampp\mysql\bin>mysql -uroot db < dump.sql
... I get the following error.
ERROR at line 145: Unknown command '¥''.
On a japanese computer windows path slashes / are represented with "¥". Which leads me to believe this is an utf8 issue. Maybe there is a way I can mysqldump with some utf8 flag? Thanks for any assistance! The exported sql is here: http://goo.gl/7MPVG - Error at line 145:
edit: Problem solved:
mysql --default-character-set=utf8 db < dump.sql
Sorry if I wasted anyone's time.
mysql --default-character-set=utf8 db < dump.sql
You can add this argument --default-character-set=utf8 in the command:
mysql -u USERNAME -p my_database --default-character-set=utf8 < "C:/My Document Names Have Spaces/my_dump.sql"
I've finally found this solution and it works fine with my new installed WampServer 2.5. I've also tried to add the default character setting in my.ini, but it's useless. Until I've tried the above argument added in my DOS batch file.
My batch file execute the command, like this way:
7zip x -so %1.%2.7z|%mysql% -uxxxx -pyyyyy %2 --default-character-set=utf8
For example, the 7zip filename is 20141231.google.7z, which contains "google.sql", and the database name is google
My backup process on Linux system was a 7zip compressed. In Winodws, I used an automatic batch file to restore the same contents, so that I can share the same database in my biz trip and work offline from the server.
Hope this method is helpful to you.

Mysql ERROR at line 1153: Unknown command '\'

I am trying to import a mysqldump file via the command line, but continue to get an error. I dumped the file from my other server using:
mysqldump -u XXX -p database_name > database.sql
Then I try to import the file with:
mysql -u XXX -p database_name < database.sql
It loads a small portion and then gets stuck. The error I receive is:
ERROR at line 1153: Unknown command '\''.
I checked that line in the file with:
awk '{ if (NR==1153) print $0 }' database.sql >> line1153.sql
and it happens to be over 1MB in size, just for that line.
Any ideas what might be going on here?
You have binary blobs in your DB, try adding --hex-blob to your mysqldump statement.
You know what's going on - you have an extra single quote in your SQL!O
If you have 'awk', you probably have 'vi', which will open your line1153.sql file with ease and allow you to find the value in your database that is causing the problem.
Or... The line is probably large because it contains multiple rows. You could also use the --skip-extended-insert option to mysqldump so that each row got a separate insert statement.
Good luck.
I had the same problem because I had Chinese characters in my datasbase. Below is what I found from some Chinese forum and it worked for me.
mysql -u[USERNAME] -p[PASSWORD] --default-character-set=latin1
[DATABASE_NAME] < [BACKUP_SQL_FILE.sql]
I think you need to use path/to/file.sql instead of path\to\file.sql
Also, database < path/to/file.sql didn't work for me for some reason - I had to use use database; and source path/to/file.sql;.
If all else fails, use MySQLWorkbench to do the import. This solved the same problem for me.
I recently had a similar problem where I had done an sql dump on a Windows machine and tried to install it on a Linux machine. I had a fairly large SQL file and my error was happening at line 3455360. I used the following command to copy all text up to the point where I was getting an error:
sed -n '1, 3455359p' < sourcefile.sql > destinationfile.sql
This copied all the good code into a destination file. I looked at the last few lines of the destination file and saw that it was a complete SQL command (The last line ended with a ';') so I imported the good code and didn't get any errors.
I then looked at the rest of the file which was about 20 lines. It turns out that the export might not have completed b/c I saw the following php code at the end of the code:
Array
(
[type] => 1
[message] => Maximum execution time of 300 seconds exceeded
[file] => C:\xampp\htdocs\openemr\phpmyadmin\libraries\Util.class.php
[line] => 296
)
I removed the offending php code and imported the rest of the database.
I had special character in table names , like _\ and it give error when try to import that tables.
i fixed it by changing \ to \\ in dumped sql.
my table names where like rate_\ and i used this command to repair dump :
sed 's._\\._\\\\.g' dump.sql > dump2.sql
i didn't replace all backslashes , because i was not sure if there is some backslash somewhere in database that should not be replaces.
special characters in table name will be converted to # at sign in file name.
read http://dev.mysql.com/doc/refman/5.5/en/identifier-mapping.html
I have same error as,
Unknown command '\▒'.
when I ran this
mysql -u root -p trainee < /xx/yy.gz
So I'd followed these answers. But I did not got the restored db trainee. Then found that
yy.gz is zip file. So I restoring after unzip the file as:
mysql -u root -p trainee < /xx/yy.sql