Unable to load the data from file to Mysql table - mysql

I am unable to load the data from file to a MySQL table.
Where else should I keep the file?
File is present as shown below.
notroot#ubuntu:~/lab/data$ ls
txns
notroot#ubuntu:~/lab/data$ pwd
/home/notroot/lab/data
notroot#ubuntu:~/lab/data$ mysql -u root -p
mysql> load data infile '/home/notroot/lab/data/txns' into table trans fields terminated by ',' lines terminated by '\n';
ERROR 29 (HY000): File '/home/notroot/lab/data/txns' not found (Errcode: 13)
mysql> load data local infile '/home/notroot/lab/data/txns' into table trans fields terminated by ',' lines terminated by '\n';
ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql>

Use the --local-infile parameter when running mysql
mysql --local-infile -u root -p

Related

load data local infile - command not found

As part of a Linux-script, i tried to insert data from a file into a table.
If I try to use the following SQL statement I get this answer from the Server:
./load_test.sh: line 28: -h$SERVER: command not found.
What does it mean?
$MYSQL -h$SERVER -P3306 -uroot -p $DB -e "load data local infile '/var/lib/mysql/scripts/load_test/test.del' INTO TABLE TAB_SNAP_GET_DB_V97 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'"

MariaDB load data infile permission error

I am attempting to load data into a MariaDB database. The ownership of the directory loaded from was modified as follows:
$ chown -R mysql load_data
permissions for the directory appear as:
drwxrwxr-x 2 mysql beta_user 4096 Jul 20 16:33 load_data
The data file, emp_list.dat, looks as follows:
Dineen,,Tim,Frog,1965-03-17
Blatter,,Greg,Butterfly,1975-10-18
Hank,Larry,Kevin,Aligator,1980-02-03
Jones,,Tom,Singer,1945-04-04
The load command in load_employees.sql is as follows:
load data infile
'/home/beta_user/sample_code/db/mariadb/load_data/emp_list.dat'
into table pktest.employees
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n'
(last_name,middle_name,first_name,pref_name,#dob)
set dob = str_to_date(#dob, '%y-%m-%d');
When I attempt to load data the following error appears:
$ mysql --defaults-file=~/bin/msqlpsw/msqlpkpsw.dat -D pktest -e "source /home/beta_user/sample_code/db/mariadb/load_data/load_employees.sql"
ERROR 13 (HY000) at line 1 in file: '/home/beta_user/sample_code/db/mariadb/load_data/load_employees.sql':
Can't get stat of '/home/beta_user/sample_code/db/mariadb/load_data/emp_list.dat'
(Errcode: 13 "Permission denied")
Can anybody suggest why the error given that the ownership of the directory is set to mysql ?

MySQL Loading Data Error

LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2)
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4)
I have a single text file composed of several of these "LOAD DATA" commands. I receive an error message saying line 6, or the start of the 2nd command, is not proper syntax. And if I try to introduce a "lines terminated by '\n'" code, it says it is not allowed with my mysql version.
You should add a ';' at the end of each load statement.
LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2);
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4);
See also ERROR 1148: The used command is not allowed with this MySQL version
You can specify that as an additional option when setting up your client connection:
mysql -u myuser -p --local-infile somedatabase

Insert CSV to MySQL using Ubuntu Terminal via shell script

Is it possible to insert a CSV file into MySQL using a shell script in Ubuntu?
Here's what I tried :
mysql -uroot -proot mysfdb < /home/sf/data.csv
But I am given an error
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
Here's a sample content from the CSV file:
showinventory_SST312V8N4615041313_1366009574txt_,800-200002.A0,00007985
Any ideas?
Maksym Polshcha's answer is correct but is only missing a few things. Apparently, since it's a local file, I have to declare it as a local file in the mysql command. The final command should be something like this:
mysql -uroot -proot --local_infile=1 3parsfdb -e "LOAD DATA LOCAL INFILE '/logfiles/Bat_res.csv' INTO TABLE Bat_res FIELDS TERMINATED BY ','"
Also I made sure that the /logfiles directory and the Bat_res.csv are world readable.
Thank you for the great answers.
Try this:
mysql -uroot -proot mysfdb -e "LOAD DATA INFILE '/home/sf/data.csv' INTO TABLE mytable"
where mytable is your table for the data. If you have non-standard field/line separators in your CSV file use FIELDS TERMINATED BY and LINES TERMINATED BY
See http://dev.mysql.com/doc/refman/5.1/en/load-data.html
I used this and it worked.
Login in mysql using `mysql -uroot -ppassword --local-infile`
Then in terminal:
LOAD DATA LOCAL INFILE '.csv path' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
Open Ubuntu Terminal and just run the following command
# mysql -u admin -p --local_infile=1 DATABASE_NAME -e "LOAD DATA LOCAL INFILE 'students.csv' INTO TABLE TABLE_NAME FIELDS TERMINATED BY ',' enclosed by '\"'"
Here,
DATABASE_NAME = The name of your database
students.csv = The CSV file directory, that you want to upload in the database
TABLE_NAME = in which table you want to upload your data
admin = Database user name
After run this command system asked for the password of the admin user.
Write the password and Enjoy.

MySQL LOAD DATA Error can't resolve...!

mysql> LOAD DATA INFILE '/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
ERROR 13 (HY000): Can't get stat of '/abc.txt' (Errcode: 2)
I used LOAD DATA as per the syntax. But getting the above error. can anybody tell what is the error..!?
I tried searching google dev.mysql can't find what error it is??
Thanks in advance
If the file is on your MYSQL server try using the full path.
LOAD DATA INFILE '/var/tmp/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
If the file isn't on the server and it is on your local machine, and your user has access to SCP files between the servers ( try this
scp abc.txt mysqlserver:
) then you should be able to do this (using full path again) ..
LOAD DATA LOCAL INFILE '/var/tmp/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';
If that doesn't work.. try renaming the file to test1.txt and using the 'mysqlimport' command?
Login to the mysql console using the following flag:
mysql -uroot -p --local-infile
and then enter the password.
After that execute the load data command as follows:
LOAD DATA LOCAL INFILE '/abc.txt' INTO TABLE test1 FIELDS TERMINATED BY '|';