Wednesday, July 27, 2011

mysql run query from command line

I've seen many times that a developer login mysql just to run a single/simple query. This is usually what he would do:
mysql -uuser -ppassword db;
mysql> select * from users;

Instead, we can run a mysql query from command line by using option -e, --execute=name. it means execute command and quit. For example:

mysql -uuser -ppassword db -e "select * from users";

or

mysql -uuser -ppassword -e "select * from db.users";

Usually when we want to install some open source PHP software, we need to create the database first. We can create a mysql database from command line as well:

mysql -uuser -ppassword -e "create database db";

No comments: