MySQL Table Dump Command
December 1, 2024 ยท AI Conversations
mysqldump -u [username] -p [database_name] [table_name] > [output_file].sql
### Explanation:
– **`-u [username]`**: Your MySQL username.
– **`-p`**: Prompts for the password.
– **`[database_name]`**: The name of the database containing the table.
– **`[table_name]`**: The name of the table to dump.
– **`[output_file].sql`**: The file where the table dump will be saved.
### Example:
If you want to dump a table named `users` from a database named `my_database`:
mysqldump -u root -p my_database users > users_table_dump.sql
### Additional Options:
– **Include structure only (no data):**
mysqldump -u root -p --no-data my_database users > users_structure.sql
– **Include data only (no structure):**
mysqldump -u root -p --no-create-info my_database users > users_data.sql
### Running in Docker:
If MySQL is running inside Docker, prepend the command with `docker exec`:
docker exec -i [container_name] mysqldump -u root -p[password] [database_name] [table_name] > [output_file].sql
Replace `[container_name]` with your MySQL container name and `[password]` with the root password.
### Syntax:
scp [user]@[remote_host]:[remote_file_path] [local_destination_path]
### Example:
If you want to download a file named `users_table_dump.sql` from a remote server:
scp ubuntu@ec2-16-170-254-93.eu-north-1.compute.amazonaws.com:/var/www/share/users_table_dump.sql ~/Downloads/
### Explanation:
– **`scp`**: Secure copy command.
– **`ubuntu`**: The SSH username (in your case, it's `ubuntu`).
– **`ec2-16-170-254-93.eu-north-1.compute.amazonaws.com`**: The remote host.
– **`/var/www/share/users_table_dump.sql`**: Path to the file on the remote server.
– **`~/Downloads/`**: Destination folder on your local machine.
### Notes:
1. Ensure SSH access is working (e.g., through `ssh ubuntu@ec2-16-170-254-93.eu-north-1.compute.amazonaws.com`).
2. If using an SSH key, you can specify it with the `-i` flag:
scp -i /path/to/ssh_key.pem ubuntu@ec2-16-170-254-93.eu-north-1.compute.amazonaws.com:/var/www/share/users_table_dump.sql ~/Downloads/
### Using Docker SSH Tunnel (if MySQL is in Docker):
If the file is inside a Docker container, first copy it from the container to the host, then use `scp` to download:
docker cp [container_name]:[path_in_container] [local_path_on_server]
scp ubuntu@ec2-16-170-254-93.eu-north-1.compute.amazonaws.com:/path/on/server/file.sql ~/Downloads/