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?
You mean while the mysql environment?
create database testdb;
Or directly from command line:
mysql -u root -e "create database testdb";
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 –Â
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.
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. –Â
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
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 –Â
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.
came back to this question many years later and noticed I’d upvoted it 😀 –Â
i keep coming back here… –Â
$ 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.
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'";
This will fail if you include any .
 –Â
You can use SQL on the command line:
echo 'CREATE DATABASE dbname;' | mysql <...>
Or you can use mysqladmin
:
mysqladmin create dbname
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
source : https://stackoverflow.com/questions/2428416/how-to-create-a-database-from-shell-command