Why cron cant dump table from database? - mysql

I like to make cron that will dump specific table from database for some reason im getting empty file in .gz any ideas why ?
mysqldump -u root -pp;qqee test_db category | gzip >/home/user/BKP-Category/backup_$( date +"\%Y_\%m_\%d" ).sql.gz
im getting this output after execution
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases
OR mysqldump [OPTIONS] --system=[SYSTEMOPTIONS]]
For more options, use mysqldump --help
/usr/local/cpanel/bin/jailshell: q: command not found

the problem was password
-pp;qqee
because include special character ;
i should escape this like
-p'p;qqee'

This should work :
mysqldump -u root -p 'p;qqee' test_db category | gzip >/home/user/BKP-Category/backup_$( date +"\%Y_\%m_\%d" ).sql.gz

Related

Export selected tables (by a name pattern) into SQL or ZIP in PhpMyAdmin [duplicate]

I have about 100 different databases and I want to dump with mysqldump just the ones starting with a prefix "asd_"
I tried this but it's not working:
mysqldump -u[user] -p[pwd] -h [server.url] asd_* --single-transaction > backup.sql
I tried also:
mysqldump -u[user] -p[pwd] -h [server.url] "SHOW DATABASES LIKE 'asd_%'" --single-transaction > backup.sql
but does not work neither.
Thanks for your help.
mysqldump alone does not have support for wildcards for tables or databases.
You'll have to generate the list of databases as a separate step, and then use it in a mysqldump command. You can combine this step in a subcommand like this:
mysqldump ...options... --databases `mysql -B -N -e "SHOW DATABASES LIKE 'asd\_%'"`
Note that I have to backslash the _ character, because it's a metacharacter for LIKE.
I also have to use the --databases option, or else the second and subsequent database names will be interpreted as table names inside the first database. That's because the usage of mysqldump is one of the following:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
If you require more complex logic than the single LIKE condition offers with SHOW DATABASES LIKE ..., an alternative is to select the table names directly:
mysqldump --databases $(mysql -Bse "SELECT SCHEMA_NAME from information_schema.SCHEMATA WHERE SCHEMA_NAME LIKE 'prefix1\_%' OR SCHEMA_NAME LIKE 'prefix2\_%'") > dbs.sql

MySQL dump does not properly dump

I first run the following command:
mysqldump -u root -p password --all-databases > dump.sql
Then when I run nano dump.sql I get the following.
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 am not sure if this is right or if I am running the command wrong or something.
From mysqldump docs
If you use the short option form (-p), you cannot have a space between
the option and the password

mysqldump dump only database with certain prefix

I have about 100 different databases and I want to dump with mysqldump just the ones starting with a prefix "asd_"
I tried this but it's not working:
mysqldump -u[user] -p[pwd] -h [server.url] asd_* --single-transaction > backup.sql
I tried also:
mysqldump -u[user] -p[pwd] -h [server.url] "SHOW DATABASES LIKE 'asd_%'" --single-transaction > backup.sql
but does not work neither.
Thanks for your help.
mysqldump alone does not have support for wildcards for tables or databases.
You'll have to generate the list of databases as a separate step, and then use it in a mysqldump command. You can combine this step in a subcommand like this:
mysqldump ...options... --databases `mysql -B -N -e "SHOW DATABASES LIKE 'asd\_%'"`
Note that I have to backslash the _ character, because it's a metacharacter for LIKE.
I also have to use the --databases option, or else the second and subsequent database names will be interpreted as table names inside the first database. That's because the usage of mysqldump is one of the following:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
If you require more complex logic than the single LIKE condition offers with SHOW DATABASES LIKE ..., an alternative is to select the table names directly:
mysqldump --databases $(mysql -Bse "SELECT SCHEMA_NAME from information_schema.SCHEMATA WHERE SCHEMA_NAME LIKE 'prefix1\_%' OR SCHEMA_NAME LIKE 'prefix2\_%'") > dbs.sql

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.

Dump only the data with mysqldump without any table information?

I am looking for the syntax for dumping all data in my mysql database. I don't want any table information.
mysqldump --no-create-info ...
Also you may use:
--skip-triggers: if you are using triggers
--no-create-db: if you are using --databases ... option
--compact: if you want to get rid of extra comments
This should work:
# To export to file (data only)
mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql
# To export to file (structure only)
mysqldump -u [user] -p[pass] --no-data mydb > mydb.sql
# To import to database
mysql -u [user] -p[pass] mydb < mydb.sql
NOTE: there's no space between -p & [pass]
If you just want the INSERT queries, use the following:
mysqldump --skip-triggers --compact --no-create-info
>> man -k mysqldump [enter in the terminal]
you will find the below explanation
--no-create-info, -t
Do not write CREATE TABLE statements that re-create each dumped table.
Note This option does not not exclude statements creating log file
groups or tablespaces from mysqldump output; however, you can use the
--no-tablespaces option for this purpose.
--no-data, -d
Do not write any table row information (that is, do not dump table
contents). This is useful if you want to dump only the CREATE TABLE
statement for the table (for example, to create an empty copy of the
table by loading the dump file).
# To export to file (data only)
mysqldump -t -u [user] -p[pass] -t mydb > mydb_data.sql
# To export to file (structure only)
mysqldump -d -u [user] -p[pass] -d mydb > mydb_structure.sql
Best to dump to a compressed file
mysqldump --no-create-info -u username -hhostname -p dbname | gzip > /backupsql.gz
and to restore using pv apt-get install pv to monitor progress
pv backupsql.gz | gunzip | mysql -uusername -hhostip -p dbname
Would suggest using the following snippet. Works fine even with huge tables (otherwise you'd open dump in editor and strip unneeded stuff, right? ;)
mysqldump --no-create-info --skip-triggers --extended-insert --lock-tables --quick DB TABLE > dump.sql
At least mysql 5.x required, but who runs old stuff nowadays.. :)
Just dump the data in delimited-text format.
Try to dump to a delimited file.
mysqldump -u [username] -p -t -T/path/to/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,
When attempting to export data using the accepted answer I got an error:
ERROR 1235 (42000) at line 3367: This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
As mentioned above:
mysqldump --no-create-info
Will export the data but it will also export the create trigger statements. If like me your outputting database structure (which also includes triggers) with one command and then using the above command to get the data you should also use '--skip-triggers'.
So if you want JUST the data:
mysqldump --no-create-info --skip-triggers