# Mysql database backup and restore by mysqldump

## MySQL: The Go-To Open-Source RDBMS

**MySQL** is a premier open-source Relational Database Management System (RDBMS) acclaimed for its ability to efficiently store, manage, and retrieve structured information. It serves as the backbone for a myriad of applications, ranging from modest personal projects to complex, high-traffic internet sites and sophisticated enterprise solutions.

Key highlights of MySQL include its high performance, robust reliability, seamless scalability, and user-friendly nature. Moreover, it boasts wide compatibility across leading operating systems such as Linux, macOS, Windows, and Ubuntu, making it a versatile solution for any environment.

## **Prerequisites: -**

Require two Ubuntu 22.04

mysql-server-8.0

### Update && upgrade

```
apt-get update -y && apt-get upgrade -y 
```

### **Install MySQL**&#x20;

Install MySQL by typing the following command:

```
apt-get install mysql-server-8.0 -y
```

To secure the installation, **MySQL** inside-

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FlRuPoL5uWouq7Ilidck1%2Fimage.png?alt=media&#x26;token=c678f132-d599-45ac-9922-36a19e923d71" alt=""><figcaption></figcaption></figure>

### Create **MySQL** databas*e:*

```
mysql> create database testing_database; 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FjwEzy2eDH4BvNOqc9g7q%2Fimage.png?alt=media&#x26;token=97625e6d-6a7b-4e6e-bc47-cad0954a758d" alt=""><figcaption></figcaption></figure>

Using testing database-

```
mysql> user tesing_database;
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FoISW4LipAFAEZF7MDMwe%2Fimage.png?alt=media&#x26;token=8697740d-b90c-4750-91a6-95d6ea4ac5ad" alt=""><figcaption></figcaption></figure>

Create testing\_table in the testing\_database&#x20;

```
mysql> create table testing_table (id int, name varchar(50), address varchar(50), primary key (id));
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FkiQFXktQ4tv7ejII6eTE%2Fimage.png?alt=media&#x26;token=679afede-0a27-41b6-ae97-e2bbc560c0c1" alt=""><figcaption></figcaption></figure>

Insert data to testing\_table

```
mysql> insert into testing_table(id, name, address) values("001", "Ubuntu", "Hiroshima");
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FuedcjUcnkMr2vVSPGvIj%2Fimage.png?alt=media&#x26;token=6803f644-1b65-4ae7-b30d-71ea9a152e3b" alt=""><figcaption></figcaption></figure>

show testing\_table

```
mysql> select * from testing_table;
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FowCtJbTFm15WlJpPlRxF%2Fimage.png?alt=media&#x26;token=1c8a626a-a13f-4aca-8eba-3a143d666b33" alt=""><figcaption></figcaption></figure>

### TAKE MYSQL BACKUP AND RESTORE

```
mysqldump --lock-all-tables --all-databases --events > mysql_dump.sql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FiafgFVjTGZrsoZe1pnJM%2Fimage.png?alt=media&#x26;token=976b4d1e-9442-4148-bbfc-6a73dd32154a" alt=""><figcaption></figcaption></figure>

Dump all data without locking but with transaction-ensured data integrity by \[--single-transaction] option:-

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F1josb0j8tHMtQasGySFP%2Fimage.png?alt=media&#x26;token=81ac6fff-b607-4db7-a3da-2152d2babf48" alt=""><figcaption></figcaption></figure>

Dump specific database:-

```
mysqldump test_database --single-transaction --events > mysql_dump.sql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FcUmWlxcZ9OrCiu7yQBOa%2Fimage.png?alt=media&#x26;token=a8ca7aad-1637-4b2e-b1ee-d43839ebc366" alt=""><figcaption></figcaption></figure>

### For Restore Database

For restoring data from backup on another host, run as follows. Before restoring, transfer dump data to the target host with \[rsync] or \[scp] and so on.

Copy these mysql\_dump.sql another machine:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FGI3ZP5s1suPgCY046S26%2Fimage.png?alt=media&#x26;token=bf089084-7728-4a73-bbcc-204294815a2e" alt=""><figcaption></figcaption></figure>

**$ sftp root\@another\_machine\_ip**

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FQYVIEbjyivYiyR1zWbUa%2Fimage.png?alt=media&#x26;token=41819280-74bd-4840-a2df-7a53525068a0" alt=""><figcaption></figcaption></figure>

Go to another server machine:&#x20;

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F3gmMc8SIpCYTPIH9xHeC%2Fimage.png?alt=media&#x26;token=ee1c5bdc-5f62-4a67-a0ea-7352035f4c4d" alt=""><figcaption></figcaption></figure>

```
apt-get install mysql-server-8.0 -y
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FwRGuHrxT7ajv0ODuYFBr%2Fimage.png?alt=media&#x26;token=d09c12de-1865-4177-8e02-489b741f2721" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FpmCpiqsn1WrJxspWc0Gr%2Fimage.png?alt=media&#x26;token=be2cae76-0d19-412a-9835-608e4faeaf5e" alt=""><figcaption></figcaption></figure>

```
mysql testing_database < mysql_dump.sql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FNK1HNKGRIwYcYgoBYAfo%2Fimage.png?alt=media&#x26;token=909db0b4-5c0a-4002-a2b9-69ee40622e55" alt=""><figcaption></figcaption></figure>
