How to use mysql and pgsql to do database backup and restore backup operations


foreword

I encountered database migration operations at work, but using the traditional navicate data connection tool may cause a series of problems, such as data loss, so I use mysql and pgsql commands to do the migration in combination with my personal situation. Mysql or pgsql must be arranged before use.

1. How does mysql do backup?

1.1, backup a database

mysqldump -u用户名 -p密码 数据库名 > 导出的文件名
例如:
 mysqldump -hhostname -uroot -ppwd dataname >dataname.sql

1.2, back up all databases

mysqldump -h *.*.*.* -p -u username -p password --all-databases > /data/backup/mysql_db.sql

1.3, backup a table

For example:

mysqldump -hhostname -uroot -ppwd dataname specific_tabname1 specific_tabname2> dataname_specific_tabname.sql

1.4, backup multiple databases

mysqldump -hhostname -uusername -ppwd --databases dbname1 dbname2 dbname3 > multibackupfile.sql

1.5. Export a database structure

 mysqldump -hhostname -uroot -ppwd  -–add-drop-table --no-data dataname > dataname_nodata.sql

--no-data: no data
--add-drop-table Add a drop table command before each create statement

1.6. Backup compression

2. Backup compression

Format: mysqldump -h host IP -P port -u user name -p password --database database name | gzip > file name
. compression

mysqldump -h *.*.*.* -p -u username -p password --database mysql | gzip > /data/backup/mysql.sql.gz

Two, mysql restore backup

2.1. Command to restore MySQL database

#database is the database name

mysql -h ... -u username -p password database < backupfile.sql

2.2. Restore the compressed MySQL database

database is the database name

gunzip < backupfile.sql.gz | mysql -u username -p password database

2.3. Transfer the database to the new server

database is the database name

mysqldump -u username -p password database | mysql –host=*.*.*.* -C database

2.4. Import database

The source command is commonly used, use use to enter a certain database, mysql>source d:\test.sql, and the following parameters are script files.

3. How to make pgsql backup

Backup on linux system (here introduces the work of migrating a single database to another server)

-h connect database ip address
-p database port
-U database user
-c clear database object before recreating
-d specify database
-f specify backup to local file path

pg_dump -h 120.55.163.126 -p 5432 -U postgres -c -d homestead_pengze_prod -f
/ws/db-bak/homestead_pengze_prod.bak

提示:window安装的话进入pgsql的bin目录使用cmd目录执行命令

Migration:
Copy the backup file to the destination server directory (such as /ws), create a database with the same name on another server, use docker to install pgsql on the destination server, and first copy the backup script
to the container

docker cp homestead_pengze_prod.bak postgresql:/

Enter the container and execute the following restore backup script

docker exec -it postgresql /bin/sh

Four, pgsql restore backup

Restore script:
-d database to restore
-f backup file

psql -h 127.0.0.1 -p 5432 -U postgres -d homestead_pengze_prod -f
homestead_pengze_prod.bak

Guess you like

Origin blog.csdn.net/wei1359765074410/article/details/127634712