从命令行下载 MySQL 转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13484667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading MySQL dump from command line
提问by Phillip Copley
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?
我要离开 Linode,因为我没有必要的 Linux 系统管理员技能;在我完成向对新手更友好的服务的过渡之前,我需要下载 MySQL 数据库的内容。有没有办法从命令行执行此操作?
回答by nickhar
You can accomplish this using the mysqldumpcommand-line function.
您可以使用mysqldump命令行函数来完成此操作。
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):
您甚至可以使用 gzip 自动压缩输出(如果您的数据库非常大):
$ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz
If you want to do this remotelyand you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):
如果您想远程执行此操作并且您可以访问相关服务器,那么以下操作将起作用(假设 MySQL 服务器在端口 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.
它应该将.sql
文件放在您运行命令行的文件夹中。
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.
编辑:已更新以避免在 CLI 命令中包含密码,请使用-p
不带密码的选项。它会提示你输入它而不是记录它。
回答by Lorenzo Lerate
In latest versions of mysql, at least in mine, you cannot put your pass in the command directly.
在 mysql 的最新版本中,至少在我的版本中,您不能直接将 pass 放入命令中。
You have to run:
你必须运行:
mysqldump -u [uname] -p db_name > db_backup.sql
mysqldump -u [uname] -p db_name > db_backup.sql
and then it will ask for the password.
然后它会要求输入密码。
回答by Andrew
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.
-p 表示您将输入密码,它与 db_name 无关。输入命令后,系统将提示您输入密码。输入它并按回车键。
回答by drooh
On windows you need to specify the mysql bin where the mysqldump.exe resides.
在 Windows 上,您需要指定 mysqldump.exe 所在的 mysql bin。
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
将其保存到文本文件中,例如 backup.cmd
回答by Nithin Raja
Open Command prompt, and directly type this command. Don't go inside mysql and then type this command.
打开命令提示符,直接输入这个命令。不要进入mysql然后输入这个命令。
mysqldump -u [uname] -p[pass] db_name > db_backup.sql
回答by user8376416
Go to MySQL installation directory and open cmd from there. Then execute the below command to get a backup of your database.
转到 MySQL 安装目录并从那里打开 cmd。然后执行以下命令以获取数据库的备份。
mysqldump -u root -p --add-drop-database --databases db> C:\db-dontdelete\db.sql
回答by Suganthan Madhavan Pillai
Just type mysqldump
or mysqldump --help
in your cmd will show how to use
只需输入mysqldump
或mysqldump --help
在您的 cmd 中将显示how to use
Here is my cmd result
这是我的cmd 结果
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
回答by Anand Raj
If you are running the MySQL other than default port:
如果您运行的是默认端口以外的 MySQL:
mysqldump.exe -u username -p -P PORT_NO database > backup.sql
回答by Tara Prasad Gurung
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
回答by Nabaasa Archie
Use this If you have the database with the name archiedb, use this mysql -p --databases archiedb > /home/database_backup.sql
使用这个如果你有名为 archiedb 的数据库,使用这个 mysql -p --databases archiedb > /home/database_backup.sql
Assuming this is linux, choose where the backup file will be saved.
假设这是 linux,选择备份文件的保存位置。