Loading Data into Mysql database - mysql

I have a text file which i would want to load into mysql database on Ubuntu server 12.04 LTS. I have entered the data into the file trey.txt and i moved the file to /tmp directory. When i move to the db, and enter the command
LOAD DATA
INFILE '/tmp/trey.txt'
into table arp_table
columns terminated by '|';
the output is
ERROR 13(HY000):Can't get stat of '/tmp/trey.txt'(Errcode: 2)
How should i modify to enter these details. And can i run this from command line as a cron job.

Put your data file in root folder after that run the command
$ sudo mysql -u root -p <database name>
mysql> LOAD DATA LOCAL INFILE '/path/trey.txt' INTO TABLE pet;

Related

How to migrate data from an Informix database to MySQL

We have to migrate data from an Informix database to a MySQL database.
Help me how we can achieve it?
What steps or commands I have to use it? If you have any document or reference link that would be very much helpful.
Step1: login to Informix and run the unload command. It will create a backup file named "file1" at current location under export directory.
UNLOAD to 'export/file1' SELECT * FROM db1.table1
Step2: Create table1 in MySQL Database.
Step3: Login to MySQL Database and set the global variable.
mysql> SET GLOBAL local_infile=1;
mysql> quit
Step4: Login to MySQL Database using below command
mysql --local-infile=1 -u root -p
Step5: Load the data into MySQL Databse using LOAD DATA command
LOAD DATA LOCAL INFILE '/export/file1'
INTO TABLE table1
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n';

Uploading data from csv to MYSQL Server using MYSQLSH

I need to upload data from CSV to my MYSQL Server, I've used mysqlsh to do it using jobs:
"C:\Program Files (x86)\MySQL\MySQL Shell\bin\mysqlsh.exe" --sql -h x.x.x.x -u user -password -D database -e "LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE table FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 ROWS (field1, field2)
But when i execute the command i got this error:
The used command is not allowed with this MySQL version
I readed that i need to set local_infile to TRUE, i've made and i can't do it
What I'm doing wrong?
You need to enable local_infile option on client side, as well. The only way to do that in MySQL Shell is to pass that option as connection option.
mysqlsh.exe mysql://user#x.x.x.x/database?local-infile=1 -e "LOAD DATA..."
You can get more information about connection options by calling mysqlsh -i -e "\? connection".
If you want to load big input CSV file, you can use MySQL Shell's Parallel data import.

Using mysql command line to generate CSV, can't generate it in any other directory except /tmp

I am creating csv and mysql dumps via mysql command line.
For creating mysql file I can easily created the .sql dump in my required directory
mysqldump -u"root" -p"root" dns packet --where="server_id=1 > /var/www/mydatafile/SQLData.sql
that works all okay but in case of CSV, it only creates the files in TMP folder, it can't create files in any other location
mysql -u"root" -p"" dns -e "SELECT * INTO OUTFILE '/var/www/mydatafile/my_csv.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' FROM TABLENAME";
it says
ERROR 1 (HY000) at line 1: Can't create/write to file '/var/www/mydatafile/my_csv.csv' (Errcode: 13)
I have given permission to the www directory but still it gives the same error...May I know the reason behind not creating the CSV into anyother location while SQL can be generated easily..
your directory /var/www/mydatafile/ has to be writable by the mysql user (usually mysql). You can check which user in file my.cnf (in debian/ubuntu based, located in /etc/mysql/ ).
The first command works because you generate sql instruction to stdout and redirect the output to a file, so that use the current user environment.
The second command is internal to mysql, so correct permissions are required for the mysql user.
EDIT: you can alternatively use mysqldump to generate csv with a command like this:
mysqldump -u"root" -p"root" dns packet -p -t --fields-terminated-by=, --lines-terminated-by="\r\n"

How many ways of importing data into mysql

I have a page in my website which is used for insertion of properties by users which has 54 boxes.
I don't want these information should go directly to my database cause it make it heavy if there will be 200 record per day.
The way i want is to collect the data from users and confirm it, after confirmation i should be able to imported.
May i know how many ways are there for importing data into mysql ?
How many ways of importing data into mysql:
It should be as simple as...
LOAD DATA INFILE '/tmp/mydata.txt' INTO TABLE PerformanceReport;
By default LOAD DATA INFILE uses tab delimited, one row per line, so should take it in just fine
IMPORT
1.Make sure the database you need has already been created. If it has not, please first create the database:
How do I create a database?
CAUTION:
If you import a backup file to a database that already has content, it will replace the existing content.
Use FTP to upload your SQL file to your server. You can upload it to
your default FTP directory. Or, see Step 1 in the "Export"
instructions above for another suggestion. Alternately, you can use
scp to upload your file via SSH.
Log into your server via SSH.
Use the command cd to navigate into the directory where you uploaded
your backup file in Step 1. If you uploaded the backup to your data
directory, go here (replace 00000 with your site number):
cd /home/00000/data/
Import the database by executing the following command:
`mysql -h internal-db.s00000.gridserver.com -u username -p dbname < dbname.sql`
OR:
`mysql -h internal-db.s00000.gridserver.com -u username -p dbname -e 'source dbname.sql'`
Once you execute this command, you will be prompted for your
database password. Type it in and hit enter. Your database will now
import. It may take a few minutes if you have a large database. When
the import is done, you will be returned to the command prompt.
NOTE:
Variables are the same as in Step 3 from the Export section above.
Please check Step 3 in the "Export" section to make sure you are
correctly replacing the example code with your own information.
dbname.sql is the actual name of your SQL file.
If you have a gzipped backup of your database, you can use this line instead:
`gunzip < dbname.gz | mysql -h internal-db.s00000.gridserver.com -u username -p dbname`
You can enter in your own username, database name, and backup file
name, as before. dbname.gz is the name of your gzipped backup file.
Use "unzip" instead of "gunzip" for zipped files.
Remove the SQL file from your web-accessible directory, if you
uploaded it to a public folder. Otherwise, anyone can download it
from the web.
If you get an error that looks like this:
Got Error: 1045: Access denied for user 'db00000#internal-db.s00000.gridserver.com' (using password: YES) when trying to connect
You have entered an incorrect password. Please retype it carefully,
or reset your password via the AccountCenter Control Panel. See
Database users on the Grid for instructions.
If you get an SQL error during the import, you can force it to finish by adding "-f" to the command, which stands for "force." For example:
`mysql -f -h internal-db.s00000.gridserver.com -u username -p dbname -e 'source dbname.sql'`
This can help you finish an import if you have a few corrupt tables,
but need to get the database as a whole imported before you do
anything else.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
https://dev.mysql.com/doc/refman/5.0/en/loading-tables.html
https://www.mysql.com/why-mysql/windows/excel/import/
http://www.itworld.com/it-management/359857/3-ways-import-and-export-mysql-database
$file = '/pathtocsviportdatabase/csv/importtabledata.csv';
$import = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE `imports` FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(AllCOLUMN SEparated by ',');";
mysql_query($import) or die(mysql_error());

Copying mysql databases from one computer to another

I want to copy my mysql database from my computer to another computer. How can I do this?
How to copy Mysql database from one Computer to another / backup database using mysqldump
We can transfer a MySQL database from one PC to another PC using
mysqldump command.
We have to create dump file of database to transfer database from
one PC to another PC.
MySQL database is not portable database i.e. we cannot transfer it
from one PC to another PC by copying and pasting it.
We can use following method to transfer database.
Creating a dumpfile from database/ Taking backup of MySQL database:
Open command prompt.
Execute following commands to change directory
>c: “press enter”
>cd program files/MySQL/MySQL Server 5.1/ bin “press enter”
>mysqldump -u root -p database_name > database_name.sql “press enter”
Enter password: password of MySQL
Copy sql file and paste it in PC where you want to transfer database.
2. Dumping sql file into database:-
- Open MySQL command line client command prompt.
- Execute following command to create database.
create database database_name;
“press enter” Database name is must as that of your database_name.
Copy that sql file into location “c:/program files/MySQL/MySQL Server 5.1/bin”
*- Now open command prompt and execute following commands.*
>C: “press enter”
>cd program files/MySQL/MySQL Server5.1/bin “press enter”
>mysql –u root –p database_name < database_name.sql “press enter”
Your database is created on PC.
Now in MySQL command prompt check your database.
Another one:1
This best and the easy way is to use a db tools(SQLyog)
http://www.webyog.com/product/downloads
With this tools you can connect the 2 databases servers and just copy one database on server a to server b.
For more info
http://faq.webyog.com/content/12/32/en/mysql-5-objects-are-greyed-out-in-copy-db-to-other-host-dialogue.html
Another one:2
For a database named "lbry", try this:
mysqldump -u root -p lbry > dump-lbry.sql
Create a database of the same name ("lbry" in this example) on the computer to which you wish to copy the database contents
Then import it:
mysql -u root -p lbry < dump-lbry.sql
You can do by this process step-by-step using MySQL WorkBench.
Install MySQL Workbench
Connect to existing Database
Go to Navigator -> Management -> Data Export.
(this will dump queries of tables one by one in a separate folder, Workbench uses the same folder to import)
Create Database on target PC.
Connect to Target Database (would consist of 0 tables in DB)
Go to Navigator -> Management -> Data Import/Restore.
(this will use the dump folder and create tables in your target Database).
Hope this helps.
The only SAFE way to copy databases from one machine to another is to first quiesce the database (make sure no clients are modifying it), then use the mysqldump command to create a text representation of your schema and the contents of your tables. Then copy that text file over to the other machine and read it in by specifying it as the input to the mysql command.
Attempting to copy the actual mysql data directories over is asking for trouble, since they are dependent on the architecture of the machine that mysql is running on and likely on the version of mysql and whatever storage engine is in use.
This tutorial is in Ubuntu but will work on Redhat, Centos, Fedora, Suse
We can dump database, transfer it to another server, and restore it
It will show how to take care of things like modified credentials as a result and moving debain.cnf file
4 dump restore will slow down the serverHow it works
4.1 Run mysqldump on source server:this builds a MySQL executable script for the destination server.
During this time the MySQL server will queue queries
4.2 Copy dump file to the destination server
4.3 Empty destination server
4.4 Execute dump file on the destintion server
Server A(Source Server)
Server B (Destination Server)
Case 1:Server A
root#source$ mysql --defaults-file=/etc/mysql/debain.cnf
mysql>show databases;
mysql>use testdb;(The database to dump)
mysql>show tables;(To Check the tables)
mysql>^c
-- now dump the databses
root#surce$ mysql --defaults-file=/etc/mysql/debain.cnf --all-databses | gzip -c > dump.sql.gz
root#surce$ gzip -dc dump.sql.gz
To copy the files create a ssh key on the source server
root#surce$ ssh-keygen
root#surce$ cat /root/.ssh/id_rsa.pub
select and copy all the ssh key string
root#surce$ scp dump.sql.gz ubuntu#destination:
goto destination server
last step copy the contents of debain.cnf file
root#surce$ cat /etc/mysql/debain.cnf
[client]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
select all and copy this file to detination server.
Note: The sockey path can be different in your machine .use locate command to find the exact path
Case 2. Server B
drop all databses
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}'
if this command doesnot drop databses use it with -force option
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}' | mysql --defaults-file=/etc/mysql/debian.cnf -f
copy the ssh key on the destination server
root#destination$ echo "paste the key here" >> /home/ubuntu/.ssh/authorised_keys
goto source Server and use scp command to move the dump on the destination server
(inject the file)
root#destination$ gzip -dc /home/ubuntu/dump.sql.gz | mysql --defaults-file=/etc/mysql/debain.cnf
root#destination$ > /etc/mysql/debain.cnf
root#destination$ nano /etc/mysql/debain.cnf
paste the contents of .cnf file from source server here and save the file
:x
root#destination$ mysql --defaults-file= /etc/mysql/debain.cnf
if you get the mysql prompt then everything should be working file
mysql>
I was able to restore a backup that was shared with me following this thread, specifically #jmail's answer, but, I thought that I could provide a bit more concise answer for future users. I received a dump file with a .sql extension, not a .dump extension as I would have expected.
I tried to place it in my project folder and restore it but I got error 22, referring to access privileges. I moved it to “c:/program files/MySQL/MySQL Server 5.1/bin” and then ran it by:
1) Starting MySQL in the command prompt.
2) Creating the new database that I wanted to restore to
3) Switching to the database
USE new_DB;
4) Running
source c:/program files/MySQL/MySQL Server 5.1/bin/backup.sql
I'm not sure how the backup.sql file was created but this worked for restoring it on my Windows 10 system.
mysqldump --databases dbname -hsource_server_ip -usource_server_userName -psource_server_passcode | mysql
-udest_server_user_name -pdest_server_user_passcode &
There are three general ways to invoke mysqldump:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
If you do not name any tables following db_name or if you use the --databases or --all-databases option, entire databases are dumped.
mysqldump does not dump the INFORMATION_SCHEMA database by default. MariaDB dumps the INFORMATION_SCHEMA if you name it explicitly on the command line, although currently you must also use the --skip-lock-tables option.
To see a list of the options your version of mysqldump supports, execute mysqldump --help.
I just summarize jmail's answer:
   Database to SQL file at computer 1:
   mysqldump --user <user name> --password <database> > <output file> for example mysqldump --user root --password movie > movie.sql
   SQL file to database at computer 2:
   mysql --user <user name> --password <database> < <output file> for example mysql --user root --password movie < movie.sql