batch file to run mysql - mysql

I wrote a sql script to create a clean database (one.sql), I did this on CMD and it works,
cd MYSQL\path1\bin
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
it gives me the test.log file and database on the sql server was updated
And I tried to do the same thing using batch file,
#echo off
c:
cd "MYSQL\path1\bin"
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
pause
from the CMD prompt, it seems working, but no test.log file generated, and the new database in my sql server was not created.
I ran bat file by double clicking it in its folder.
Any suggestions?
Thank you

Related

How do i restore a MYSQL .dump file?

I have file.sql, and I want to restore it. I found this command:
mysql -u username -p database_name < file.sql
Where should I put 'file.sql' in file system? I'm using Windows and XAMPP. Thank you.
*) The dump file is 3GB++
you should navigate to dump file location and run mysql -u username -p database_name < file.sql
for example if your file is at c drive you can open terminal type cd \ (this will go to root folder if your windows is installed on c dirve) and run the above command

MySQL Copy a .sql file into a database from a ssh connection

I want to copy a database from one server to another (Germany and China), since they are both hosted servers by two different service providers I can't just replicate them because they won't allow me to change the configfiles of the servers. I came to the conclusion that I need to setup a 3rd Server at my location from which I then can copy the data towards the other server so I have a "Master" in Germany a "Slave" in China and a "Messenger" at my location. All the Commands must be executed on the "messenger" by a bash script. The script works fine until the file should be sent into the database there it gives me the error that the file doesn't exist, but it exists.
mysqldump -h [host] -u [user] -p[mysqlpassword] databasename > filename.sql
sshpass -p [mypassword] ssh [ChineseServerIp] -l [user] sshpass -p [mypassword] scp user#GermanServerIP:filename.sql /home
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql
I can't just copy the file from the "messenger" Server into the Chinese one, it just would take to long because of the "great Chinese firewall" (I gzip the .sql file and then Transfer it when its on the Chinese one I unzip and upload it).
its solved, I just had to set the inserting of the sql file in ""
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] "mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql"

Run sql script using batch file from command prompt

D:
cd Tools/MySQL5/bin
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
\q
When I run the above lines in command prompt it works fine but when I save it as a batch file and run the file it connects to mysql but doesn't perform the sql scripts.
Now what I see is wrong here is that while executing the above commands one by one in your prompt, once you run mysql -u root mysql, you are in the mysql console. So your source command would work there but would not work in your batch since you are not in mysql console while running the batch file.
Solution:
What you can do for this is, instead of using source in mysql you can use
mysql dbname < filename
in your batch file in place of
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
This link can assist you further if needed
This should work
mysql -u root xyz < C:/Users/abc/Desktop/xyz.sql;
It sources the SQL commands from your file
You could write something like this
mysql -u dbUsername yourDatabase -e "SELECT * FROM table;"
Or to run repeating tasks create a runtasks.bat file, save under the root of your project then write your cmd tasks inside
mysql -u dbUser -e "DROP DATABASE IF EXISTS testDatabase;"
mysql -u dbUser -e "CREATE DATABASE testDatabase;"
php index.php migration latest #runs your migration files
cd application\tests
phpunit
This would work.
mysql.exe -u user_name -p -h _host_ _schema_ -e "select 1 from dual;"
This will also give you output on same command terminal

How to convert .sql file to tables in mysql db

I have .sql files that I'm guessing is auto-generated. I did not generate this file, but anyway it includes the database name and all tables with fields. I'd like to add the db and tables to my mysql localhost and would like to know how to do this. I have tried uploading the file but keep getting errors about my sql syntax. The syntax looks all correct to me so perhaps the .sql file needs to be changed?
From command-line:
mysql -u root -p databaseName < file.sql
Where databaseName is an already created empty database and file.sql is the .sql file you have, you must be in the same folder as the file when you run the command. This also assumes using root as the user and that it is password protected. Modify as needed for your own setup.
Additionally, you can do the reverse by flipping the angled bracket to create a database dump to a file. Like below.
mysql -u root -p databaseName > file.sql
To IMPORT:
ype the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u sat -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
OR use hostname such as mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql
If you do not know the database name or database name is included in sql dump you can try out something as follows:
$ mysql -u username -p -h 202.54.1.10 < data.sql
Refer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
If you want a GUI tool then you could probably use SQLyog or navicat for this.
You also can use a GUI tool - dbForge Studio for MySQL to execute the script. The SQL syntax check option will highlight syntax errors automatically, on file opening.

MySQL Dump to Directory

I'm currently trying to make a copy of a site to run locally, and i'm having some difficulty in downloading a dump of the mySQL database using PhpMyAdmin. (In that it doesn't work). I was wondering (and I suspect) if there is an sql command I can execute that would dump out the database to a location that I can download it via FTP.
I've found the following:
mysqldump --opt --user=root --password password > /home/backup/db.sql
but the page says that this will only run via shell, and I don't have shel access to the server. Can anyone help?
mysqldump -u root -p -T/path/ database
Simple Three steps Export mysql DB
**step1 :** run windows command prompt
**step2 :** make where yours wamp mysql stored directory ex E:\wamp\bin\mysql\mysql5.5.24\bin
**step 3 :**
E:\wamp\bin\mysql\mysql5.5.24\bin> mysqldump -u UserName -p PassWord dbname >c:\dbname.sql
PERFORM MySQL DATABASE DUMP:
Having registered mysql.exe in the Paths of Environment Variables (On
Windows Os)
Open windows console
Type the following Query:
mysqldump [Your database Name] -u root -p > C:/[Your Destination Directory ]/[ new Backup Database Name.sql ]
You will be prompted for a database user password
Enter Password:*******
when the dump is over...the directory will go back to the current
user session in Windows console
For this to work well, replace all parameters in square braces "[ ]" with your own values without the square braces
Open the Command prompt from MySQL bin folder (Make sure you have Read/Write Access)
Use the below mysqldump command
mysqldump -h [hostname] -u [username] -p [your database] > [your destination path][your filename.extn]
Ex: mysqldump -h localhost -u root -p test_DB > D:\MySQL\Dumps\test_file.sql
***WARNING : In case, if your custom path has any spaces in between, the above query will throw an error.
Invalid Path Description
To avoid Invalid Path Description, Give your path in double-quotes.
Ex : mysqldump -h localhost -u root -p test_DB > D:\MySQL\"Dumps from ABC"\test_file.sql
What are you looking for is Sypex Dumper 2. It exports (backups) your database directly to disk, so you can download the backup via ftp. It is written on php, supports large databases and has a very nice interface.
You can go to the directory where you want store the file, then execute de mysqldump command.
~/Desktop$ sudo /Applications/XAMPP/xamppfiles/bin/mysqldump -u root -p publicaciones > publicaciones.sql
PD. Use sudo to grant full permissions.
PD. If you have your PATH setup, don`t need use the full route to your MySql commands.