使用 .SQL 文件创建 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10769344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:32:40  来源:igfitidea点击:

Create MySQL Database with .SQL File

mysqlmysqldump

提问by Amy

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):

我对 MySQL 一无所知。但我正在尝试使用 Visio 对 MySQL 数据库进行逆向工程。我知道我应该采取哪些步骤来做到这一点,但我无法让我的数据库显示在“数据库”部分(如下所示):

enter image description here

在此处输入图片说明

How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code: mysql -u username -p password database_name < filename.sql using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?

如何使用 .SQL 文件创建 MySQL 数据库并使其显示在此列表中?我试过这个代码: mysql -u username -p password database_name < filename.sql 当然使用我自己的凭据。但这似乎不起作用。如果要执行此语句,应将 .SQL 文件放在哪个文件夹中?

回答by rizzz86

1) Create a file "filename.sql"

1)创建一个文件“filename.sql”

2) Create a database in your DB in which you want to import this file.

2) 在您要导入此文件的数据库中创建一个数据库。

3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".

3) 从命令提示符/终端,移动到您创建“filename.sql”的目录。

4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sqlwill also work.

4) 运行命令:mysql -u username -p password database_name < filename.sql. (您还可以提供文件的正确路径并从任何地方运行此命令)。可能是您没有为 MySQL 设置密码。如果是这样,mysql -u username database_name < filename.sql也将起作用。

In your case if you have created a database with name ojsand also created a file with name ojs.sqlin C: drivethen run the following command:

在您的情况下,如果您创建了一个名为ojs的数据库,并在C: 驱动器中创建了一个名为ojs.sql的文件,则运行以下命令:

Edit:Put the path inside quotes.

编辑:将路径放在引号内。

mysql -u username -p password ojs < "C:\ojs.sql"

There is another way of importing tables in mysql. You can do it this way as well:

在 mysql 中还有另一种导入表的方法。你也可以这样做:

1) Connect your database

1)连接你的数据库

2) Type command "use ojs;"

2) 输入命令“use ojs;”

3) Type command "source C:/ojs.sql"

3) 输入命令“source C:/ojs.sql”

回答by Steven

Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:

大多数创建数据库的 MySQL SQL 文件都会“即时”创建数据库,因此您通常不需要做任何事情,除了:

  1. log-in

    mysql -u [username] -p[password]

  1. 登录

    mysql -u [username] -p[password]

(Note: make sure you do NOT include a space (' ') character between the -p and the [password]. MySQL will think that [password] is the name of the database you want to connect to. The 'general' log-in (above) does not assume you want to connect to any particular schema.)

(注意:确保在 -p 和 [password] 之间不要包含空格 (' ') 字符。MySQL 会认为 [password] 是您要连接的数据库的名称。'general' 日志-in(以上)并不假设您要连接到任何特定架构。)

  1. source the file (do not use quotes around filename)

    mysql> source [database_creation_file].sql

  1. 源文件(不要在文件名周围使用引号)

    mysql> source [database_creation_file].sql