如何知道mysql my.cnf的位置

是否有一个MySQL命令来定位my.cnfconfiguration文件,类似于PHP的phpinfo()定位它的php.ini

没有内部的MySQL命令来追踪这个,这太抽象了。 该文件可能位于5个(或更多?)位置,并且它们都将是有效的,因为它们会加载级联。

  • /etc/my.cnf中
  • /etc/mysql/my.cnf
  • $ MYSQL_HOME / my.cnf中
  • [DATADIR] /my.cnf
  • 〜/ .my.cnf

这些是MySQL查看的默认位置。 如果它find多个,它将​​加载它们中的每一个值并相互覆盖(按照列出的顺序,我认为)。 此外,– --defaults-file参数可以覆盖整个事物,所以…基本上,这是一个巨大的痛苦。

但是由于它很混乱,所以很有可能只是在/etc/my.cnf中。

(如果您只想查看值: SHOW VARIABLES ,但您需要权限才能这样做)。

实际上,您可以让MySQL向您显示searchmy.cnf(或Windows上的my.ini)的所有位置的列表。 这不是一个SQL命令。 相反,执行:

 $ mysqld --help --verbose 

在第一行中,您将find一个包含所有my.cnf位置列表的消息。 在我的机器上是:

 Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

或者,在Windows上:

 Default options are read from the following files in the given order: C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\Program Files\MySQL\MySQL Server 5.5\my.ini C:\Program Files\MySQL\MySQL Server 5.5\my.cnf 

但是请注意, 这可能是在任何这些位置都没有my.cnf文件 。 所以,你可以自己创build文件 – 使用MySQL发行版提供的示例configuration文件之一(在Linux上 – 请参阅/usr/share/mysql/*.cnf文件,并使用适合您的 – 将其复制到/etc/my.cnf然后根据需要修改)。

另外请注意, 还有一个命令行选项--defaults-file可以定义my.cnf或my.ini文件的自定义path。 例如,Windows上的MySQL 5.5就是这种情况 – 它指向数据目录中的my.ini文件,通常不会使用mysqld --help --verbose列出。 在Windows上 – 查看服务属性以查明这是否属于您。

最后,检查http://dev.mysql.com/doc/refman/5.5/en/option-files.html – 在这里有更详细的描述。

编辑2017 – MySQL 5.7+

在MySQL 5.7中使用:

 mysqladmin --help 

你总是可以在terminal上运行查找。

 find / -name my.cnf 

您可以使用 :

 locate my.cnf whereis my.cnf find . -name my.cnf 

这可能工作:

 strace mysql ";" 2>&1 | grep cnf 

在我的机器上这个输出:

 stat64("/etc/my.cnf", 0xbf9faafc) = -1 ENOENT (No such file or directory) stat64("/etc/mysql/my.cnf", {st_mode=S_IFREG|0644, st_size=4271, ...}) = 0 open("/etc/mysql/my.cnf", O_RDONLY|O_LARGEFILE) = 3 read(3, "# /etc/mysql/my.cnf: The global "..., 4096) = 4096 stat64("/home/xxxxx/.my.cnf", 0xbf9faafc) = -1 ENOENT (No such file or directory) 

所以它看起来像/etc/mysql/my.cnf是因为它stat64()和read()成功。

默认情况下,mysql首先在/ etc文件夹中searchmy.cnf。 如果这个文件夹里没有/etc/my.cnf文件,我build议你在这个文件夹中创build一个新文件夹( https://dev.mysql.com/doc/refman/5.6/en/option -files.html )。

您还可以search由您的mysql安装提供的现有my.cnf。 您可以启动以下命令

 sudo find / -name "*.cnf" 

你可以在myisam表中使用下面的configuration文件,并且不需要innodb mysql支持(从mac os上的mysql的port安装到maverick)。 请validation此configuration文件中的每个命令。

 # Example MySQL config file for large systems. # # This is for a large system with memory = 512M where the system runs mainly # MySQL. # # MySQL programs look for option files in a set of # locations which depend on the deployment platform. # You can copy this option file to one of those # locations. For information about these locations, see: # http://dev.mysql.com/doc/mysql/en/option-files.html # # In this file, you can use all long options that a program supports. # If you want to know which options a program supports, run the program # with the "--help" option. # The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /opt/local/var/run/mysql5/mysqld.sock # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /opt/local/var/run/mysql5/mysqld.sock skip-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 8 # Don't listen on a TCP/IP port at all. This can be a security enhancement, # if all processes that need to connect to mysqld run on the same host. # All interaction with mysqld must be made via Unix sockets or named pipes. # Note that using this option without enabling named pipes on Windows # (via the "enable-named-pipe" option) will render mysqld useless! # #skip-networking # Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin # binary logging format - mixed recommended binlog_format=mixed # required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1 # Replication Slave (comment out master section to use this) # # To configure this host as a replication slave, you can choose between # two methods : # # 1) Use the CHANGE MASTER TO command (fully described in our manual) - # the syntax is: # # CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>, # MASTER_USER=<user>, MASTER_PASSWORD=<password> ; # # where you replace <host>, <user>, <password> by quoted strings and # <port> by the master's port number (3306 by default). # # Example: # # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306, # MASTER_USER='joe', MASTER_PASSWORD='secret'; # # OR # # 2) Set the variables below. However, in case you choose this method, then # start replication for the first time (even unsuccessfully, for example # if you mistyped the password in master-password and the slave fails to # connect), the slave will create a master.info file, and any later # change in this file to the variables' values below will be ignored and # overridden by the content of the master.info file, unless you shutdown # the slave server, delete master.info and restart the slaver server. # For that reason, you may want to leave the lines below untouched # (commented) and instead use CHANGE MASTER TO (see above) # # required unique id between 2 and 2^32 - 1 # (and different from the master) # defaults to 2 if master-host is set # but will not function as a slave if omitted #server-id = 2 # # The replication master for this slave - required #master-host = <hostname> # # The username the slave will use for authentication when connecting # to the master - required #master-user = <username> # # The password the slave will authenticate with when connecting to # the master - required #master-password = <password> # # The port the master is listening on. # optional - defaults to 3306 #master-port = <port> # # binary logging - not required for slaves, but recommended #log-bin=mysql-bin # Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /opt/local/var/db/mysql5 #innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /opt/local/var/db/mysql5 # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high #innodb_buffer_pool_size = 256M #innodb_additional_mem_pool_size = 20M # Set .._log_file_size to 25 % of buffer pool size #innodb_log_file_size = 64M #innodb_log_buffer_size = 8M #innodb_flush_log_at_trx_commit = 1 #innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout 

正如konyak所指出的那样,你可以通过运行mysqladmin --help来得到mysql将寻找my.cnf文件的地方列表。 由于这是相当详细的,你可以快速的到达你关心的部分:

 $ mysqladmin --help | grep -A1 'Default options' 

这会给你输出类似于:

 Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf 

取决于你如何安装mysql,可能这些文件都不存在。 你可以把它们给cat看,看看你的configuration是如何构build的,如果需要的话在你喜欢的位置创build你自己的my.cnf

如果您在Mac上使用Homebrew,请使用

酿造信息mysql

你会看到类似的东西

 $ brew info mysql mysql: stable 5.6.13 (bottled) http://dev.mysql.com/doc/refman/5.6/en/ Conflicts with: mariadb, mysql-cluster, percona-server /usr/local/Cellar/mysql/5.6.13 (9381 files, 354M) * 

最后一行是每个MySQL文档的INSTALLERDIR

我不知道你是如何在你的Linux环境下设置MySQL的,但你有没有检查过?

  • /etc/my.cnf中

尝试运行mysqld --help --verbose | grep my.cnf | tr " " "\n" mysqld --help --verbose | grep my.cnf | tr " " "\n"

输出将是类似的东西

 /etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf 

另一个select是使用whereis命令。

例如wheremy my.cnf

发现我的使用

 mysqld --help --verbose | grep my.cnf 

所有伟大的build议,在我的情况下,我没有find它的任何位置,但在/ usr / share / mysql中,我有一个RHEL虚拟机,我安装了mysql5.5

对于Ubuntu 16:/etc/mysql/mysql.conf.d/mysqld.cnf

我在ubuntu上安装了apache, php and mysql xampp包。 my.cnf文件位于/opt/lampp/etc/文件夹中。 希望它会帮助别人。

只回答MySQL Workbench用户,

在这里输入图像说明

如果你正在使用MAMP,请访问Templates> MySQL(my.cnf)> [version]

如果您正在无窗运行MAMP,则可能需要使用“自定义”button来自定义工具栏。

MAMP PRO模板菜单