MySQL使用纯命令行快速备份还原数据库【测试成功】

艺帆风顺 发布于 2025-04-03 20 次阅读


一、需求背景

    需要迁移Mysql数据库,图形化界面备份还原速度太慢。数据量较大,通过命令行执行备份还原。

    A服务器:需要迁移的数据库(公网);

    B服务器:迁移目标库(内网)

二、Mysql数据库备份(内网操作)

    将远程服务器的库备份到本地:

    mysqldump  -h 124.x.x.x -P 33606 -u root -pxxx store > store.sql

三、Mysql数据库还原

    1、创建名称为store的数据库,编码格式UTF8

CREATE DATABASE store CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    2、进入数据库:

    mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 130Server version: 5.7.34-log MySQL Community Server (GPL)
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    mysql> CREATE DATABASE store CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Query OK, 1 row affected (0.02 sec)
    mysql> use store;Database changedmysql> show tables;Empty set (0.00 sec)

        3、导入数据库sql文件:

            source  /home2/demo_envs/store/store.sql

      mysql> show tables;+------------------------------+| Tables_in_store |+------------------------------+| act_evt_log || act_ge_bytearray || act_ge_property || act_hi_actinst || act_hi_attachment || act_hi_comment || act_hi_detail || act_hi_identitylink || act_hi_procinst || act_hi_taskinst || act_hi_varinst || act_id_group || act_id_info || act_id_membership || act_id_user || act_procdef_info || act_re_deployment || act_re_model || act_re_procdef || act_ru_event_subscr || act_ru_execution || act_ru_identitylink || act_ru_job || act_ru_task || act_ru_variable || act_setting || ai_box_report || app_version || doc_material_type || doc_material_type_attr || doc_supplier || oauth_client_details || personal_dept || personal_info || sequence || store_arrival_doc || store_arrival_doc_detail || store_charge_off || store_charge_off_detail || store_inventory || store_inventory_detail || store_loc || store_loc_detail || store_loc_first || store_loc_log || store_loc_second || store_loc_second_type_attr || store_loc_third || store_loc_third_attr || store_loc_third_type_attr || store_material || store_material_attr || store_material_month_summary || store_outbound_order || store_outbound_order_detail || store_receipt_doc || store_receipt_doc_detail || store_requisition || store_requisition_detail || store_segment || store_segment_detail || store_segment_task || store_segment_task_loc || store_segment_task_type || sys_api_aspect || sys_dept || sys_dept_user_manager || sys_enum || sys_fiscal_period_set || sys_log || sys_menu || sys_menu_role || sys_message || sys_per_group || sys_role || sys_setting || sys_setting_value || sys_user || sys_user_dept || sys_user_dept_per || sys_user_per_group || sys_user_role || sys_warning |+------------------------------+83 rows in set (0.00 sec)