如何从终端导入mysql数据库?

我找不到确切的语法。


当前回答

如果您想从SQL转储中导入数据库,其中可能有“use”语句,我建议使用“-o”选项作为保护,以免意外导入到错误的数据库。

   •   --one-database, -o

       Ignore statements except those those that occur while the default
       database is the one named on the command line. This filtering is
       limited, and based only on USE statements. This is useful for
       skipping updates to other databases in the binary log.

完整的命令:

mysql -u <username> -p -o <databasename> < <filename.sql>

其他回答

在挣扎了一段时间后,我在https://tommcfarlin.com/importing-a-large-database/上找到了信息

连接Mysql(让我们使用root用户名和密码): Mysql -uroot - root 连接到数据库(假设它被称为emptyDatabase(你应该得到一个确认消息): 连接emptyDatabase

3导入源代码,假设这个文件叫做mySource。它位于myUser用户配置文件下的mySoureDb文件夹中:

source /Users/myUser/mySourceDB/mySource.sql

解释:

首先创建一个数据库或使用现有的数据库。在我的例子中,我使用的是一个现有的数据库 在我的例子中,通过给出<数据库> = ClassicModels的名称来加载数据库,并使用操作符<给出数据库的路径= sakilla -data.sql 通过运行show tables,我得到了您可以看到的表列表。

注意:在我的情况下,我得到了一个错误1062,因为我试图再次加载相同的东西。

打开终端

 mysql -u root -p

 eg:- mysql -u shabeer -p

之后创建数据库

 mysql> create database "Name";

 eg:- create database INVESTOR;

然后选择新的数据库INVESTOR

 mysql> USE INVESTOR;

从机器中选择sql文件的路径

 mysql> source /home/shabeer/Desktop/new_file.sql;

然后按enter并等待一段时间,如果它全部执行

 mysql> exit

在终端类型中

mysql -uroot -p1234; use databasename; source /path/filename.sql

窗户的首选方式:

打开控制台并启动交互式MySQL模式 使用< name_of_your_database >; 源< path_of_your_.sql >