0
0
MysqlHow-ToBeginner · 3 min read

Where is my.cnf in MySQL: Location and Usage Explained

The my.cnf file is the main configuration file for MySQL. It is usually located in /etc/mysql/my.cnf, /etc/my.cnf, or /usr/local/mysql/etc/my.cnf depending on your operating system and installation method.
📐

Syntax

The my.cnf file is a plain text file used to configure MySQL server settings. It contains sections like [mysqld] for server options and [client] for client options.

Common locations include:

  • /etc/mysql/my.cnf - typical on Debian/Ubuntu
  • /etc/my.cnf - common on RedHat/CentOS
  • /usr/local/mysql/etc/my.cnf - sometimes used on macOS or custom installs

You can also specify a custom config file when starting MySQL with --defaults-file=path/to/my.cnf.

ini
[mysqld]
port=3306
socket=/var/run/mysqld/mysqld.sock

[client]
port=3306
socket=/var/run/mysqld/mysqld.sock
💻

Example

This example shows how to find the active my.cnf file used by your MySQL server by running a command inside MySQL.

bash
mysql -u root -p -e "SHOW VARIABLES LIKE 'pid_file';"
Output
Variable_name Value pid_file /var/run/mysqld/mysqld.pid
⚠️

Common Pitfalls

Many users expect my.cnf to be in one fixed location, but MySQL checks multiple locations in order. If you edit the wrong file, changes won't apply.

Also, some installations use my.ini on Windows instead of my.cnf.

To avoid confusion, always check which config file MySQL is actually using.

ini
[Wrong way]
# Editing /etc/my.cnf but MySQL uses /etc/mysql/my.cnf

[Right way]
# Run <code>mysql --help | grep my.cnf</code> to see all config file locations MySQL checks
📊

Quick Reference

Operating SystemTypical my.cnf Location
Debian/Ubuntu/etc/mysql/my.cnf
RedHat/CentOS/etc/my.cnf
macOS (Homebrew)/usr/local/etc/my.cnf
Windowsmy.ini in MySQL installation folder

Key Takeaways

The my.cnf file configures MySQL server and client settings.
Common my.cnf locations vary by OS and installation method.
Use commands like mysql --help or SHOW VARIABLES to find the active config file.
Editing the wrong my.cnf file will not affect MySQL behavior.
Windows uses my.ini instead of my.cnf for configuration.