my.cnf文件
你已經學習了通過包裝程序mysqld_safe啟動MySQL 守護進程時可以在命令行中做哪些配置改變。不過,還有一種更方便的方法來調整許多MySQL 客戶端的啟動參數和行為,包括mysqladmin、myisamchk、myisampck、mysql、mysqlcheck、mysqld、mysqldump、mysqld_safe、mysql.server、mysqlhotcopy、mysqlimport和mysqlshow 。可以通過MySQL 的配置文件my.cnf 來維護這些修改。
啟動時,MySQL 將在一些目錄中查找my.cnf 文件,每個目錄將確定在其中聲明的參數的作用域。每個目錄和相對作用域如下所示。
•/etc/my.cnf(在Windows中為C:\my.cnf或Windows-sys-directory\my.ini)。全局配置文件。所有位於服務器中的MySQL 服務器守護進程將首先引用此文件。注意,如果選擇將配置文件放在windows系統目錄中,要使用.ini擴展名。還要注意,MySQL 的Windows分發包只支持全局配置文件。對於MysQL 4.1.5 及以後版本,使用Windows MySQL配置向導時,此文件將放在安裝目錄中。
•DATADIR/my.cnf。服務器特定的配置。此文件放在服務器安裝所引用的目錄中。這個配置文件有一個有些奇怪但很重要的特點,即使在運行時指定了新的數據目錄,它也只引用在配置時指定的數據目錄。注意,MySQL的Windows分發包不支持此特性。
•--default-extra-file=name。此文件由給出的文件名指定,包括絕對路徑。注意MySQL 的Windows 分發包不支持此特性。
•~/.my.cnf。用戶特定的配置。此文件放在用戶的主目錄中。注意,MySQL 的Windows 分發包不支持此特性。
應當了解,MysQL 在啟動時會嘗試從上述各個位置讀取配置文件。如果存在多個文件,後面讀取的參數將覆蓋之前設置的參數。雖然可以創建你自己的配置文件,不過應當基於5 個預配置的my.cnf 文件之一來創建,這5 個文件都由MySQL 分發包提供。這些模板位於INSTALL-DIR/support(在Windows中這些文件位於安裝目錄)。每個文件的作用如下所示:
•my-huge.cnf 用於高端產品服務器,包含1到2GB RAM,主要運行MySQL
•my-innodb-heavy-4G.ini 用於只有InnoDB的安裝,最大有4GB RAM ,支持大大查詢和低流量
•my-large.cnf 用於中等規模的產品服務器,包含大約512MB RAM,主要運行MySQL
•my-medium.cnf 用於低端產品服務器,包含很少內存(少於128MB)
•my-small.cnf 用於最低裝備的服務器,只有一點內存(少於64MB)
那麼此文件包含什麼內容?下面是my-large.cnf配置模板的部分內容:
# Example MySQL config file for large systems.
#
# This is for a large system with memory = 512M where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# MySQL-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:\MySQL\data) or
# ~/.my.cnf to set user-specific options.
#
# 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 = /tmp/MySQL.sock
# Here follows entrIEs for some specific programs
# The MySQL Server
[MySQLd]
port = 3306
socket = /tmp/MySQL.sock
skip-locking
key_buffer = 256M
max_allowed_packet = 1M
table_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
# Disable Federated by default
skip-federated
# Replication Master Server (default)
# binary logging is required for replication
log-bin=MySQL-bin
# 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
# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update = /path-to-dedicated-directory/hostname
# Uncomment the following if you are using BDB tables
#bdb_cache_size = 64M
#bdb_max_lock = 100000
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\MySQL\data/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:\MySQL\data/
#innodb_log_arch_dir = C:\MySQL\data/
# 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]
…注解:如果你的my-large.cnf文件與此類似,但變量前面有set-variable,請不要擔心。這是版本4.0.2之前在MySQL配置文件中設置變量的標准方法。雖然這在之後的版本中仍可用,但已經不鼓勵這樣用來。
看起來非常簡單,對吧?確實如此。配置文件可以總結為三點。
•注釋前面使用井號(# )。
•變量賦值與調用MySQLd_safe 時的賦值完全相同,只是前面沒有雙連字號。
•這些變量的上下文是通過在該節的前面加上用中括號括起的目標名來設置的。例如,如果希望調整MySQLdump 的默認行為,就可以在開始處加上:
[MySQLdump]然後加上相關的變量設置,如下:
quick
max_allowed_packet=16M此上下文會一直持續到下一個中括號設置。