I need to get the result of mysql query execution results saved into a txt file.
The file contains over 600000 lines of insert statements.
The command that I used is:
mysql -h10.100.109.123 -f -uUserName -pPassword
--database=databaseName < AFC.sql > abc.txt
Abc.txt gets created but there is no data in it. From console, I can see there are many errors. -f is used so that it wont stop on error.
Have gone through and tried out many suggestions of similar question. But none of them worked.
MySql version 5.7
I have a .sql file 25 sql statements, that runs ok when all statements are copypasted into mysql client It takes app 20 mins to finish and no warnings are displayed. However, if I call this file through batch file on windows (I would like to add this to task scheduler) - - the job does not get finished. I can see which statement is the problem - however - no warnings get displayed - I can only see in (show processes), that mysql process end and no subsequent commands from the file get executed - this happens somewhere in the middle on my initial sql file. Again - the statement get executed ok when copypasted into mysql console. If anyone has any idea - would appreciate a lot..
c:
cd\program files\mysql\mysql server 5.7\bin
mysql --show-warnings -h12.24.56.78 -u user -ppass --port=3306 --default-character-set=utf8 < "C:\SVN\dis\777e\777a_777e\777a_ua_into_777e.sql" >> "C:\Users\user1\db_transfers\777a_ua\777a_ua_into_777e_warnings.log" 2>&1
I have the following batch code, the code executes fine when I double click on the batch but when I try to run it through windows task scheduler, it only runs partially. I am unable to understand why.
Basically the code deletes all the rows ergo, runs until mysql -e "DELETE FROM software_it.hardware" -u root and does not execute further. Any help would be great.
#ECHO OFF
setlocal enabledelayedexpansion
mysql -e "DELETE FROM software_it.hardware" -u root
FOR %%f IN ("*.csv") DO (
set old=%%~dpnxf
set new=!old:\=\\!
mysql -e "load data local infile '"!new!"' into table software_it.hardware COLUMNS TERMINATED BY ',' IGNORE 1 ROWS" -u root
echo %%~nxf DONE
)
When run from task scheduler it is likely that the 'current directory' will be different from that used when you run the script directly. I would therefore suggest adding the following third line:
IF /I "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"
I'm running MySQL queries from within Windows command prompt by utilizing a batch file.
The file contains:
#echo off
cd "c:\Program Files\MySQL\MySQL Server 5.6\bin"
echo Please type the root password
set /p password=
mysql -uroot -p%password% < C:\Temp\GenerateDB.sql
echo Please wait while script is executing and when finished successfully
Pause
mysql -uroot -p%password% < C:\Temp\UpgradeSchema.sql
echo Please wait while script is executing and when finished successfully
Pause
Exit
My only problem is that I cannot know if the first query (GenerateDB.sql) is complete or not in order to proceed to the next one(UpgradeSchema.sql).
Is there a way to get a prompt when it is complete or even better track real time progress as when you run the script from MySQL Command Client?
Many thanks in advance
I have several sql files and I want to import all of them at once into a MySQL database.
I go to PHPMyAdmin, access the database, click import, select a file and import it. When I have more than a couple of files it takes a long time.
I would like to know if there is a better way to import multiple files, something like one file which will import the other files or similar.
I'm using WAMP and I would like a solution that does not require installing additional programs on my computer.
In Windows, open a terminal, go to the content folder and write:
copy /b *.sql all_files.sql
This concate all files in only one, making it really quick to import with PhpMyAdmin.
In Linux and macOS, as #BlackCharly pointed out, this will do the trick:
cat *.sql > .all_files.sql
Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.
A) Put the result in a separate directory to be safe (Thanks #mosh):
mkdir concatSql
cat *.sql > ./concatSql/all_files.sql
B) Concat them in a file with a different extension and then change it the name. (Thanks #William Turrell)
cat *.sql > all_files.sql1
mv all_files.sql1 all_files.sql
This is the easiest way that I have found.
In Windows (powershell):
cat *.sql | C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe -u user -p database
You will need to insert the path to your WAMP - MySQL above, I have used my systems path.
In Linux (Bash):
cat *.sql | mysql -u user -p database
Goto cmd
Type in command prompt
C:\users\Usersname>cd [.sql tables folder path ]
Press Enter
Ex: C:\users\Usersname>cd E:\project\database
Type command prompt
C:\users\Usersname>[.sql folder's drive (directory)name]
Press Enter
Ex: C:\users\Usersname>E:
Type command prompt for marge all .sql file(table) in a single file
copy /b *.sql newdatabase.sql
Press Enter
EX: E:\project\database>copy /b *.sql newdatabase.sql
You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
Ex: E:\project\database
I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.
So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.
for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f
With some assumed values (as an example):
for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f
If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).
just type:
cat *.sql |mysql -uroot -p
and mysql will import all the sql file in sequence
Enter the mysql shell like this.
mysql --host=localhost --user=username --password --database=db
Then use the source command and a semicolon to seperate the commands.
source file1.sql; source file2; source file3;
You could also a for loop to do so:
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql your_db_name < $i
wait
done
Source
Save this file as .bat and run it , change variables inside parenthesis ...
#echo off
title Mysql Import Script
cd (Folder Name)
for %%a in (*) do (
echo Importing File : %%a
mysql -u(username) -p(password) %%~na < %%a
)
pause
if it's only one database modify (%%~na) with the database name .
The easiest solution is to copy/paste every sql files in one.
You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).
in windows open windows powershell and go to the folder where sql files are then run this command
cat *.sql | C:\xampp\mysql\bin\mysql.exe -u username -p databasename
Just type below command on your command prompt & it will bind all sql file into single sql file,
c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;
Import From multiple SQL files into one Database.
Step 1: Goto to the folder and create file 'import-script.sh' with execute permission
(give Permission to file is chmod u+x import-script.sh )
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql -u USERNAME -pPASSWORD DBNAME < $i
wait
done
The main thing is -p and PASSWORD didn't add any space.
Step 2: then in your terminal run this command ./import-script.sh
for windows users,
You can select the database in the phpMyadmin interface on the left, drag and drop all your files from your windows folder onto the web UI of phpMyadmin.