MySQL configuration files my.cnf and my.ini

1. my.cnf and my.ini

win system, the MySQL configuration file is my.ini

Other systems (Ubuntu, CentOS, macOS) MySQL configuration file is my.cnf

Two, the path of my.cnf and my.ini

2.1 Default path

MySQL's configuration file, my.cnf, may be located in several locations, depending on the installation method and operating system. The following are the default paths for some common my.cnf configuration files:

Default installation path
installation method default path
macOS installation using Homebrew /usr/local/etc/my.cnf
macOS is installed using the official installation package (DMG) /usr/local/mysql/my.cnf
Linux (such as Ubuntu, CentOS, etc.) /etc/mysql/my.cnf or /etc/my.cnf
Windows: my.ini file under the installation path

Note that these are common default paths, and actual paths may vary due to how you installed, custom configuration, or other factors.

2.2 Find my.cnf path

2.2.1, command line search configuration file

If you cannot find the my.cnf file in the above path, you can try to use the command line to find the location of the configuration file: 

mysql --help


The command will output some information, including Default options are read from the following files in the given order: and the path where my.cnf is located.

or use the command

mysql --help | grep "Default options" -A 1

2.2.2, multiple my.cnf configuration files

Using mysql --help found multiple my.cnf configuration files

  1. /etc/my.cnf
  2. /etc/mysql/my.cnf
  3. /opt/homebrew/Cellar/mysql-client/8.0.33_1/etc/my.cnf
  4. ~/.my.cnf

1. The order of action of multiple configuration files

When MySQL reads configuration files, it searches in the following order:

  1. /etc/my.cnf
  2. /etc/mysql/my.cnf
  3. /opt/homebrew/Cellar/mysql-client/8.0.33_1/etc/my.cnf
  4. ~/.my.cnf

Configuration files are located in one or more locations under these paths. MySQL will read the configuration files in the above order, and the later configuration files will override the same options in the previous configuration files.

2. The scope of action is different

  1. /etc/my.cnf or /etc/mysql/my.cnf is a system-level configuration file that takes effect for MySQL on the entire system. If you want to modify the global configuration, you can choose to modify it under one of the paths.
  2. /opt/homebrew/Cellar/mysql-client/8.0.33_1/etc/my.cnf is the path to the configuration file of the MySQL client installed by Homebrew.
  3. ~/.my.cnf represents the .my.cnf configuration file in the current user's home directory, which only takes effect for this user.

Notice:

mysql --help Although multiple my.cnf paths are displayed, these files do not necessarily exist.

2.2.3 What to do if my.cnf does not exist

mysql --help Although multiple my.cnf paths are displayed, these files do not necessarily exist.

When the my.cnf configuration file does not exist, it means that MySQL may be running with default configuration parameters.

1. Create a new my.cnffile

If you want to customize MySQL's configuration, create a new my.cnffile and place it in the appropriate location.

like:

sudo vim /etc/my.cnf

In the opened editor, you can add the required configuration parameters, then save the file and restart the MySQL service for the configuration to take effect.

Note that if /etc/my.cnfthe file does not exist, you can create a new one instead, or use another available location for configuration. Make sure to back up your old configuration file before editing so you can revert to your previous configuration if something goes wrong.

2. Initialize my.cnffile configuration

In the newly created /etc/my.cnffile, you can add the following common initialization data:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# 设置字符集
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

# 设置服务器ID(如果是复制集群,确保每个节点的ID唯一)
server-id=1

# 启用二进制日志
log-bin=mysql-bin

# 指定错误日志文件路径
log-error=/var/log/mysql/error.log

This is just an example, and the specific configuration parameters depend on your actual needs. You can add, modify or delete configuration parameters according to your requirements. After completing the modification, save the file and restart the MySQL service to make the configuration take effect.


 

Guess you like

Origin blog.csdn.net/qq_39208536/article/details/132333029