mysqldump will not export an individual database - mysql

I am trying to export a database using mysqldump from command line. I am using the following syntax:
mysqldump -u root -ppassword databasename > outputfile.sql
I've tried several variations on this, but I always end up with the following as the contents of the output file:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
I can get mysqldump to export all of my databases if I exclude the database name, but it will not export just a single database.
Am I overlooking something here?

Troubleshooting from the comments above:
That is correct syntax. I'd guess that mysqldump is picking up some other options somewhere. Maybe it's a shell alias with an option like -A included in the alias definition? Try running \mysqldump ... to run it un-aliased.
Your reply:
#BillKarwin you were on the right track with -A. I tried mysqldump --print-defaults and apparently --all-databases is in the default arguments. I ran it with --no-defaults and it worked like a charm.
The problem is that --all-databases was configured as a default option. When you try using that option together with an argument specifying one database, it outputs the usage error you described.
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html says that all-databases can be either a command-line flag, or an option in the config file.
I'd suggest looking in your /etc/my.cnf or $HOME/.my.cnf for the all-databases option. It can appear either in the [mysqldump] group or the [client] group.

How about (without the space between u and root)
mysqldump -uroot -ppassword databasename > outputfile.sql

Putting your root password in a command line is a really bad idea. At the very least, create a .my.cnf in your home directory, setting permissions to 600 (rw for you only) containing:
[mysqldump]
user=root
password=yourpassword
This will allow you to perform that particular command without a password. Since there's no particular reason your root user needs to be doing the dump, why not just create a user that can do this?
Presumably, you're doing the mysqldump to back things up. To make life even easier on you, set it in cron as in the example below which executes at midnight. Because of the presence of the .my.cnf file containing the password, it doesn't need a password in the command
0 0 * * * /usr/bin/mysqldump -u root -h localhost databasename > /home/someuser/outputfile.sql 2>&1

Related

mysqldump in sh script not working

I've got a sh script to backup a database-server.
#!/bin/bash
mysqldump -u <username> -p<password> --all-databases --single-transaction --opt > /home/backup/h_157_2-1.sql
rsync -zrp --partial /home/backup/h_157_2-1.sql root#<server-ip>:/home/backup/H_157_2/
When I execute those two command on their own in the command line, they work as expected and I get a .sql file with content. But when I execute the script the file only contains this:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
I already tried to change up the order of the options or leave out the --opt but the result was still the same. So what could cause the command to not work in the script?
I couldn't find a way to resolve the initial problem. Even with the help from Elzo Valugi in chat the result was still the same: Excecuted on command line the mysqldump worked finde. Excecuted in the script I got the same msg all the time. (see initial question).
To resolve it I built a workaround to dump every database on it's own.
#!/bin/sh
for dir in /var/lib/mysql/*/;
do
dir=${dir%*/}
mysqldump -u <user> -p<password> ${dir##*/} --single-transaction --opt > /home/backup/h_157_2_${dir##*/}.sql
rsync -zrp --partial /home/backup/h_157_2_${dir##*/}.sql root#<server-ip>:/home/backup/H_157_2/
done;
With this I loop through all the directorys in the mysql storage directory, cut them down to the dir-name and use that for the mysqldump. This puts every database in its own file. I think this could be resolved to combine them all to one, but for my needs I'm fine with different files.

Mysqldump syntax error

I'm currently trying to execute a dump on mysql schema, but it keeps showing a message of sql syntax.
The command that I'm using is:
mysqldump -u root -p password "logicstore" > "c:\backup.sql";
Is that a punctuation issue or something like that?
PS: I've already tried different syntaxes. I've seen on others questions like these.
You can't set password in command line. And it is much more safe to use -r instead of pipe...
mysqldump -u root -p <database_name> -r <file_name>
If you want to use it without asking for password, you have to edit cardinals in .my.cnf file:
[mysqldump]
user=root
password=<password>

mysqldump wont dump my data

here is the command I'm using:
mysqldump.exe -u root -d capstone -verbse --skip-quote-names > capstone.sql
and the output I get
mysqldump: Warning: Can't set SQL_QUOTE_SHOW_CREATE option ()
-- Skipping dump data for table 'users', --no-data was used
any ideas? if I dump to XML it works but the place I'm importing it to doesn't handle XML and my data ruins the CSV output somehow too.
the -d option is alias of --no-data, see https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_no-data
perhaps you intended to state "use database capstone" but in that case it wouldn't be -d capstone, the database name doesn't need any switch/option, just put it in there
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#mysqldump-syntax
I think you mean to use either -B / --databases (which includes allows you to indicate multiple databases to dump instead of a database and tables) or no such argument at all. I think you also mistyped --verbose.
Note that if you include --databases a CREATE DATABASE statement is also included. This could be important depending up on how you intend to use the data.

mysql restore to different database

I back up my production database with the following command:
mysqldump -u root --opt --skip-extended-insert --databases my_production_db
The resulting dump file has the following lines near the top:
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `my_production_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `my_production_db `;
In order to restore the database to a different destination ie. my_debvelopment_db I have to open the dump file and edit the bits where the database is named.
Then I run:
mysql -u root -p <password> < mydumpfile
I have not figured out another way to do it.
As the database gets bigger this becomes impractical.
Am I missing something? Cant I somehow specify where I want to restore the database? Would I need a different backup command?
#minaz answer was good, but I want to append a little bit more.
The problem was caused by --databases keyword. If you omit the keyword, it will not contain any database creation contents.
So, Dump without --databases keyword.
mysqldump -u username -p database_name > dump.sql
And restore it with the target database name.
mysql -u username -p target_database_name < dump.sql
Also, there are several ways to do this. See the similar problem on here (dba.stackexchange).
If you drop the option --databases but still specify the database name, you will NOT get the create database statements. ie:
mysqldump -u root --opt --skip-extended-insert my_production_db
On your dev machine simply create any database you wish to restore to.
If you already have your dump you can strip the commands for creating and using the database. Simply remove the fourth and the new fifth line.
sed '4d' dump.sql | sed '5d' > dump-striped.sql
On windows xampp I used following commands to achieve this
Export
mysqldump -u root -p mydb > mydb.sql
Import
mysql -u root -p mynewdb < mydb.sql

Mysqldump question

I am trying to dump a large database using mysqldump command. I would like to avoid 'use database' command in the generated sql file.
This is because I want to create the same database with a different name. Since the sql file size is large I am unable to open the sql file and edit it.
I tried --no-create-db but still I am getting use command in the dump file
Please help.
Maybe you used something like this:
mysqldump -u -p <other options> --database your_database > file.sql
I discovered that when you use --database, the script is generated with that 'use your_database' line. So, don't use that option and the line is gone:
mysql -u -p <other options> your_database > file.sql
You should maybe post this on serverfault, but if you are on a linux box, you could consider sed (or perl/python scripts) to replace the name of the database, or remove the "use " line.
The way to do this is to run mysqldump once for each database. They way I did it is mysqldump -u user -p --tables databasename. This dumps all the tables for a database and removes the USE database statement.
--no-create-db is your friend:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-db