MySQL 如何从shell命令创建数据库?

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

How to create a database from shell command?

mysqlshellcommand-line

提问by koszikot

I'm looking for something like createdb in PostgreSQL or any other solution that would allow me to create database with a help of a shell command. Any hints?

我正在寻找类似 PostgreSQL 中的 createdb 或任何其他解决方案,它允许我在 shell 命令的帮助下创建数据库。任何提示?

回答by Felix Kling

You mean while the mysql environment?

你的意思是同时mysql环境?

create database testdb;

Or directly from command line:

或者直接从命令行:

mysql -u root -e "create database testdb"; 

回答by Kris

cat filename.sql | mysql -u username -p # type mysql password when asked for it

Where filename.sql holds all the sql to create your database. Or...

filename.sql 保存所有用于创建数据库的 sql。或者...

echo "create database `database-name`" | mysql -u username -p

If you really only want to create a database.

如果你真的只想创建一个数据库。

回答by jmarceli

If you create a new database it's good to create user with permissions only for this database (if anything goes wrong you won't compromise root user login and password). So everything together will look like this:

如果您创建一个新数据库,最好创建仅对该数据库具有权限的用户(如果出现任何问题,您不会危及 root 用户登录名和密码)。因此,所有内容都将如下所示:

mysql -u base_user -pbase_user_pass -e "create database new_db; GRANT ALL PRIVILEGES ON new_db.* TO new_db_user@localhost IDENTIFIED BY 'new_db_user_pass'"

Where:
base_useris the name for user with all privileges (probably the root)
base_user_passit's the password for base_user (lack of space between -p and base_user_pass is important)
new_dbis name for newly created database
new_db_useris name for the new user with access only for new_db
new_db_user_passit's the password for new_db_user

其中:
base_user是具有所有权限(可能是 root)的用户的名称
base_user_pass它是base_user的密码(-p 和 base_user_pass 之间缺少空格很重要)
new_db是新创建的数据库的名称
new_db_user是具有访问权限的新用户的名称仅适用于 new_db
new_db_user_pass它是new_db_user的密码

回答by karlingen

mysqladmin -u$USER -p$PASSWORD create $DB_NAME

Replace above variables and you are good to go with this oneliner. $USER is the username, $PASSWORD is the password and $DB_NAME is the name of the database.

替换上面的变量,你就可以使用这个 oneliner 了。$USER 是用户名,$PASSWORD 是密码,$DB_NAME 是数据库的名称。

回答by Willa

Use

$ mysqladmin -u <db_user_name> -p create <db_name>

You will be prompted for password. Also make sure the mysql user you use has privileges to create database.

系统将提示您输入密码。还要确保您使用的 mysql 用户具有创建数据库的权限。

回答by salah-1

The ist and 2nd answer are good but if anybody is looking for having a script or If you want dynamic i.e (db/username/password in variable) then here:

第一个和第二个答案很好,但是如果有人正在寻找脚本,或者如果您想要动态即(变量中的数据库/用户名/密码),那么在这里:

#!/bin/bash



DB="mydb"
USER="user1"
PASS="pass_bla"

mysql -uroot -prootpassword -e "CREATE DATABASE $DB CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql -uroot -prootpassword -e "CREATE USER $USER@'127.0.0.1' IDENTIFIED BY '$PASS'";
mysql -uroot -prootpassword -e "GRANT SELECT, INSERT, UPDATE ON $DB.* TO '$USER'@'127.0.0.1'";

回答by Luká? Lalinsky

You can use SQLon the command line:

您可以在命令行上使用SQL

echo 'CREATE DATABASE dbname;' | mysql <...>

Or you can use mysqladmin:

或者你可以使用mysqladmin

mysqladmin create dbname

回答by CaptZ-

Connect to DB using base user: mysql -u base_user -pbase_user_pass And execute CREATE DATABASE, CREATE USER and GRANT PRIVILEGES Statements.

使用基本用户连接到数据库: mysql -u base_user -pbase_user_pass 并执行 CREATE DATABASE、CREATE USER 和 GRANT PRIVILEGES 语句。

Here's handy web wizard to help you with statements www.bugaco.com/helpers/create_database.html

这是一个方便的网络向导,可帮助您处理报表 www.bugaco.com/helpers/create_database.html