windows batch file for mysqldump command - mysql

this is first time I am upto making a windows batch file.I wish not to do experiments first as its related to live server.
I use to back up mySql database by following
open cmd
write
mysqldump -u user -p DBname >C:\DBname.sql
then cmd ask password
password = password
I want to make a windows batch file ,upon click I wish to perform the above functionality.
Can any one please give some instructions related script of .bat file
So far I wrote this
ECHO OFF
mysqldump -u user -p DBname >C:\DBname.sql
GO
SET password = password
Go
but still it prompts me for password .
I wish password get enter automatically

The above was still prompting me for a password I used whats below to get it to work correctly.
"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -uroot -pDBPass DBName > C:\DBname.sql

The one-liner:
mysqldump -u user -p DBname --password YOURPASSWORD > C:\DBname.sql
should do it.

You can use this:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -u root -proot test > D:\DBname.sql
Please note this: -proot, the p and root must be together

"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe" -u root -pmypassword --databases mydatabase --hex-blob > C:\mybackup.sql
Paste into a bat file, I couldnt get the plain command to work in task scheduler so used a BAT
You can omit the --hex-blob, its there incase you're using blob columns

Related

mysqldump prompting password even with credentials saved at /home/user/.my.cnf

My ultimate goal is to create a cron script to automatically dump a selected MySQL database (ngsRunStats_FK) once a day. Please note that the MySQL user ngs_run_stats has all privileges on MySQL.
I was expecting the answer from Franklin here would do the job, though when I run on the terminal
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats -p ngsRunStats_FK --lock-tables=false > test.sql
I am still required to prompt my password. If I enter the password correctly, the dump will work as expected. Though, as stated previously, this should be done automatically i.e. without prompting for the password.
I am assuming that if I am being asked to prompt my password when I call the command on the terminal my cron (not shown here) script will not work. Or is this assumption wrong?
My /home/user/.my.cnf looks like so:
[mysqldump]
user = ngs_run_stats
password = mypassword
and has permissions 600
Try this:
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats -pngsRunStats_FK --lock-tables=false > test.sql
there should be no space between -p flag and the input password.
However, note that this is an insecure way of connecting to your database as the password will be in plaintext within the command and can be visible through the logs.
The mistake is that I am passing the -p flag, which will prompt the password. I misread the information from this website. So I should omit the -p flag and the database is actually specified by the --databases flag
mysqldump --defaults-extra-file=/home/user/.my.cnf -S /nexus/cliniphenome/mysql.sock -u ngs_run_stats --databases ngsRunStats_FK --lock-tables=false > test4.sql

How to enter password in mysql5.5 using batch script?

i have written a batch script but it still ask me for a password. i want to enter it automatically. please help me
here is my batch script :
setlocal EnableDelayedExpansion
c:
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysql -u root -p root
but still in output it ask for a password as:
Enter Password :
i got the answer. for that find below my comment
You can't have a space between the option and the password. So it should be:
mysql -u root -proot
Or use --password=root
For other googlers like me, I'll add my solution.
Unfortunately, official documentation doesn't tell this clearly
Short parameters like these didn't work to me and prompted for password
mysqldump -uroot -p "qwerty" mybase > Z:\mybase.sql
With "full" name parameters it worked, just warning about this action as insecure
mysqldump --user="root" --password="qwerty" mybase > Z:\mybase.sql
echo 'Backup OK' > mysql_dump.log
Problem Solved Got the solution
c:
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysql -uroot -proot -e "delete from db.tablename where columnname =
'something';
The problem is In this line "setlocal EnableDelayedExpansion"
This should be deleted. and the code runs smoothly :)
AFAIK you cannot do this using the mysql bin, but mysqladmin can.
See the docs here: http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html
-ppassword displays warning
Warning: Using a password on the command line interface can be insecure.
https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
Solution: You can use cnf options file
https://dev.mysql.com/doc/refman/5.7/en/option-files.html
linux:
echo -e "[client]\nhost=mysqlhost\nport=3306\nuser=root\npassword=${ROOT_PASS}" > root.cnf
mysql --defauls-file=root.cnf -e "SELECT * FROM users;" ${MYDB}
windows:
Ship root.cnf file with batch script and run
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql" --defauls-file=root.cnf -e "SELECT * FROM users;" %MYDB%
try this create a file for password .pw and below command works in batch fine.
mysql --default-file=C:\path*.pw -u %username% blah blah..
Your best option is to use MySQL options files.
MySQL 5.5 makes use of the .my.cnf file, you just need to supply your username and password. This approach means you can login to mysql just by typing mysql in your script and the .my.cnf file will fill in the rest; Here's how:
Create a .my.cnf file in the home directory of the user which will run the script. The contents should look like the following code block, swapping out your_user and your_password for their actual values.
[client]
user=your_user
password=your_password
It's prudent to change the permission of this file to either 400 or 600, this will mean only your user and root will be able to see the file.
chmod 600 .my.cnf
You're all set!
N.B.
From version 5.6 onwards you can use the mysql_config_editor utility, which generates a .mylogin.cnf, it has the same result as the above apporoach with the added benefit that the password will be encrypted by the utility.
Links
MySQL 5.5
https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html
MySQL 5.6+
https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html
This worked for me, put it in a .bat file, and change the password with yours.
#echo OFF
setlocal EnableDelayedExpansion
C:
cd "C:\xampp\mysql\bin\"
mysql.exe --user="root" --password="the password"

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.

How to import an SQL file by using mysqldump on Windows through command line

How do I import an SQL file to MySQL dump using command line. I am using windows.
Navigate to the directory where you have mysql and issue this command, changing the bold values to your file/database locations.
c:\mysql\bin\> mysql -u USERNAME -p PASSWORD database_name < filename.sql
Simple steps to import sql file.
Click on WampServer and then go to MySQL=> MySQL console
enter password \If no password click simply enter
create database database_name; \If database already there skip this step
use database_name;
source D:\backup.sql \Path of database file
To dump all databases, use the -all-databases option. With that option, no database needs to be specified.
mysqldump -u username -ppassword –all-databases > dump.sql
Try like this:
I think you need to use the full path at the command line, something like this, perhaps:
C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql
Refer this link also:
http://www.ryantetek.com/2011/09/importing-large-sql-files-through-command-line-when-using-phpmyadminxampp/
Try this
C:\Program Files\Mysql\Mysql server 5.6\bin\mysql -u {username} -p {password} {your database name} < file-name.sql
Note here file-name.sql is in mysql server 5.6\bin

How can I import a database with MySQL from terminal?

How can I import a database with mysql from terminal?
I cannot find the exact syntax.
Assuming you're on a Linux or Windows console:
Prompt for password:
mysql -u <username> -p <databasename> < <filename.sql>
Enter password directly (not secure):
mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
Example:
mysql -u root -p wp_users < wp_users.sql
mysql -u root -pPassword123 wp_users < wp_users.sql
See also:
4.5.1.5. Executing SQL Statements from a Text File
Note: If you are on windows then you will have to cd (change directory) to your MySQL/bin directory inside the CMD before executing the command.
Preferable way for windows:
Open the console and start the interactive MySQL mode
use <name_of_your_database>;
source <path_of_your_.sql>
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
-u - for Username
-p - to prompt the Password
Eg. mysql -u root -p mydb < /home/db_backup.sql
You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.
Directly from var/www/html
mysql -u username -p database_name < /path/to/file.sql
From within mysql:
mysql> use db_name;
mysql> source backup-file.sql
Open Terminal Then
mysql -u root -p
eg:- mysql -u shabeer -p
After That Create a Database
mysql> create database "Name";
eg:- create database INVESTOR;
Then Select That New Database "INVESTOR"
mysql> USE INVESTOR;
Select the path of sql file from machine
mysql> source /home/shabeer/Desktop/new_file.sql;
Then press enter and wait for some times if it's all executed then
mysql> exit
From Terminal:
mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
in the terminal type
mysql -uroot -p1234; use databasename; source /path/filename.sql
Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.
Export SQL file:
$ mysqldump -u [user_name] -p [database_name] > [database_name.sql]
Example : mysqldump -u root -p max_development > max_development.sql
Import SQL file:
$ mysqldump -u [user_name] -p [database_name] < [file_name.sql]
Example: mysqldump -u root -p max_production < max_development.sql
Note SQL file should exist same directory
I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.
for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password> <database name> < $anyvar; done
Works well on OSX shell.
Explanation:
First create a database or use an existing database. In my case, I am using an existing database
Load the database by giving <name of database> = ClassicModels in my case and using the operator < give the path to the database = sakila-data.sql
By running show tables, I get the list of tables as you can see.
Note : In my case I got an error 1062, because I am trying to load the same thing again.
mysql -u username -ppassword dbname < /path/file-name.sql
example
mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
Use this from terminal
After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/
Connect to Mysql (let's use root for both username and password):
mysql -uroot -proot
Connect to the database (let's say it is called emptyDatabase (your should get a confirmation message):
connect emptyDatabase
3 Import the source code, lets say the file is called mySource.sql and it is in a folder called mySoureDb under the profile of a user called myUser:
source /Users/myUser/mySourceDB/mySource.sql
Open the MySQL Command Line Client and type in your password
Change to the database you want to use for importing the .sql file data into. Do this by typing:
USE your_database_name
Now locate the .sql file you want to execute.
If the file is located in the main local C: drive directory and the .sql script file name is currentSqlTable.sql, you would type the following:
\. C:\currentSqlTable.sql
and press Enter to execute the SQL script file.
If you are using sakila-db from mysql website,
It's very easy on the Linux platform just follow the below-mentioned steps, After downloading the zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and the other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory.
First connect to mysql via command line
mysql -u root -p
Enter MySQL PW
Select target DB name
use <db_name>
Select your db file for import
SET autocommit=0; source /root/<db_file>;
commit;
This should do it. (thanks for clearing)
This will work even 10GB DB can be imported successfully this way. :)
In Ubuntu, from MySQL monitor, you have already used this syntax:
mysql> use <dbname>
-> The USE statement tells MySQL to use dbname as the default database for subsequent statements
mysql> source <file-path>
for example:
mysql> use phonebook;
mysql> source /tmp/phonebook.sql;
Important: make sure the sql file is in a directory that mysql can access to like /tmp
If you want to import a database from a SQL dump which might have "use" statements in it, I recommend to use the "-o" option as a safeguard to not accidentially import to a wrong database.
• --one-database, -o
Ignore statements except those those that occur while the default
database is the one named on the command line. This filtering is
limited, and based only on USE statements. This is useful for
skipping updates to other databases in the binary log.
Full command:
mysql -u <username> -p -o <databasename> < <filename.sql>
For Ubuntu/Linux users,
Extract the SQL file and paste it somewhere
e.g you pasted on desktop
open the terminal
go to your database and create a database name
Create database db_name;
Exit Mysql from your terminal
cd DESKTOP
mysql -u root -p db_name < /cd/to/mysql.sql
Enter the password:....
Before running the commands on the terminal you have to make sure that you have MySQL installed on your terminal.
You can use the following command to install it:
sudo apt-get update
sudo apt-get install mysql-server
Refrence here.
After that you can use the following commands to import a database:
mysql -u <username> -p <databasename> < <filename.sql>
The simplest way to import a database in your MYSQL from the terminal is done by the below-mentioned process -
mysql -u root -p root database_name < path to your .sql file
What I'm doing above is:
Entering to mysql with my username and password (here it is root & root)
After entering the password I'm giving the name of database where I want to import my .sql file. Please make sure the database already exists in your MYSQL
The database name is followed by < and then path to your .sql file. For example, if my file is stored in Desktop, the path will be /home/Desktop/db.sql
That's it. Once you've done all this, press enter and wait for your .sql file to get uploaded to the respective database
There has to be no space between -p and password
mysql -u [dbusername] -p[dbpassword] [databasename] < /home/serverusername/public_html/restore_db/database_file.sql
I always use it, it works perfectly. Thanks to ask this question. Have a great day. Njoy :)