MySQL Configuration Options
You can specify startup options on the command line when you invoke the server (or client), or in an option file. MySQL client programs look for option files at startup and use appropriate options.
By default, the server uses precompiled values for its configuration variables when it runs. However, if the default values are not suitable for your environment, add runtime options to tell the server to use different values to:
o Specify the locations of important directories and files
o Control which log files the server writes
o Override the server’s built-in values for performance-related variables (that is, to control the maximum number of simultaneous connections and the sizes of buffers and caches)
o Enable or disable precompiled storage engines at server startup You can specify runtime options when the server is started (to change its configuration and behavior) by using command-line options or an option file, or by using a combination of both. Command-line options take precedence over any settings in an option file.
To find out what options your server supports, execute the following at a shell prompt:
mysqld --verbose --help
Note: The preceding command provides information. It does not start the MySQL server.
Option File Groups
Examples of groups include:
o [client]: Used for specifying options that apply to all client programs. A common use for the [client] group is to specify connection parameters, because typically connections are made to the same server no matter which client program is used.
o [mysql] and [mysqldump]: Used for specifying options that apply to mysql and mysqldump clients, respectively. Other client options can also be specified individually.
o [server]: Used for specifying options that apply to both the mysqld and mysqld_safe server programs
o [mysqld], [mysqld-5.6], and [mysqld_safe]: Used for specifying options to different server versions or startup methods
Write Option File
To create or modify an option file, the end user must have write permission for it. The server itself needs only read access; it reads option files but does not create or modify them. To write an option in an option file:
o Use the long option format, as used on the command line, but omit the leading dashes.
o If an option takes a value, spaces are allowed around the equal ( = ) sign. This is not true for options specified on the command line.
In the example in the slide, note the following:
o [client]: Options in this group apply to all standard clients.
-host: Specifies the server host name
-compress: Directs the client/server protocol to use compression for traffic sent over the network.
o [mysql]: Options in this group apply only to the mysql client.
-show-warnings: Tells MySQL to show any current warnings after each statement
o The mysql client uses options from both the [client] and [mysql] groups, so it would use all three options shown.
Option File Locations
The standard option files are as follows:
The file /etc/my.cnf serves as a global option file used by all users. You can create a user-specific option file named .my.cnf in the user’s home directory. If the MYSQL_HOME environment variable is set, it searches for the $MYSQL_HOME/my.cnf file.
Programs look for option files in the following order: my.ini and my.cnf in the Windows C:\ directory, then the C:\Windows (or C:\WinNT) directory. However, because the Windows installation wizard places the configuration file in the directory C:\Program Files\MySQL\MySQL Server <version number>, the server also searches this directory in Windows.
o MySQL command-line programs search for option files in the MySQL installation directory.
To view the locations of options file and the order in which they are read, use the mysql command-line client with the --help option.
The following command filters the output of the command:
[root@songyb usr]# mysqld --help --verbose 2> /dev/null | grep -A1 "Default options"
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
Startup Options in an Option File
To specify server options in an option file, indicate the specific options under the [mysqld] or [server] groups.
o Logging: You can enable logging for your server by turning on the types of logs required. The following options turn on the general query log, the binary log, and the slow query log:
o Default Storage Engine: You can specify a default storage engine different from InnoDB by using the --default-storage-engine option.
o System Variables: You can customize your server by setting server system variable values. For example, to increase the maximum allowed number of client connections and to increase the number of the InnoDB buffer pools from their defaults, set the following variables:
o Shared Memory: Not enabled by default on Windows. You can turn on shared memory by using the shared-memory option.
o Named Pipes: To turn on named-pipe support, use the enable-named-pipe option.
Sample Option Files
o Linux: The my-default.cnf sample option file is in /usr/share/mysql for RPM installations or the share directory under the MySQL installation directory for TAR file installations.
o Windows: The my-default.ini sample option file and my.ini are located in the MySQL installation directory.
Before you change any of the default options, make sure that you fully understand the effects that the options have on server operation.
o --defaults-file=<file_name>: Use the option file at the specified location.
o --defaults-extra-file=<file_name>: Use an additional option file at the specified location.
o --no-defaults: Ignore all option files.
For example, to use only the /etc/my-opts.cnf file and ignore the standard option files, invoke the program like this:
shell> mysql --defaults-file=/etc/my-opts.cnf
If an option is specified multiple times, either in the same option file or in multiple option files, the option value that occurs last takes precedence.
For more information about using option files, see the MySQL Reference Manual: http://dev.mysql.com/doc/mysql/en/option-files.html.
Displaying Options from Option Files
You can view which options are used by programs that read the specified option groups by executing the mysql client with the --print-defaults option or by using the my_print_defaults utility. The output consists of options, one per line, in the form that they would be specified on the command line. This output varies according to your option file settings. my_print_defaults accepts the following options:
o --help, -o : Display a help message and exit.
o --config-file=<file_name> , --defaults-file=<file_name>, -c <file_name>: Read only the given option file.
o --debug=<debug_options>, -# <debug_options>: Write a debugging log.
o --defaults-extra-file=<file_name>, --extra-file=<file_name>, -e <file_name>: Read this option file after the global option file, but (on Linux) before the user option file.
o --defaults-group-suffix=<suffix>, -g <suffix>: Read groups with this suffix.
o --no-defaults, -n: Return an empty string.
o --verbose, -v: Verbose mode. Print more information about what the program does.
o --version, -V: Display version information and exit.
Obscuring Authentication Options
The mysql_config_editor utility enables you to store authentication credentials in an encrypted login file named .mylogin.cnf. The file location is the current user’s home directory on Linux and UNIX, and the %APPDATA%\MySQL directory on Windows.
Here is an example:
user = root
password = oracle
host = 127.0.0.1
If you invoke mysql_config_editor without using the --login-path option, it uses the [client] login path.
shell> mysql_config_editor set --user=root --password
Enter password: oracle
Server System Variables
mysqld --verbose --help
mysqld --no-defaults --verbose --help
SHOW GLOBAL VARIABLES;
Dynamic System Variables
MySQL maintains two scopes that contain system variables. GLOBAL variables affect the overall operation of the server. SESSION variables affect its operation for individual client connections. Variables exist in one or the other scope, or in both. Examples of variables and their scope include:
o Global only: key_buffer_size, query_cache_size
o Both global and session: sort_buffer_size, max_join_size
o Session only: timestamp, error_count When you change variable values, the following points apply:
o Setting a session variable requires no special privilege, but a client can change only its own session variables, not those of any other client.
o LOCAL and @@local are synonyms for SESSION and @@session.
o If you do not specify GLOBAL or SESSION, SET changes the session variable if it exists and produces an error if it does not.
Displaying Dynamic System Variables
SHOW GLOBAL|SESSION VARIABLES;
SHOW VARIABLES LIKE 'bulk%';
mysql> SHOW VARIABLES LIKE 'bulk%';
| Variable_name | Value |
| bulk_insert_buffer_size | 8388608 |
1 row in set (0.00 sec)
mysql> SET bulk_insert_buffer_size=6000000
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'bulk%';
| Variable_name | Value |
| bulk_insert_buffer_size | 6000000 |
1 row in set (0.00 sec)
Structured System Variables
MySQL supports one structured variable type, which specifies parameters governing the operation of key caches. A key cache structured variable has these components:
To refer to a component of a structured variable instance, you can use a compound name:
Server Status Variables
SHOW GLOBAL STATUS;
The SQL mode consists of optional values that control some aspect of query processing. When you set the SQL mode appropriately, a client can have some control over the following:
o Input data: SQL modes can be used tell the server how strict or forgiving to be about accepting input data.
o Standard SQL conformance: SQL modes can be used to enable or disable behaviors relating to standard SQL conformance.
o Compatibility: SQL modes can be used to provide better compatibility with other database systems.
Here are some SET examples:
o Set the SQL mode by using a single mode value:
SET sql_mode = ANSI_QUOTES;
SET sql_mode = 'TRADITIONAL';
o Set the SQL mode by using multiple mode names:
sql_mode = 'IGNORE_SPACE,ANSI_QUOTES,NO_ENGINE_SUBSTITUTION';
Check the current sql_mode setting by using this SELECT statement:
Common SQL Modles
o STRICT_TRANS_TABLES, STRICT_ALL_TABLES: Without these modes, MySQL is forgiving with values that are missing, out of range, or malformed. Enabling STRICT_TRANS_TABLES sets “strict mode” for transactional tables; it is also enabled in the default my.cnf file. Enabling STRICT_ALL_TABLES sets strict mode for all tables.
o TRADITIONAL: Enable this SQL mode to enforce restrictions on input data values that are similar to those of other database servers. With this mode, using the GRANT statement to create users requires that you specify a password.
o IGNORE_SPACE: By default, you must invoke functions with no space between the function name and the following parenthesis. Enabling this mode allows such spaces, and causes function names to be reserved words.
o ERROR_FOR_DIVISION_BY_ZERO: By default, division by zero produces a result of NULL. A division by zero when inserting data with this mode enabled causes a warning, or an error in strict mode.
o ANSI: Use this composite mode to cause the MySQL server to be more “ANSI-like.” That is, it enables behaviors that are more like standard SQL, such as ANSI_QUOTES and PIPES_AS_CONCAT.
o NO_ENGINE_SUBSTITUTION: When you specify an unavailable storage engine while creating or altering a table, MySQL substitutes the default storage engine unless this mode is enabled. This is the default SQL mode.
Record information about the SQL statements processed by the server:
o Error log: Diagnostic messages regarding startups, shutdowns, and abnormal conditions
o General query log: All statements that the server receives from clients
o Slow query log: Queries that take a long time to execute
o Binary log: Statements that modify data
o Audit log: Policy-based audit for Enterprise edition
For more information about log files, see the MySQL Reference Manual:
LOG Files Usage Matrix
The server creates all the log files in the data directory and sets the file name to the current hostname if you do not set another path name. You can enable these log files by starting the server with the corresponding options (on the command line or in an option file without the preceding ‘--’).
o Error log: Set --log-error=<file_name> to log errors to the given file. The mysqld_safe script creates the error log and starts the server with its output redirected to the error log.
o General query log: Set --general_log_file=<file_name> to log queries.The global general_log and general_log_file server variables provide runtime control over the general query log. Set general_log to 0 (or OFF) to disable the log, or to 1 (or ON) to enable it.
o Slow query log: Set --slow_query_log_file=<file_name> to provide runtime control over the slow query log. Set slow_query_log to 0 to disable the log, or to 1 to enable it. If a log file is already open, it is closed and the new file is opened.
o Binary log: Set --log-bin to enable binary logging. The server uses the option value as a base name, adding an increasing sequential numeric suffix to the base name as it creates new log files. These log files are stored in binary format rather than text format. For detailed information on the options available for all of the above log types, see the MySQL Reference Manual: http://dev.mysql.com/doc/mysql/en/server-logs.html
o Audit log: The audit log is provided as an Enterprise Edition plugin that, when loaded, you can use to log events into the file specified by the audit_log_file option. Auditing constantly writes to the audit log until you remove the plugin, or you turn off the auditing with the audit_log_policy=NONE option setting. You can prevent the removal of the plugin by using audit_log=FORCE_PLUS_PERMANENT as an option when the server is started. For detailed information about all available audit plugin options, see the MySQL Reference Manual: http://dev.mysql.com/doc/mysql/en/audit-log-plugin-options-variables.html
The binary log rotates when one of the following events occurs:
o The MySQL server is restarted.
o The maximum allowed size is reached (max_binlog_size).
o A FLUSH LOGS SQL command is issued.
The binary log is independent of the storage engine. MySQL replication works regardless of the storage engine that is being used (that is, InnoDB or MyISAM).
Binary Logging Formats
Statement-based binary logging:
o Contains the actual SQL statements
o Includes both DDL (CREATE, DROP, and so on) and DML (UPDATE, DELETE, and so on) statements
o The relatively small files save disk space and network bandwidth.
o Not all replicated statements replay correctly on a remote machine.
o Requires that replicated tables and columns be identical (or compatible with several restrictions) on both master and slave
Row-based binary logging:
o Indicates how individual table rows are affected
o Replays all statements correctly, even for changes caused by features that do not replicate correctly when using statement-based logging
Set the format as follows: SET [GLOBAL|SESSION] BINLOG_FORMAT=[row|statement|mixed|default];
Note: Use the mixed option to cause MySQL to pick the most appropriate format for individual events. It generally uses the statement-based binary log, but it reverts to the row-based replication when required.
List Binary Log files
SHOW BINARY LOGS;
SHOW MASTER STATUS;
View Binary Log Contents
Deleting Binary Logs
SET GLOBAL expire_logs_days = 7;
PURGE BINARY LOGS BEFORE now() - INTERVAL 3 day;
PURGE BINARY LOGS TO 'host-bin.000001';
You can also configure expire_logs_days in the option file:
Configuring Enterprise Audit
To install the audit_log plugin, use the INSTALL PLUGIN syntax, as in the following example:
INSTALL PLUGIN audit_log SONAME 'audit_log.so';
Alternatively, set the plugin-load option at server startup:
By default, loading the plugin enables logging. Setting the option audit-log to OFF disables logging. To prevent the plugin from being removed at run time, set the following option:
The log file is named audit.log, and by default is in the server data directory. To change the name or location of the file, set the audit_log_file system variable at server startup.
Audit Log File
The TIMESTAMP of each audit record is in UTC.
If you are using a Red Hat Enterprise-based operating system such as Oracle Linux, use the mysql-log-rotate script in /usr/share/mysql/, provided as part of the RPM installation.
After backing up all the log files, flush the logs to force the MySQL server to start writing to new log files. Execute the FLUSH LOGS SQL statement, or run a suitable client command. Flushing the logs causes binary logging to recommence with the next file in the sequence. With the general and slow query logs however, flushing merely closes the log files, reopens them and then recommences logging under the same file name. To start new logs, rename the existing log files before flushing. Example:
# cd mysql-data-directory
# mv mysql.log mysql.old
# mv mysql-slow.log mysql-slow.old
# mysqladmin flush-logs