从命令行创建 MySQL 数据库

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

Create MySQL DB from Command Line

mysqlwindowscommand-line

提问by linuts

I am trying to create a batch file to create a MySQL Database. So far, none of the information I am finding is working. Just for testing, this is what I am trying...

我正在尝试创建一个批处理文件来创建 MySQL 数据库。到目前为止,我发现的所有信息都不起作用。只是为了测试,这就是我正在尝试的......

C:\>mysql -uroot -ppassword < CREATE DATABASE testdb;
C:\>mysql -uroot -ppassword mysql < CREATE DATABASE testdb;

No matter how I put it, I keep getting the error "The system cannot find the file specified". If I just put...

不管我怎么写,我总是收到错误“系统找不到指定的文件”。如果我只是放...

C:\>mysql -uroot -ppassword

It logs into the MySQL prompt fine. What exactly am I doing wrong?

它登录到 MySQL 提示符很好。我到底做错了什么?

回答by linuts

I agree with the other posters, it's much better to put the schema into a file. However, here's how you can do it on the command line:

我同意其他海报,将架构放入文件中要好得多。但是,您可以通过以下方式在命令行上执行此操作:

mysql -uroot -ppassword -e "CREATE DATABASE testdb"

回答by Developer

acess as root user :

以 root 用户身份访问:

mysql -u root -p

it asks for password..enter your password

它要求输入密码..输入您的密码

then run the create command like this:

然后像这样运行创建命令:

mysql> create database database_name;

回答by Dan Blows

It's better to write your MySQL inside a file and then import that file. That way, when you need to run it again (reinstalling or migrating) you have a record of the MySQL to run. The code I use for a file like this is as follows, which destroys anything that's already there, and then creates the database and assigns a dedicated user.

最好将 MySQL 写入文件中,然后导入该文件。这样,当您需要再次运行它(重新安装或迁移)时,您就有了要运行的 MySQL 的记录。我用于这样的文件的代码如下,它会破坏已经存在的任何内容,然后创建数据库并分配一个专用用户。

# uninstall if anything's already there
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';
DROP USER 'username'@'%';
DROP DATABASE IF EXISTS `tablename`;

# create the user
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `tablename`;
GRANT ALL PRIVILEGES ON `tablename` . * TO 'username'@'%';

回答by mylesmg

Try putting your sql into a text file, like 'createDb.sql' and then run:

尝试将您的 sql 放入文本文件中,例如“createDb.sql”,然后运行:

mysql -uroot -ppassword < createDb.sql;