MySQL从数据库文件中导入数据

MySQL将数据库文件中的数据导入到数据库,有以下几种方式

① 使用phpMyAdmin导入

此种方式的导入有个缺点就是大数据文件可能无法导入成功。

② 使用MySQL图形化工具导入,例如:heidisql软件

直接将数据库文件朝窗口拖进去,点击执行按钮导入

③ 使用宝塔导入

点击导入按钮,上传数据库文件即可导入

④ 使用MySQL命令行导入

先将要导入的数据库文件上传至服务器,记住文件位置,例如:/root/test.sql

登录MySQL

mysql -u root -p

root为MySQL数据库用户名,登录其它用户只需要将root修改为其它用户名即可,然后按回车键,输入该用户对应的数据库密码即可。

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 859
Server version: 5.7.38-log Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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.

mysql> 

选择数据库

use wp5;

wp5为数据将要导入到的数据库名称。

导入数据

source /root/test.sql

/root/test.sql为数据库文件位置。

导入成功将会出现下面的文字提示

mysql> source /root/test.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)