How to change the default database for one user in MySQL

9,665

In the user's home directory (e.g. ~username) create a file named .my.cnf, or add to the existing .my.cnf file a line like :

database=databasename-you-want-by-default

Here are the contents of a typical .my.cnf:

[client]
host=localhost
user=root
password=root
database=mygotodb
Share:
9,665

Related videos on Youtube

Yitzchak
Author by

Yitzchak

Updated on September 18, 2022

Comments

  • Yitzchak
    Yitzchak over 1 year

    Though this might seem like a http://serverfault.com question at first glance I don't think it is.
    I have a mysql database on a server at work. Every time I log in to execute a query, I have to manually select the one database I want from the one database I have, which is a waste of time. Is there any way for me as an end user to set a default?

  • Yitzchak
    Yitzchak over 12 years
    Does the file have to be owned by mysql or can I just write it up myself?
  • Ram
    Ram over 12 years
    should be owned by the user, but so long as the user has read permissions that will suffice
  • AtliB
    AtliB over 2 years
    I was able to use just [client] database=mygotodb (that is, skipped the host/user/password)