How to create a database from shell command in MySQL?
Asked Answered
E

9

200

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?

Etalon answered 11/3, 2010 at 20:24 Comment(0)
W
186
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...

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

If you really only want to create a database.

Welcher answered 11/3, 2010 at 20:29 Comment(8)
I get this error ERROR 1046 (3D000) at line 27: No database selected when I run echo "create database databasename" | mysql -u -u username -p commandSiderosis
@keerthi: perhaps your user doesn't have privileges on any existing schema?Welcher
It's because there are two -u flagsAriosto
@Welcher Can I read database name from terminal and use the variable value as database name in create database.Moth
@Clifford: you could using simple (ba)sh script using normal shell arguments. (https://mcmap.net/q/41225/-shell-script-arguments) Probably merits its own question.Welcher
@Welcher Thank you for your help.My problem is something different.Moth
@Felix Kling's answer is the better one and most directly answers the question. I could not get this to work because my .sql file (straight from a mysqldump) didn't contain a command to create the database, and the database itself didn't already exist.Hardiness
echo "create database \`database-name\`" | mysql -u username -p (escape with \)Precise
H
256

You mean while the mysql environment?

create database testdb;

Or directly from command line:

mysql -u root -e "create database testdb"; 
However answered 11/3, 2010 at 20:27 Comment(5)
mysql -u root -e "create database testdb"; Thats what i was looking for :) thank youEtalon
In case you have password, just add -p. After pressing enter, you will be asked your password. The following is an example using my user which is root (change it if you have anoter username) and it also uses the name "oc_local" for the database (change it if you want your database to be called in a different way)------> mysql -u root -p -e "create database oc_local";Tehee
hi @Etalon -- if this is what you were looking for, you should accept it as the correct answer :-)Imre
I like to know what the command line options do, for others -e is the short-form for --execute=statement. Find command line options here dev.mysql.com/doc/refman/8.0/en/mysql-command-options.htmlGerri
the semicolon should be inside quotesSenatorial
W
186
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...

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

If you really only want to create a database.

Welcher answered 11/3, 2010 at 20:29 Comment(8)
I get this error ERROR 1046 (3D000) at line 27: No database selected when I run echo "create database databasename" | mysql -u -u username -p commandSiderosis
@keerthi: perhaps your user doesn't have privileges on any existing schema?Welcher
It's because there are two -u flagsAriosto
@Welcher Can I read database name from terminal and use the variable value as database name in create database.Moth
@Clifford: you could using simple (ba)sh script using normal shell arguments. (https://mcmap.net/q/41225/-shell-script-arguments) Probably merits its own question.Welcher
@Welcher Thank you for your help.My problem is something different.Moth
@Felix Kling's answer is the better one and most directly answers the question. I could not get this to work because my .sql file (straight from a mysqldump) didn't contain a command to create the database, and the database itself didn't already exist.Hardiness
echo "create database \`database-name\`" | mysql -u username -p (escape with \)Precise
D
79

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:

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_user is the name for user with all privileges (probably the root)
base_user_pass it's the password for base_user (lack of space between -p and base_user_pass is important)
new_db is name for newly created database
new_db_user is name for the new user with access only for new_db
new_db_user_pass it's the password for new_db_user

Darreldarrell answered 16/7, 2013 at 12:6 Comment(1)
Massive. Just added this great one liner to dashdocs snippets. In case anyone has a root password with spaces: -p"root password here" works fineAlyssaalyssum
E
44
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.

Ezequieleziechiele answered 2/12, 2013 at 7:52 Comment(2)
came back to this question many years later and noticed I'd upvoted it :DBenally
i keep coming back here...Benally
G
24

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.

Geyser answered 11/8, 2015 at 19:8 Comment(0)
K
13

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'";
Kendrakendrah answered 8/4, 2017 at 21:5 Comment(1)
This will fail if you include any .Clementina
W
11

You can use SQL on the command line:

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

Or you can use mysqladmin:

mysqladmin create dbname
Worcester answered 11/3, 2010 at 20:26 Comment(0)
G
2

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

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

Gantrisin answered 3/3, 2015 at 2:36 Comment(0)
L
0

For example, after login, you can create apple database as shown below:

CREATE DATABASE apple;

And, before login, you can create apple database as shown below. *CREATE ... with "" and without ; is also available:

mysql -u root -p -e 'CREATE DATABASE apple;'

Or:

mysql -u root -p --execute='CREATE DATABASE apple;'
Laos answered 22/10, 2023 at 2:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.