how to write a bat file to execute the .sql file - mysql

I want to execute a query inside a db.sql file using MySQL 5.1 in the Windows environment. Can any one help me with this? I want to do this by running a .bat file.

Col Shrapnel's answer is the way to do it. The batch file would look like this:
mysql < %1
...which really just shows that it'd just be easier to use that directly instead of a .bat.
runsql myfile.sql // (assuming you name your batch file "runsql.bat")
// vs
mysql < myfile.sql
Reading your comment on the other answer, it seems like you might actually get some use out of the batch file as long as you don't mind storing your password in plain text on your computer.
Make the batch file like this:
mysql -u myusername -pmypassword < %1
Note that there is no space between -p and mypassword. If you use that above, every script would have to specify which database to use, which might be a hassle. The other way you could do it is like this:
mysql -u myusername -pmypassword -D %1 < %2
And the usage would be:
mysql database_name input.sql

mysql.exe < db.sql

Related

mysql 5.7.19 dumping data does not work(input)

I am trying input datadump to mysql.5.7.19.
I command like below :
mysql -uroot -p temp < temp201708.sql
my my.cnf prints error to /var/log/mysqld.log, but I do not see any error log while dumping.
However, when jobs done, I can not find any tables or data in temp schema. I think I saw some articles that different mysql versions could be a problem. Is it right?
Currently, I do not know what point is wrong, since there were no error log. What should I look for to solve this problem?
Thanks.
FYI, I do not know what version of mysql which made that dump file, I just received from the client.
mysqldump -u root -p DatabaseName > /Path-To-put-The-File/fileName.sql
When doing a dump in mysql:
run cmd
locate location of your mysql server like
Enter:
cd C:\Program Files\MySQL\MySQL Server 5.6\bin
mysqldump -uUsernameHere -pYourPasswordHere dbnamehere > "D:\sample.sql" make sure you have a drive D: in your computer or you can change it.
This is working and tested.
You can use it by calling a .bat file to execute the command.
Otherwise if you want to store a data into your Database use mysql only and not mysqldump and change > to <
like this:
mysql -uUsernameHere -pYourPasswordHere dbnamehere < "D:\sample.sql"
Thanks for answers.
I found the cause.
In the dump file, there are some states like USE mysql;, USE temp;.
So even though I indicate which schema to put in in command line,
it just ignore the command line and follow the script.
I found all of my data in mysql schema.
==========================
Conclusion
if you dump with some command like below
$ > mysqldump -u[user] -p[passwd] [scheme] > dump.sql
it is ok with
$ > mysql -u[user] -p[passwd] [target_scheme] < dump.sql
If not, you should check the contents of .sql file.

Why does MySQLDump write data to the terminal when importing/restoring a file

I'm trying to restore/import a MySQLdump file using the following command (on Windows, version 5.x.x):
mysqldump -u root -p --all-databases < myfile.sql
However, in the middle of processing, the command prompt window starts beeping like crazy. Is it possible to remove the output of the mysqldump command (which I'm assuming is what is causing this sound to occur)? The file in question is slightly more than 200MB.
I've looked at the flags and only seems to be --verbose that would help me. However, it seems that it wouldn't be able to stop the output in the command prompt window (but add more output?).
Edit #1
One answer, that has been removed, suggested piping the output /dev/null. I'm on Windows and I have no idea what would be the equivalent. If I pipe it to an actual file, I would be in effect almost duplicating my dump to another file? Why is the restore outputting to the prompt in the first place?
I think you really intend to run
mysqldump -u root -p --all-databases > myfile.sql
(your redirection is the wrong way around)
mysqldump writes out to a file. To import from that file, you would use
mysql -u root -p < myfile.sql

Import large MySQL .sql file on Windows with Force

I would like to import a 350MB MySQL .sql file on a Windows 7 machine. I usually do this by using
mysql -uuser -p -e "source c:/path/to/file.sql" database
since < doesn't work in Powershell.
My .sql file has an error in it this time. I'd prefer to just skip the bad row and continue the import. How can I force the import to continue on Windows?
On a unix/linux based system, I could use
mysql --force ... < file.sql
but --force doesn't seem to work with the -e "source ..." command (understandably so).
Thanks,
Mike
You're probably going to have to have Powershell execute this in the standard console in order to use < properly. Technically you could use get-content and pipe the output to mysql, but I've always found that to be slow, and it somehow still keeps the file contents in memory of the Powershell session.
This is how I would execute it from the Powershell prompt (changed file path to include spaces to demonstrate inner quotes, just in case):
cmd /C 'mysql -uuser -p --force < "C:\path\with spaces\to\file.sql"'
[GC]::collect() would apparently clear it up the memory, but you can't do that until after it's done anyway. When it comes to mysql and mysqldump, I don't bother with Powershell. The default encoding used in > is Unicode, making dump files twice as big out of Powershell as out of cmd unless you remember to write | out-file dump.sql -enc ascii instead of > dump.sql.
I'd suggest to also have a look at this SO answer, that takes advantage of source SQL command:

using mysqldump to restore database to mysql located on another computer

Any idea how to do this restore ?
I looked into help of mysqldump but couldn't see it there .
If so can you give me some example.
With mysqldump you will generate a script you can use for restore on a different computer like this:
$ mysql -U user_name < your_backup.sql
Run on your favorite shell (windows command prompt, bash, csh...).
I think you can use CMD to navigate to the mysqldump location, then type this command,
mysqldump database_name -u username >location\to\save\dump.sql
change database_name to the database you want to backup, username to the username associated with the database, and location\to\save\dump.sql to the location where you want to save the output sql file, for me I wrote it D:\dump.sql
Then on the other machine you can import the SQL file using the PHPMyAdmin.
You can just execute the SQL using the mysql command-line command. There is a switch to specify which file to import, I think it is -I but I'm not sure.
It's just plain SQL. Pass the file to mysql (the mysql command line tool) and it will execute it:
mysql < backup.sql
From the shell prompt, using
parameters form the mysqldump
doc, mysqldump the database using a > redirect to a
human readable .sql file. E.g.
$ mysqldump --databases src_db > src_db.sql
Transfer the human readable file to
another machine.
After making sure the destination database exists has been created, redirect < the .sql file into the destination database.
$ mysql dest_db < src_db.sql

mysql import on windows

I have a MySQL file, db.sql. I have tried to import it using:
mysql -uroot -p[password] db < db.sql
All I get is a listing of mysql commands, or I get a syntax error. The weird thing is I used this file last week and, as far as I know, I'm doing it the same way.
I create the database, then in command line enter the above but it's not working. I've tried being inside mysql and just at command line and nothing seems to be working.
Is there something I should be doing differently in windows or MySQL5? I don't know how the heck I got it to work the first time...
TIA
Try this instead:
mysql -u root -p
(prompts for password)
use db;
source db.sql
I found out it is different to run this command from Windows Command Line (cmd.exe) and Windows PowerShell.
Using CMD.exe the command works okay, but in PowerShell I get this error:
mysql -uroot exampledb < exampledb.sql
The '<' operator is reserved for future use.
Not sure if your example was a typo or not, but for starters you need to have a space in between your flags and their values, roughly like this:
mysql -u root -p [password] db < db.sql
If you are already logged in the try this it will be very useful, but depend upon the MySQL version, it works on MySQL 5.0
For log in if you are not already logged in.
mysql>[your password]
Other wise, use the database to which you want to import the SQLDump file by command.
mysql>use [your database name]
And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")
mysql> source [dataBasePath+name.sql or dataBaseName.sql]
I've been using PHP script called "BigDump":
http://www.ozerov.de/bigdump.php
This perfectly works
mysql>[your password]
Other wise, use the database to which you want to import the SQLDump file by command.
mysql>use [your database name]
And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")
mysql> source [dataBasePath+name.sql or dataBaseName.sql]EG: source C:.....sql
I am using mysql server 5.5
In Windows PowerShell, you can pipe in the contents like so:
Get-Content db.sql | mysql -u root -p [password]