I have created a batch script which is supposed to execute the SQL file in the same directory and then pause, where I connect to MYSQL and issue the commands inside of the SQL file. For some reason when I directly paste these lines into a command window, it works fine. When I have it setup as follows, I receive an Access Denied error. Can anyone please point out what I am doing wrong?
BAT File:
cmd < script.sql
pause
Script.SQL file
#echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname
SELECT `SERVER_ID`
FROM tc_services
WHERE `GAME_ID` LIKE '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
exit
You have the files organized incorrectly. Please redo the file as follows:
BAT File:
#echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname < script.sql
pause
script.sql file
SELECT `SERVER_ID`
FROM tc_services
WHERE `GAME_ID` LIKE '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
Give it a Try !!!
You did not say what operating system you are using. It looks like you are trying to write your output file to the "\Program Files (x86)" directory. As a security measure Windows does not allow a user to write to that file area. You will need to go to the security settings of the "D:\Program Files (x86)\TCAdmin Control Panel\ScheduledTasks\GAME\" folder and make sure the user running the bat/script file has write access to that folder.
Related
I have a set of raw data files named using a pattern like a-1.txt, a-2.txt, etc. I am using the LOAD DATA LOCAL INFILE command in a MySQL script to load the raw data files into the database. That command cannot be run in a stored procedure. I'd like to avoid doing a copy/paste for loading 20 raw data files, like I described, in the MySQL script and would much rather use a LOOP to load the raw data files, but LOOP cannot be used outside of a stored procedure.
What's the best way to handle this? How do I get the MySQL script to do this?
Assuming you are using Bash, you can run the following to generate the SQL file.
rm testfile.sql
ls *.txt | xargs -I inputfile echo "LOAD DATA LOCAL INFILE 'inputfile' INTO TABLE mytable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n';" >> testfile.sql
Then to run it, you can add another line.
mysql -h localhost -u root -pXXXXXXX mydatabase < testfile.sql
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"
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());
I am using the following command to export thedabase,however i can't find the FILE.sql file after executing the command.Where is it stored?
mysqldump -u username -ppassword database_name > FILE.sql
Also how can i check my home directory , I have checked the program files(x86) mysql and respective bin folder in it.
The other way to export data to CSV file is by using "OUTFILE" syntax provided by MySQL like shown below.
[Usual MySQL Query]
INTO OUTFILE '/var/data_exports/huge_data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Read practical usage and more here. ResolveBug.com
It should be at either the root dir or in current dir where you run the dump command.
I am writing a very simple shell script to dump a table into CSV file. Here is part of it:
day=`/bin/date +'%Y-%m-%d'`
file="/tmp/table-$day.csv"
rm $file
query="SELECT * FROM table INTO OUTFILE '$file' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'"
echo "$query" | mysql <connection_parameters>
I put in rm $file to make sure that the file does not exist prior to the query's execution.
However, when I execute the script, I get conflicting messages:
rm: cannot remove `/tmp/table-2013-02-08.csv': No such file or directory
ERROR 1086 (HY000) at line 1: File '/tmp/table-2013-02-08.csv' already exists
I cannot find the OUTFILE anywhere in the machine.
So what is wrong.. ?
Thank you.
I have found the answer.
OUTFILE creates the file on the MySQL server, rather than on my MySQL client's machine.
Check if you have this in /etc/passwd
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
change shell to /bin/bash instead