DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  1. DZone
  2. Data Engineering
  3. Databases
  4. Import and Export a Database with SSH

Import and Export a Database with SSH

Paul Underwood user avatar by
Paul Underwood
·
Jul. 17, 13 · Interview
Like (0)
Save
Tweet
Share
7.76K Views

Join the DZone community and get the full member experience.

Join For Free

The following code snippets will allow you to import and export a large database using SSH commands.

To get SSH access to your hosts server you will need to contact your current web hosting company. If you are on a shared hosting package you may not be allowed to have SSH access to your hosts server.

To run both of these commands you will need to have MySQL installed on your server.

Export A Database

The following example will show how you how to export your database into a single SQL file. It is important to backup your database regularly or when you are making changes to the server such as releasing new code.

To export the database you need to run the following command.

mysqldump -p -u username -h hostname database_name > dbname.sql

This runs the mysqldump command with a number of parameters. The first parameter is -p which means password, when you run this command the script will ask you to enter your database password. If you want to do this in one line then you type in the password after the -p without any spaces.

mysqldump -pP@55w0rd -u username -h hostname database_name > dbname.sql

The next parameter is -u which means username, this will be the username you use to access your database. The next parameter is -h for host, you only need to use this if your database is on a different server, if you have your database on the current server then you do not need this parameter.

Next you type in the database name that you want to export, followed by a > for export and then the name and location of the file you are going to export this data into. In this example it just puts the file in dbname.sql which will place the file in the current location you are in, if you want to put these in a certain folder you need to provide the full folder path.

mysqldump -pP@55w0rd -u username -h hostname database_name > /var/www/vhosts/website/backup/dbname.sql

Import A Database

With the SQL file that you have just exported you can now easily import this into any database that you want by using the following code snippet.

First you need to make sure that they file you want to import is of a .sql format, then upload this file to your server so that you have access to it from an SSH command. Next make sure that the database you want to import this file into has been created, now you will able to import the database.

mysql -p -u username -h hostname database_name < /var/www/vhosts/website/backup/dbname.sql

This time you are running the mysql command with the same parameters, -p for password again if you want to put the password in this one command you can do or you will be asked for the password when you run this command. Next parameter is -u for the username of the database, -h for the host if the database is on a different server, next is the name of the database you want to import the SQL file. The next parameter is a < for import and then the location of the file you want to import.

Database

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: