Sei sulla pagina 1di 33

Server Configuration

Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Objectives

After completing this lesson, you should be able to:


• Set up MySQL server configuration files
• Explain the purpose of dynamic server variables
• Review the server status variables that are available
• Configure operational characteristics of the MySQL server
by using the SQL modes
• Describe the available log files
• Explain binary logging
• Configure audit logging with MySQL Enterprise Audit

4-2 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
MySQL Configuration Options

Precompiled
Options

Startup
Configuration

Command-Line Configuration
Options File Options

4-3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Reasons to Use Option Files

• No need to specify options each time you start the server


• Lower chance for error when you enter options
• Opportunity to quickly review a single file to view the
server configuration

4-4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Option File Groups

Option File

All Clients All Servers


• [client] • [server]

Client-Specific Server-Specific

• [mysql] • [mysqld]
• [mysqldump] • [mysqld_safe]
• ... • ...

4-5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Writing an Option File

A brief example of groups in an option file:


[client]
host = myhost.example.com
compress

[mysql]
show-warnings

4-6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Option File Locations

• MySQL server looks for files in standard locations.


• Standard files are different for Linux and Windows:
– In Linux, use the my.cnf file.
– In Windows, use the my.ini file.
• There is no single my.cnf or my.ini file. The server
might read multiple my.cnf or my.ini files from different
locations.
• You can view option file lookup locations, the order in
which they are read, and groups with options:
shell> mysql –-help
... 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
The following groups are read: mysql client ...

4-7 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Sample Option Files

• The MySQL installation provides a sample configuration


file called my-default.cnf.
– The my-default.cnf file is not read by MySQL programs.
• The install process copies the my-default.cnf sample
file to /etc/my.cnf.
– If /etc/my.cnf already exists, it copies it to /etc/my-
new.cnf instead.
• Specify new options in the file to replace, add, or ignore
the standard options.
– Must be the first option on the command line
--defaults-file=<file_name>
--defaults-extra-file=<file_name>
--no-defaults

4-9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Displaying Options from Option Files

Display options per group from the command line.


• Examples of [mysql] and [client] groups:
shell> my_print_defaults mysql client
--user=myusername
--password=secret
--host=localhost
--port=3306
--character-set-server=latin1
• Or (for the same output):
mysql --print-defaults mysql client

4 - 10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Obscuring Authentication Options

Use mysql_config_editor to create encrypted option files.


• Store user, password, and host options in a dedicated
option file:
– .mylogin.cnf in the current user’s home directory
– To specify an alternative file name, set the
MYSQL_TEST_LOGIN_FILE environment variable.
• The .mylogin.cnf file contains login paths.
– They are similar to option groups.
– Each login path contains authentication information for a
single identity.
– Clients refer to a login path with the --login-path
command-line option:
mysql --login-path=admin

4 - 11 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Login Paths

• To create a login path:


mysql_config_editor set
--login-path=<login-path> --user=<user>
--password --host=<hostname>
• To view a single login path in clear text:
mysql_config_editor print
--login-path=<login-path>
• To view all login paths in clear text:
mysql_config_editor print --all
• To remove a login path:
mysql_config_editor remove
--login-path=<login-path>
• The default login path name is client. It is read by all
standard clients.

4 - 12 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Server System Variables

• The MySQL server maintains many server system


variables that indicate how it is configured.
• Each system variable has a default value.
• You can set variables at server startup by doing either of
the following:
– Use options on the command line.
– Use an option file.
• You can refer to system variable values in expressions.
• You can view the values that a server uses.

4 - 13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Dynamic System Variables

• You can change most variables dynamically while the


server is running.
– In this way, you can modify the operation of the server
without having to stop or restart it.
• Variable categories:
GLOBAL GLOBAL and SESSION
SESSION
Affects
Affects all sessions new Affects current session
session

Requires SUPER privilege No privilege required

Set variables at run time Set variables at run time


•SET GLOBAL <variable> •SET SESSION <variable>
•SET @@global.<variable> •SET @@session.<variable>

4 - 14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Displaying Dynamic System Variables

• List all available variables and their values:


SHOW [GLOBAL|SESSION] VARIABLES;

• List specific variable values:


mysql> SHOW VARIABLES LIKE 'bulk%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| bulk_insert_buffer_size | 8388608 |
+-------------------------+---------+
• Set a new value and then list:
mysql> SET bulk_insert_buffer_size=4000000;
mysql> SHOW VARIABLES LIKE 'bulk%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| bulk_insert_buffer_size | 4000000 |
+-------------------------+---------+

4 - 15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Structured System Variables

• A structured variable differs from a regular system variable


in two ways:
– It is a structure with components that specify closely related
server parameters.
– There can be several instances of a given type of structured
variable. Each one has a different name and refers to a
different resource maintained by the server.
• MySQL supports a structured variable to govern the
operation of key caches.

4 - 16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Server Status Variables

• Use the SHOW STATUS statement to assess the health of a


system.
• Choose from two categories (similar to dynamic variables):

GLOBAL SESSION

Status for all Status for current


connections connection only

Display global status Display session status


• SHOW GLOBAL STATUS • SHOW SESSION STATUS
• SHOW GLOBAL STATUS • SHOW SESSION STATUS
LIKE <variable> LIKE <variable>

4 - 17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


SQL Modes

Configure many server operation characteristics by setting the


SQL mode:
• Specify how strict or forgiving MySQL is about accepting
input data.
• Set compatibility with other database systems.
• Control query processing.
– Enable or disable behavior relating to SQL conformance.
• Override SQL “empty” default mode.
– Empty mode does not enable restrictions or conformance
behaviors.
The default SQL mode is NO_ENGINE_SUBSTITUTION.
• The default configuration file adds STRICT_TRANS_TABLES.

4 - 18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Setting the SQL Mode

• Set at startup from the command line:


shell> mysqld --sql-mode=<mode_value>

• Set within an option file:


[mysqld]
sql-mode=IGNORE_SPACE

• SET statement
– Within mysql, after startup:
SET [SESSION|GLOBAL] sql_mode=<mode_value>

– Clear the current SQL mode:


SET sql_mode=''

4 - 19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Common SQL Modes

STRICT_TRANS_TABLES, • Recommended for most installations


• Sets “strict mode,” restricting acceptable database input
STRICT_ALL_TABLES • Bad values are treated as erroneous.

• Sets “strict mode,” restricting acceptable input data values like


TRADITIONAL other database servers
• Restricts user account creation

• Sets double quote (") as an identifier instead of a string-


ANSI_QUOTES quoting character

• Tells server to ignore spaces after function names


IGNORE_SPACE • Allows space between name and the parenthesis
• Sets function names as reserved words

• Causes division-by-zero (when inserting table data) to


ERROR_FOR_DIVISION_BY_ZERO
produce a warning or an error

• Composite mode; allows standard SQL behavior


ANSI • More “ANSI-like”

• Disables automatic substitution of the default storage engine


NO_ENGINE_SUBSTITUTION when the chosen engine for CREATE TABLE or ALTER
TABLE is unavailable

4 - 20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Log Files

• MySQL has different log files to record server activity:


– Error
– General Query
– Slow Query
– Binary
– Enterprise Audit
• Log files:
– Can use large amounts of disk space
– Can be stored in files
– Can be stored in tables
— General and slow query logs only
– Are written in text format
— Except binary log

4 - 21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Log File Usage Matrix

File Name
Log File Options Programs
Table Name
Error --log-error host_name.err N/A

General --general_log host_name.log N/A


general_log
Slow Query --slow_query_log host_name-slow. log mysqldumpslow
--long_query_time
slow_log
Binary --log-bin host_name-bin.000001 mysqlbinlog
--expire-logs-days

Audit --audit_log audit.log N/A


--audit_log_file
...

4 - 22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Binary Logging

Log shipping system:

MASTER
Binary Log Rotation
Events

Server
Restart

FLUSH Maximum
LOGS Log Size

SLAVE
Execution of Master
Binary Log Binary Log Files

4 - 24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Binary Logging Formats

Choose from two different types of logging:


Statement-Based Row-Based

Size of log files Small Large

Replication limitations Not all statements can be All statements can be


replicated. replicated.

Master/Slave MySQL Slave can be a newer Slave must be an identical


versions version with a different row version and row structure.
structure.

Locking INSERT and SELECT INSERT, UPDATE, and


require a greater number DELETE require fewer
of row locks. locks on slaves.

Point-in-time recovery Yes Yes (more difficult due to


binary format of log
events)

4 - 25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


List Binary Log Files

• Use the SHOW BINARY LOGS statement to:


– List current log files and file size
mysql> SHOW BINARY LOGS;
+---------------+-----------+
| Log_name | File_size |
+---------------+-----------+
| binlog.000015 | 724935 |
| binlog.000016 | 733481 |
+---------------+-----------+
• Use the SHOW MASTER STATUS statement to:
– Show the master status for the next event
mysql> SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000016 | 733481 | world_innodb | manual,mysql |
+---------------+----------+--------------+------------------+

4 - 26 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


View Binary Log Contents

• Cannot be viewed with normal text viewers:


– Logs are stored in compact binary format.
• Use the mysqlbinlog utility to:
– Convert binary logs to SQL text format
• View the data in standard out:
shell> mysqlbinlog host-bin.000001 host-bin.000002

• Redirect output to more:


shell> mysqlbinlog host-bin.000001 | more

4 - 27 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Deleting Binary Logs

• By default, old log files are not deleted.


• Delete logs based on age:
SET GLOBAL expire_logs_days = 7;
…or…
PURGE BINARY LOGS BEFORE now() - INTERVAL 3 day;

• Delete logs based on file name:


PURGE BINARY LOGS TO 'mysql-bin.000010';

4 - 29 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Configuring Enterprise Audit

• Implemented using the audit_log server plugin


– Available with Enterprise Edition subscriptions
• Policy-based logging:
– Is set with the audit_log_policy option
– Offers the choice of logging ALL, NONE, LOGINS, or
QUERIES
– Defaults to ALL
• Produces an audit record of server activity in a log file
– Contents depend on policy, and can include:
— A record of errors that occur on the system
— When clients connect and disconnect
— What actions they perform while connected
— Which databases and tables they access

4 - 30 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Audit Log File

The audit log file is written as XML, using UTF-8.


• The root element is <AUDIT>.
• The closing </AUDIT> tag of the root element is written
when the plugin terminates.
– The tag is not present in the file while the audit log plugin is
active.
• Each audit entry is an <AUDIT_RECORD /> element:

<AUDIT_RECORD TIMESTAMP="2012-10-12T09:35:15"
NAME="Connect" CONNECTION_ID="4" STATUS="0"
USER="root" PRIV_USER="root" OS_LOGIN="" PROXY_USER=""
HOST="localhost" IP="127.0.0.1" DB=""/>
<AUDIT_RECORD TIMESTAMP="2012-10-12T09:38:33"
NAME="Query" CONNECTION_ID="4" STATUS="0"
SQLTEXT="INSERT INTO tbl VALUES(1, 2)"/>

4 - 31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Log File Rotation

• Log files take up space over time. Periodically back up and


remove old log files and recommence logging to new log
files.
– Use caution if you are using binary logs for replication.
• After backup, flush the logs. Flushing the logs:
– Creates new binary log files
– Closes and re-opens the general and slow query log files
— To create new logs, you must rename the current log files
before flushing.
• Schedule log file rotation at regular intervals:
– Create your own script or use the provided mysql-log-
rotate script (RHEL only)

4 - 32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Set up MySQL server configuration files
• Explain the purpose of dynamic server variables
• Review the server status variables that are available
• Configure operational characteristics of the MySQL server
by using the SQL modes
• Describe the available log files
• Explain binary logging
• Configure audit logging with MySQL Enterprise Audit

4 - 33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Practice 4-1 Overview:
Quiz
In this quiz, you answer questions about MySQL server
configuration.

4 - 34 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Practice 4-2 Overview:
Editing and Creating a Configuration File
In this practice, you edit the my.cnf configuration file and
create a new custom configuration file.

4 - 35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.


Practice 4-3 Overview:
Additional Practice – Server Configuration
In this practice, you use the knowledge from this and previous
lessons to complete configuration-related exercises.

4 - 36 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Potrebbero piacerti anche