88

I use the pylons and sqlalchemy. I constantly update the schema files and delete and recreate the database so that new schema can be made.

Every time I do this by opening the MySql Query Browser and login and delete the database/schema.

How do I delete the MySQL db/schema thorough linux shell commands in Ubuntu Linux?

1
  • This works on any OS not just linux Commented Jan 20, 2015 at 9:42

8 Answers 8

149

Try the following command:

mysqladmin -h[hostname/localhost] -u[username] -p[password] drop [database]
Sign up to request clarification or add additional context in comments.

6 Comments

Your password will be accessible to any other user on the system. ps aux. Consider using a .my.cnf instead.
I'm newbie to shell scripting, but for those of you who found this looking for a method to drop and create db from inside your script, this method worked for me.
if you want to see for yourself without logging in if indeed your database is blown away, I fancy mysqlshow -u [username] -p[Password]. You can even grep for the database as well mysqlshow -u [username] -p[password] | grep [database].
Without spaces between -u and username for sure?
Is there any way to drop all the databases with a single command instead of writing down the command for each DB?
|
45

In general, you can pass any query to mysql from shell with -e option.

mysql -u username -p -D dbname -e "DROP DATABASE dbname"

3 Comments

Your password will be accessible to any other user on the system. ps aux. Consider using a .my.cnf instead.
Or maybe just make a note that this is only for your local development environment where you are the only user.
The -D flag is not needed to DROP a database. It should be enough with: mysql -uuser -ppass -e "DROP DATABASE dbname"
29

If you are tired of typing your password, create a (chmod 600) file ~/.my.cnf, and put in it:

[client]
user = "you"
password = "your-password"

For the sake of conversation:

echo 'DROP DATABASE foo;' | mysql

2 Comments

although it is development environment, I really not much worried about password visible to others. I like your .my.cnf idea very much. +1 on the idea
For automatic script, second part of answer is the best. Great, thanks.
13

Another suitable way:

$ mysql -u you -p
<enter password>

>>> DROP DATABASE foo;

3 Comments

That's basically the same as what he's been doing.
Illustrating the possiblities on the command line does not warrant a down vote. He was using MySql Query Browser. Not quite the same.
But the problem statement clearly states that he wants a "shell command".
11

No need for mysqladmin:

just use mysql command line

mysql -u [username] -p[password] -e 'drop database db-name;'

This will send the drop command although I wouldn't pass the password this way as it'll be exposed to other users of the system via ps aux

Comments

3

MySQL has discontinued drop database command from mysql client shell. Need to use mysqladmin to drop a database.

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
It does not answer the question directly since the question is too vague.
1

You can remove database directly as:

$ mysqladmin -h [host] -u [user] -p drop [database_name]

[Enter Password]

Do you really want to drop the 'hairfree' database [y/N]: y

Comments

0
[root@host]# mysqladmin -u root -p drop [DB]

Enter password:******

1 Comment

I don't think this exposes the password to other users on the server, like many of the other answers do; instead the user is asked for it after the user hits enter. Possibly people haven't realised this when they downvoted what are possibly the best two answers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.