MySQL Database

ID #242

How do I create a .my.cnf MySQL preference file?

Applies to: Grid System

A .my.cnf file (the period at the beginning of the filename is mandatory) is a file that contains some preset MySQL preferences for you.

If the file does not exist in your home directory (top level directory) of your account, then you can create one using a plain text editor like pico or vi. If you create or edit this file on your own computer and then upload it to the server, you'll need to use a plain text editor like Notepad or Homesite, not MS Word.

Because this file name begins with a period, some FTP programs may give you trouble seeing it in directory listings. At a shell prompt, you can always see it, if it is there, by typing

ls -la /

A basic .my.cnf file has the following lines. A pound sign '#' before any line means that line should be ignored by the MySQL interpreter.

[client]
user = DBUSERNAME
password = DBPASSWORD
host = DBSERVER

[mysql]
database = DBNAME

Where DBUSERNAME, DBPASSWORD, DBSERVER and
DBNAME should be replaced with your own correct database username, database hostname, database name and database password. You will not have any of these until you create a database through your Control Panel.

With a correctly setup .my.cnf file, you will be able to get a MySQL Monitor prompt by just typing

mysql

at your shell prompt without any of the arguments such as -h, -u, -p, and without having to explicitly type out your database name. The preferences also work with mysqldump and other mysql related shell commands.


Last update: 2010-10-03 16:50
Author: FAQ Admin
Revision: 1.2

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 4 (16 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
If permission to write to the .my.cnf file is denied, it is probably because the file is set read-only (chmod 400). To change that, get a shell prompt:

http://www.modwest.com/help/kb.phtml?cat=7&qid=116

and change permissions on the file to make it writable:

chmod 600 /.my.cnf

Added at: 2003-09-04 17:47

Comment of Anonymous:
Seems that if the password contains a # character, this won't work. Presumably this is because # is the comment character, and even within a password, is treated as such.

The only fix I found is to choose a new password.
Added at: 2008-07-30 23:15

Comment of Anonymous:
If your password has a special character in it, such as #, you can escape it using a \. So, if your password is #password#, your password line would look like this:
password=\#password\#
Added at: 2008-10-06 15:26

Comment of David:
The # escape didn't work for me - but the hint solved a headache ! Thanks. Just changing the password to other one withoud the octothorpe made the .my.cnf work as a charm.
Added at: 2011-10-07 14:15