Cara menggunakan mysqldump ssh remote server

If you need to create a backup of a remote MySQL database, you can use the mysqldump command. The mysqldump application is known as a client utility and installed alongside MySQL itself.

When executed, mysqldump will create a dump file of the database that includes all the data and the structure of the database in INSERT statements that you can run on your local machine to create a local copy of the remote database.

In this tutorial, I’ll work through a simple example of using mysqldump to create a local copy of a remote database. I’ll be connecting to a remote server running Ubuntu Server via SSH, running the mysqldump command to backup the database to a file, and then downloading that file to my local machine using SCP.

1. Login to the remote server using SSH

First, open a terminal and use SSH to login to the remote server hosting your MySQL database. You will need to enter your remote server username before the server IP address using the @ symbol. After running the command enter your password to login.

2. Use mysqldump to create a backup of your MySQL database

Next, on the remote server, run the mysqldump command to create a backup of your MySQL database. Modify the command below and replace your_mysql_username with your MySQL username, and replace your_database_name with the name of your database. Then run the command.

It may take a few minutes depending on the size of your database. The mysqldump command will export the entire MySQL database and write the contents to a .sql file that you can download. This can then be used to create a new instance of your MySQL database on another server.

mysqldump -u your_mysql_username --password='xxxxxxxxxxx' your_database_name > your_database_name.sql

For the next step you’ll need the full path to the backup file, so enter pwd to obtain your present working directory. For example, mine is /home/matt.

3. Logout of the SSH connection

Logout of the SSH connection to your remote machine and return to the shell on your local machine by entering

scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
0. This will return you to the shell on your local machine.

4. Use SCP to download the SQL backup to your local machine

Finally, on your local machine, we’ll use the SCP or secure copy application to download the MySQL backup file from the server via SSH.

In the command below we’re starting

scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
1 and creating an SSH connection for the user
scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
2 on the IP address
scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
3.

Once logged in, SCP will look for the file at

scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
4 on the remote server and download it to
scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql
5 on the local machine. It will give you a useful progress bar, so you can track how much of the database has been downloaded.

scp [email protected]:/home/matt/your_database_name.sql /home/work/backups/your_database_name.sql

Creating a local copy of your MySQL database is a good idea for several reasons. Firstly, it keeps your data backed up. Secondly, it allows you to create your own development environment where you can safely run complex MySQL queries against your database without the risk of slowing down your production database.

To create a local development environment for data science projects running on MySQL databases my preferred approach is to run MySQL in a Docker container. This is a great way to create a development environment for data science projects that can be run on a local machine without the need to install a full-blown server.

Yang pertama saya akan membahas bagaimana saya mengambil semua data dan structure dari database kedalam satu file yaitu file .sql.

Login ke server via ssh

kalian harus login dulu ke server via ssh untuk bisa mengakses database yang ada pada server. kalian bisa login via ssh menggunakan perintah berikut

ssh [email protected]

namadomain.local diganti dengan domain yang diinginkan, ini tanya sebagai contoh. setelah itu kalian akan diminta masukan password/katasandi user tersebut. Jika berhasil login akan muncul command prompt seperti ini :

namauser@namaserver:~#

itu berarti anda berada pada direktori ~ pada server.


Menurut pengalaman saya, saya login ke mysql untuk mengakses database, untuk masuk ke mysql, bisa dilakukan dengan perintah :

#mysql -u namauserdatabase -p (tekan enter)
enter password:

Setelah masuk, command prompt akan berubah menjadi :

mysql>_

itu artinya kalian sudah berada pada aplikasi mysql, nah setelah itu kalian bisa meng-export database yang diinginkan dengan perintah :

mysql>mysqldump -u namauserdatabase -p database > backup.sql;

keterangan :

  • namauserdatabase adalah nama user yang diberikan izin untuk mengakses database
  • database adalah database yang akan di-bakcup
  • backup.sql adalah file backup dari database dengan extension sql

jika kalian melakukan yang seperti ini, maka perintah yang kalian lakukan sudah benar tapi penggunaannya keliru. saat kalian menekan enter, maka akan muncul pesan error :