I am running the most recent version of MariaDB on a CentOS7 machine. I have two databases I need to back up. One for Postfix/Dovecot and Another for Wordpress. I have read a few guides and most just say that I should be good running
sudo mysqldump -h 127.0.0.1 -u root -p somepassword --all_databases > /tmp/backup.sql
Produces the output
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
However, I suspect wordpress is complicating this process a bit because my CLI input is rewritten as
sudo mysqldump -h 127.0.0.1 -u root -p SomeIncorrectPassword find /var/www/somewordpressdir/wordpress/ -type d -exec chmod 755 {} \;! --all-databases > /tmp/backup.sql
I'm not sure why this is happening and I didn't come across anything similar. I am clearly missing something very basic here. What additional options do I need to provide?
I was reading an article that used the
--single_transaction
tag and this produced a different output
mysqldump: You can't use --single-transaction and --lock-all-tables at the same time.
it's not "--all_databases" with underscore but it's "--all-databases" with minus :-)
Related
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
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.
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
I know how mysqldump works.
But dont know where to use it?
If I execute this command after starting mysql program then it says error.
I am using ubuntu. So how can I use this utility?
Backup your database this way too..
mysql -u root -p DB_NAME > db_name_backup.sql
If you want to backup all database simply run this
mysql -u root -p > mysql_db_backup.sql
You will learn more about mysql and mysqldump here..
Guide:
mysqldump and mysql
MySQL Database Backup using mysqldump
shell> mysqldump --opt db_name > backup-file.sql
You can read the dump file back into the server like this:
shell> mysql db_name < backup-file.sql
Or like this:
shell> mysql -e "source /path-to-backup/backup-file.sql" db_name
mysqldump is also very useful for populating databases by copying data
from one MySQL server to another:
shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name
It is possible to dump several databases with one command:
shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql
If you want to dump all databases, use the --all-databases option:
shell> mysqldump --all-databases > all_databases.sql
If tables are stored in the InnoDB storage engine, mysqldump provides a
way of making an online backup of these (see command below). This
backup just needs to acquire a global read lock on all tables (using
FLUSH TABLES WITH READ LOCK) at the beginning of the dump. As soon as
this lock has been acquired, the binary log coordinates are read and
lock is released. So if and only if one long updating statement is
running when the FLUSH... is issued, the MySQL server may get stalled
until that long statement finishes, and then the dump becomes
lock-free. So if the MySQL server receives only short (in the sense of
"short execution time") updating statements, even if there are plenty
of them, the initial lock period should not be noticeable.
shell> mysqldump --all-databases --single-transaction > all_databases.sql
For point-in-time recovery (also known as “roll-forward”, when you need
to restore an old backup and replay the changes which happened since
that backup), it is often useful to rotate the binary log (see
Section 8.4, “The Binary Log”) or at least know the binary log
coordinates to which the dump corresponds:
shell> mysqldump --all-databases --master-data=2 > all_databases.sql
or
shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql
The simultaneous use of --master-data and --single-transaction works as
of MySQL 4.1.8. It provides a convenient way to make an online backup
suitable for point-in-time recovery if tables are stored in the InnoDB
storage engine.
For more information on making backups, see Section 6.1, “Database
Backups”.
mysqldump -u MYSQL_USER -h MYSQL_SERVER -pMYSQL_PASS --all-databases > "dbs.sql"
You use it directly on the terminal, just like mysql it self, and pass the parameters directly to it.
mysqldump -u [user] -p[password] [database name] > dumpfilename.sql
yes you can.
see http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html for more information on the tool.
If it's an entire DB, then:
$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql
If it's all DBs, then:
$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql
If it's specific tables within a DB, then:
$ mysqldump -u [uname] -p[pass] db_name table1 table2 >
table_backup.sql
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz
If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name >
db_backup.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
I am moving away from Linode because I don't have the Linux sysadmin skills necessary; before I complete the transition to a more noob-friendly service, I need to download the contents of a MySQL database. Is there a way I can do this from the command line?
You can accomplish this using the mysqldump command-line function.
For example:
If it's an entire DB, then:
$ mysqldump -u [uname] -p db_name > db_backup.sql
If it's all DBs, then:
$ mysqldump -u [uname] -p --all-databases > all_db_backup.sql
If it's specific tables within a DB, then:
$ mysqldump -u [uname] -p db_name table1 table2 > table_backup.sql
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz
If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql
It should drop the .sql file in the folder you run the command-line from.
EDIT: Updated to avoid inclusion of passwords in CLI commands, use the -p option without the password. It will prompt you for it and not record it.
In latest versions of mysql, at least in mine, you cannot put your pass in the command directly.
You have to run:
mysqldump -u [uname] -p db_name > db_backup.sql
and then it will ask for the password.
If downloading from remote server, here is a simple example:
mysqldump -h my.address.amazonaws.com -u my_username -p db_name > /home/username/db_backup_name.sql
The -p indicates you will enter a password, it does not relate to the db_name. After entering the command you will be prompted for the password. Type it in and press enter.
On windows you need to specify the mysql bin where the mysqldump.exe resides.
cd C:\xampp\mysql\bin
mysqldump -u[username] -p[password] --all-databases > C:\localhost.sql
save this into a text file such as backup.cmd
Don't go inside mysql, just open Command prompt and directly type this:
mysqldump -u [uname] -p[pass] db_name > db_backup.sql
Just type mysqldump or mysqldump --help in your cmd will show how to use
Here is my cmd result
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump
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
Go to MySQL installation directory and open cmd from there.
Then execute the below command to get a backup of your database.
mysqldump -u root -p --add-drop-database --databases db> C:\db-dontdelete\db.sql
If you are running the MySQL other than default port:
mysqldump.exe -u username -p -P PORT_NO database > backup.sql
For those who wants to type password within the command line. It is possible but recommend to pass it inside quotes so that the special character won't cause any issue.
mysqldump -h'my.address.amazonaws.com' -u'my_username' -p'password' db_name > /path/backupname.sql
If you have the database named archiedb, use this:
mysql -p <password for the database> --databases archiedb > /home/database_backup.sql
Assuming this is Linux, choose where the backup file will be saved.
For some versions of MySQL try.
sudo mysqldump [database name] > db_backup.sql
mysqldump is another program (.exe file) in the MySQL directory
Program Files\MySQL\MySQL Server 8.0\bin
step 1: First you have to go to the path and open CMD from the folder.
step 2: Then type mysqldump in the CMD
it should display as follows
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
step 3: Then type this command
mysqldump -u [user_name] -p [database_name] > D:\db_dump.sql
Note :
you should provide an absolute path for the output file.
Here I provide D:\
For Windows users you can go to your mysql folder to run the command
e.g.
cd c:\wamp64\bin\mysql\mysql5.7.26\bin
mysqldump -u root -p databasename > dbname_dump.sql
Note: This step only comes after dumping your MySQL file(which most of the answers above have addressed).
It assumes that you have the said dump file in your remote server and now you want to bring it down to your local computer.
To download the dumped .sql file from your remote server to your local computer, do
scp -i YOUR_SSH_KEY your_username#IP:name_of_file.sql ./my_local_project_dir
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%.%MM%.%DD%.%HH%.%Min%.%Sec%"
set drive=your backup folder
set databaseName=your databasename
set user="your database user"
set password="your database password"
subst Z: "C:\Program Files\7-Zip"
subst M: "D:\AppServ\MySQL\bin"
set zipFile="%drive%\%databaseName%-%datestamp%.zip"
set sqlFile="%drive%\%databaseName%-%datestamp%.sql"
M:\mysqldump.exe --user=%user% --password=%password% --result-file="%sqlFile%" --databases %databaseName%
#echo Mysql Backup Created
Z:\7z.exe a -tzip "%zipFile%" "%sqlFile%"
#echo File Compress End
del %sqlFile%
#echo Delete mysql file
pause;