MySQL 从终端运行数据库中的sql文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31820750/
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 21:10:59  来源:igfitidea点击:

Run sql file in database from terminal

mysqldatabaseshellterminal

提问by Brennan Casey

I have a server and within that server I have an sql script in the /var/www/ directory. That script is to create tables and databases. I need to know how to execute this sql script from terminal or some client while keeping the file on the server.

我有一台服务器,在该服务器中,我在 /var/www/ 目录中有一个 sql 脚本。该脚本用于创建表和数据库。我需要知道如何在将文件保存在服务器上的同时从终端或某些客户端执行此 sql 脚本。

PS- I don't know much of the terminology for this topic so I will look into other suggestions for my approach.

PS- 我不太了解这个主题的很多术语,所以我会为我的方法寻找其他建议。

回答by Tomislav

I presume that it is MYSQL. To run it from Unix / Linux environment you must do this:

我认为它是 MYSQL。要从 Unix / Linux 环境运行它,您必须执行以下操作:

$ mysql -h "server-name" -u "your_username" -p "your_password" "database-name" < "filename.sql"

There is another way:

还有一种方法:

mysql --host=localhost --user=your_username --password=your_password  -e "filename.sql"

回答by Max Droid

Try this:

尝试这个:

mysql -u your_username -p your_password

use db_name

source <path_to_sql_file>/file.sql

回答by Muzaffar Mahmood

If you want to run an SQL file for a specific database, then select the database first:

如果要为特定数据库运行 SQL 文件,请先选择数据库:

mysql -u your_username -p your_password

use db_name

source <path_to_sql_file>/file.sql