206

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?

9 Answers 9

266

You mean while the mysql environment?

create database testdb;

Or directly from command line:

mysql -u root -e "create database testdb"; 
Sign up to request clarification or add additional context in comments.

5 Comments

mysql -u root -e "create database testdb"; Thats what i was looking for :) thank you
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";
hi @koszikot -- if this is what you were looking for, you should accept it as the correct answer :-)
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.html
the semicolon should be inside quotes
188
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.

8 Comments

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 command
@keerthi: perhaps your user doesn't have privileges on any existing schema?
It's because there are two -u flags
@Kris Can I read database name from terminal and use the variable value as database name in create database.
@Clifford: you could using simple (ba)sh script using normal shell arguments. (stackoverflow.com/questions/23563043/shell-script-arguments) Probably merits its own question.
|
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

1 Comment

Massive. Just added this great one liner to dashdocs snippets. In case anyone has a root password with spaces: -p"root password here" works fine
45
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.

2 Comments

came back to this question many years later and noticed I'd upvoted it :D
i keep coming back here...
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.

Comments

14

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'";

1 Comment

This will fail if you include any .
11

You can use SQL on the command line:

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

Or you can use mysqladmin:

mysqladmin create dbname

Comments

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

Comments

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;'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.