记一次MySQL数据库备份与SQL格式内容导出导入

记一次MySQL数据库备份与SQL格式内容导出导入

MySQL数据导出

使用数据库管理工具(如MySQL Workbench和某cat)导出效果虽然好,但太慢了,这时候,我们可以使用服务器里的mysqldump命令行工具导出数据,速度非常快。

导出命令如下:

bash 复制代码
mysqldump -u 用户名 -p 数据库名 > 导出文件名.sql

在云服务器中使用示例如下:

bash 复制代码
[root@VM-16-8-opencloudos home]# /home/webapp/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysqldump -u root -p oxxx >  oxxx_backup.sql
Enter password: ********
[root@VM-16-8-opencloudos home]#

执行该语句,会在服务器本地生成对应的.sql文件,包含了数据库的结构和数据条目,其执行效率要比管理工具运行效率高不少。

MySQL的.sql文件导入

导出就是为了导入,更好地导出是为了更好地导入。

导入命令如下:

bash 复制代码
mysql -u 用户名 -p 数据库名 < 导入文件名.sql

或使用MySQL的source命令,参考如下示例:

bash 复制代码
[root@VM-16-7-opencloudos db_bak]# /home/webapp/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysql -uroot -p
/home/webapp/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

source命令的使用需要先连上mysql服务,然而......

错误就此出现:
error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

缺啥补啥呗,这是后我们可以通过安装ncurses-compat-libs解决该脸上数据库的问题,对于我要处理的服务器,命令是:

bash 复制代码
# dnf install ncurses-compat-libs

于是:

bash 复制代码
[root@VM-16-7-opencloudos db_bak]# dnf install ncurses-compat-libs
BaseOS 9 - x86_64                                                                   6.7 kB/s | 3.8 kB     00:00    
BaseOS 9 - x86_64                                                                   566 kB/s | 1.4 MB     00:02    
AppStream 9 - x86_64                                                                6.8 kB/s | 3.8 kB     00:00    
AppStream 9 - x86_64                                                                3.5 MB/s |  15 MB     00:04    
extras 9 - x86_64                                                                   5.3 kB/s | 3.0 kB     00:00    
extras 9 - x86_64                                                                   947  B/s | 1.1 kB     00:01    
Extra Packages for OpenCloudOS 9 - EPOL                                             3.9 kB/s | 3.0 kB     00:00    
Extra Packages for OpenCloudOS 9 - EPOL                                             4.6 MB/s |  19 MB     00:04    
Dependencies resolved.
====================================================================================================================
 Package                            Architecture          Version                    Repository                Size
====================================================================================================================
Installing:
 ncurses-compat-libs                x86_64                6.4-5.oc9                  AppStream                316 k
Upgrading:
 ncurses                            x86_64                6.4-5.oc9                  BaseOS                   216 k
 ncurses-base                       noarch                6.4-5.oc9                  BaseOS                    60 k
 ncurses-devel                      x86_64                6.4-5.oc9                  AppStream                530 k
 ncurses-libs                       x86_64                6.4-5.oc9                  BaseOS                   345 k

Transaction Summary
====================================================================================================================
Install  1 Package
Upgrade  4 Packages

Total download size: 1.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): ncurses-base-6.4-5.oc9.noarch.rpm                                             57 kB/s |  60 kB     00:01    
(2/5): ncurses-6.4-5.oc9.x86_64.rpm                                                 142 kB/s | 216 kB     00:01    
(3/5): ncurses-compat-libs-6.4-5.oc9.x86_64.rpm                                     207 kB/s | 316 kB     00:01    
(4/5): ncurses-libs-6.4-5.oc9.x86_64.rpm                                            399 kB/s | 345 kB     00:00    
(5/5): ncurses-devel-6.4-5.oc9.x86_64.rpm                                           762 kB/s | 530 kB     00:00    
--------------------------------------------------------------------------------------------------------------------
Total                                                                               661 kB/s | 1.4 MB     00:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                            1/1 
  Upgrading        : ncurses-base-6.4-5.oc9.noarch                                                              1/9 
  Upgrading        : ncurses-libs-6.4-5.oc9.x86_64                                                              2/9 
  Running scriptlet: ncurses-libs-6.4-5.oc9.x86_64                                                              2/9 
  Upgrading        : ncurses-6.4-5.oc9.x86_64                                                                   3/9 
  Upgrading        : ncurses-devel-6.4-5.oc9.x86_64                                                             4/9 
  Installing       : ncurses-compat-libs-6.4-5.oc9.x86_64                                                       5/9 
  Running scriptlet: ncurses-compat-libs-6.4-5.oc9.x86_64                                                       5/9 
  Cleanup          : ncurses-devel-6.4-3.oc9.x86_64                                                             6/9 
  Cleanup          : ncurses-6.4-3.oc9.x86_64                                                                   7/9 
  Cleanup          : ncurses-libs-6.4-3.oc9.x86_64                                                              8/9 
  Running scriptlet: ncurses-libs-6.4-3.oc9.x86_64                                                              8/9 
  Cleanup          : ncurses-base-6.4-3.oc9.noarch                                                              9/9 
  Running scriptlet: ncurses-base-6.4-3.oc9.noarch                                                              9/9 
  Verifying        : ncurses-compat-libs-6.4-5.oc9.x86_64                                                       1/9 
  Verifying        : ncurses-6.4-5.oc9.x86_64                                                                   2/9 
  Verifying        : ncurses-6.4-3.oc9.x86_64                                                                   3/9 
  Verifying        : ncurses-base-6.4-5.oc9.noarch                                                              4/9 
  Verifying        : ncurses-base-6.4-3.oc9.noarch                                                              5/9 
  Verifying        : ncurses-libs-6.4-5.oc9.x86_64                                                              6/9 
  Verifying        : ncurses-libs-6.4-3.oc9.x86_64                                                              7/9 
  Verifying        : ncurses-devel-6.4-5.oc9.x86_64                                                             8/9 
  Verifying        : ncurses-devel-6.4-3.oc9.x86_64                                                             9/9 

Upgraded:
  ncurses-6.4-5.oc9.x86_64              ncurses-base-6.4-5.oc9.noarch         ncurses-devel-6.4-5.oc9.x86_64        
  ncurses-libs-6.4-5.oc9.x86_64        
Installed:
  ncurses-compat-libs-6.4-5.oc9.x86_64                                                                              

Complete!

进入数据库服务的问题解决了:

bash 复制代码
[root@VM-16-7-opencloudos db_bak]# /home/webapp/mysql/mysql-5.7.29-linux-glibc2.12-x86_64/bin/mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 116260
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

不忘查看了下有些什么数据库:

bash 复制代码
mysql> show databases;

然而也没啥好看的。

执行source命令:

bash 复制代码
mysql> use oxxx;
Database changed
mysql> source /home/db_bak/oxxx_backup_20260123.sql
Query OK, 0 rows affected (0.01 sec)

数据库导入成功。

总结

有效使用服务器上MySQL的命令,可以提升我们数据处理的效率。用管理工具做这种操作,虽然运行起来方便,但数据有一定量的时候,效率会比较低,这是值得注意的。

相关推荐
小陈工1 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
0xDevNull5 小时前
MySQL数据冷热分离详解
后端·mysql
科技小花6 小时前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸6 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain6 小时前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希6 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神6 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员7 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java7 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿7 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb