Home >Database >Mysql Tutorial >How Can I Export a MySQL Database from the Command Line?

How Can I Export a MySQL Database from the Command Line?

Linda Hamilton
Linda HamiltonOriginal
2024-12-05 00:47:11266browse

How Can I Export a MySQL Database from the Command Line?

Exporting MySQL Database from Command Line

Problem:

Migrating away from a Linux-based server and in need of a method to export the contents of a MySQL database using the command line.

Solution:

Utilizing the mysqldump command-line function provides an efficient and straightforward solution for exporting MySQL databases. Here's how to do it:

Exporting an Entire Database:

mysqldump -u [username] -p database_name > database_backup.sql

Exporting All Databases:

mysqldump -u [username] -p --all-databases > all_databases_backup.sql

Exporting Specific Tables:

mysqldump -u [username] -p database_name table1 table2 > table_backup.sql

Auto-Compressing Output:

mysqldump -u [username] -p database_name | gzip > database_backup.sql.gz

Remote Export:

mysqldump -P 3306 -h [server_ip] -u [username] -p database_name > database_backup.sql

Notes:

  • Use -p option without specifying password for security reasons. It will prompt for user input.
  • The SQL file will be saved in the current working directory.
  • It's recommended to replace placeholders ([username], [database_name], etc.) with actual values.

The above is the detailed content of How Can I Export a MySQL Database from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn