You are previewing MySQL High Availability, 2nd Edition.
O'Reilly logo
MySQL High Availability, 2nd Edition

Book Description

Server bottlenecks and failures are a fact of life in any database deployment, but they don’t have to bring everything to a halt. This practical book explains replication, cluster, and monitoring features that can help protect your MySQL system from outages, whether it’s running on hardware, virtual machines, or in the cloud.

Table of Contents

  1. Foreword for the Second Edition
  2. Foreword for the First Edition
  3. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
      1. Part I, High Availability and Scalability
      2. Part II, Monitoring and Managing
      3. Appendixes
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  4. I. High Availability and Scalability
    1. 1. Introduction
      1. What’s This Replication Stuff, Anyway?
      2. So, Backups Are Not Needed Then?
      3. What’s With All the Monitoring?
      4. Is There Anything Else I Can Read?
      5. Conclusion
    2. 2. MySQL Replicant Library
      1. Basic Classes and Functions
      2. Supporting Different Operating Systems
      3. Servers
      4. Server Roles
      5. Conclusion
    3. 3. MySQL Replication Fundamentals
      1. Basic Steps in Replication
        1. Configuring the Master
        2. Configuring the Slave
        3. Connecting the Master and Slave
      2. A Brief Introduction to the Binary Log
        1. What’s Recorded in the Binary Log
        2. Watching Replication in Action
        3. The Binary Log’s Structure and Content
      3. Adding Slaves
        1. Cloning the Master
        2. Cloning a Slave
        3. Scripting the Clone Operation
      4. Performing Common Tasks with Replication
        1. Reporting
          1. Handling reporting in Python
          2. Scheduling tasks on Unix
          3. Scheduling tasks on Windows
      5. Conclusion
    4. 4. The Binary Log
      1. Structure of the Binary Log
        1. Binlog Event Structure
        2. Event Checksums
      2. Logging Statements
        1. Logging Data Manipulation Language Statements
        2. Logging Data Definition Language Statements
        3. Logging Queries
          1. Current database
          2. Current time
          3. Context events
          4. Thread ID
        4. LOAD DATA INFILE Statements
        5. Binary Log Filters
        6. Triggers, Events, and Stored Routines
          1. Statements that define or destroy stored programs
          2. Statements that invoke triggers and stored routines
        7. Stored Procedures
        8. Stored Functions
        9. Events
        10. Special Constructions
          1. The LOAD_FILE function
        11. Nontransactional Changes and Error Handling
      3. Logging Transactions
        1. Transaction Cache
          1. How nontransactional statements are logged
          2. How to avoid replication problems with nontransactional statements
        2. Distributed Transaction Processing Using XA
        3. Binary Log Group Commit
      4. Row-Based Replication
        1. Enabling Row-based Replication
        2. Using Mixed Mode
      5. Binary Log Management
        1. The Binary Log and Crash Safety
        2. Binlog File Rotation
        3. Incidents
        4. Purging the Binlog File
      6. The mysqlbinlog Utility
        1. Basic Usage
          1. Reading remote files
          2. Reading raw binary logfiles
        2. Interpreting Events
          1. Query event post header and body
          2. Format description event post header and body
      7. Binary Log Options and Variables
        1. Options for Row-Based Replication
      8. Conclusion
    5. 5. Replication for High Availability
      1. Redundancy
      2. Planning
        1. Slave Failures
        2. Master Failures
        3. Relay Failures
        4. Disaster Recovery
      3. Procedures
        1. Hot Standby
          1. Handling a switchover
          2. Handling a switchover in Python
        2. Dual Masters
          1. Shared disks
          2. Replicated disks using DRBD
          3. Bidirectional replication
        3. Slave Promotion
          1. The traditional method for promoting a slave
          2. A revised method for promoting a slave
        4. Circular Replication
      4. Conclusion
    6. 6. MySQL Replication for Scale-Out
      1. Scaling Out Reads, Not Writes
      2. The Value of Asynchronous Replication
      3. Managing the Replication Topology
        1. Application-Level Load Balancing
          1. Example of an application-level load balancer
          2. MySQL native driver replication and load balancing plug-in
      4. Hierarchical Replication
        1. Setting Up a Relay Server
        2. Adding a Relay in Python
      5. Specialized Slaves
        1. Filtering Replication Events
          1. Master filters
          2. Slave filters
        2. Using Filtering to Partition Events to Slaves
      6. Managing Consistency of Data
        1. Consistency in a Nonhierarchical Deployment
        2. Consistency in a Hierarchical Deployment
      7. Conclusion
    7. 7. Data Sharding
      1. What Is Sharding?
        1. Why Should You Shard?
        2. Limitations of Sharding
          1. Cross-shard joins
          2. Using AUTO_INCREMENT
      2. Elements of a Sharding Solution
        1. High-Level Sharding Architecture
      3. Partitioning the Data
        1. Shard Allocation
          1. Single shard per server
          2. Multiple shards per server (virtual shards)
      4. Mapping the Sharding Key
        1. Sharding Scheme
          1. Static sharding schemes
          2. Dynamic sharding schemes
        2. Shard Mapping Functions
          1. Range mapping
            1. Creating the index table
            2. Adding new shards
            3. Fetching the shard
          2. Hash mapping and consistent hashing
            1. Creating the index table
            2. Adding new shards
            3. Fetching the shard
      5. Processing Queries and Dispatching Transactions
        1. Handling Transactions
        2. Dispatching Queries
      6. Shard Management
        1. Moving a Shard to a Different Node
        2. Splitting Shards
      7. Conclusion
    8. 8. Replication Deep Dive
      1. Replication Architecture Basics
        1. The Structure of the Relay Log
        2. The Replication Threads
        3. Starting and Stopping the Slave Threads
      2. Running Replication over the Internet
        1. Setting Up Secure Replication Using Built-in Support
        2. Setting Up Secure Replication Using Stunnel
      3. Finer-Grained Control Over Replication
        1. Information About Replication Status
          1. The state of the I/O and SQL threads
          2. The binary log and relay log positions
      4. Options for Handling Broken Connections
      5. How the Slave Processes Events
        1. Housekeeping in the I/O Thread
        2. SQL Thread Processing
          1. Context events
          2. Thread-specific events
          3. Filtering and skipping events
      6. Semisynchronous Replication
        1. Configuring Semisynchronous Replication
        2. Monitoring Semisynchronous Replication
      7. Global Transaction Identifiers
        1. Setting Up Replication Using GTIDs
        2. Failover Using GTIDs
        3. Slave Promotion Using GTIDs
        4. Replication of GTIDs
      8. Slave Safety and Recovery
        1. Syncing, Transactions, and Problems with Database Crashes
          1. I/O thread syncing
          2. SQL thread syncing
        2. Transactional Replication
          1. Setting up transactional replication
          2. Details of transactional replication
        3. Rules for Protecting Nontransactional Statements
      9. Multisource Replication
      10. Details of Row-Based Replication
        1. Table_map Events
        2. The Structure of Row Events
        3. Execution of Row Event
        4. Events and Triggers
        5. Filtering in Row-Based Replication
        6. Partial Row Replication
      11. Conclusion
    9. 9. MySQL Cluster
      1. What Is MySQL Cluster?
        1. Terminology and Components
        2. How Does MySQL Cluster Differ from MySQL?
        3. Typical Configuration
        4. Features of MySQL Cluster
        5. Local and Global Redundancy
        6. Log Handling
        7. Redundancy and Distributed Data
      2. Architecture of MySQL Cluster
        1. How Data Is Stored
        2. Partitioning
        3. Transaction Management
        4. Online Operations
      3. Example Configuration
        1. Getting Started
        2. Starting a MySQL Cluster
          1. Starting the management node
          2. Starting the management console
          3. Starting data nodes
          4. Starting the SQL nodes
        3. Testing the Cluster
        4. Shutting Down the Cluster
      4. Achieving High Availability
        1. System Recovery
        2. Node Recovery
        3. Replication
          1. Replication inside the cluster versus MySQL replication
          2. Replicating inside the cluster
          3. MySQL replication between clusters
          4. Architecture of MySQL Cluster (external) replication
          5. Single-channel and multichannel replication
      5. Achieving High Performance
        1. Considerations for High Performance
        2. High Performance Best Practices
      6. Conclusion
  5. II. Monitoring and Managing
    1. 10. Getting Started with Monitoring
      1. Ways of Monitoring
      2. Benefits of Monitoring
      3. System Components to Monitor
        1. Processor
        2. Memory
        3. Disk
        4. Network Subsystem
      4. Monitoring Solutions
      5. Linux and Unix Monitoring
        1. Process Activity
          1. The top command
          2. The iostat command
          3. The mpstat command
          4. The ps command
        2. Memory Usage
          1. The free command
          2. The pmap command
        3. Disk Usage
          1. The iostat command
          2. The sar command
          3. Disk usage analyzer
        4. Network Activity
          1. The netstat command
          2. The ifconfig command
        5. General System Statistics
          1. The uptime command
          2. The vmstat command
        6. Automated Monitoring with cron
      6. Mac OS X Monitoring
        1. System Profiler
        2. Console
        3. Activity Monitor
      7. Microsoft Windows Monitoring
        1. The Windows Experience
        2. The System Health Report
        3. The Event Viewer
        4. The Reliability Monitor
        5. The Task Manager
        6. The Performance Monitor
      8. Monitoring as Preventive Maintenance
      9. Conclusion
    2. 11. Monitoring MySQL
      1. What Is Performance?
      2. MySQL Server Monitoring
        1. How MySQL Communicates Performance
        2. Performance Monitoring
        3. SQL Commands
        4. The mysqladmin Utility
        5. MySQL Workbench
          1. MySQL server administration
            1. Management
            2. Configuration
            3. Security
            4. Data export/restore
          2. SQL development
        6. Third-Party Tools
          1. MySAR
          2. mytop
          3. InnoTop
          4. MONyog
        7. The MySQL Benchmark Suite
      3. Server Logs
      4. Performance Schema
        1. Concepts
        2. Getting Started
        3. Using Performance Schema to Diagnose Performance Problems
      5. MySQL Monitoring Taxonomy
      6. Database Performance
        1. Measuring Database Performance
          1. Using EXPLAIN
          2. Using ANALYZE TABLE
          3. Using OPTIMIZE TABLE
        2. Best Practices for Database Optimization
          1. Use indexes sparingly but effectively
          2. Use normalization, but don’t overdo it
          3. Use the right storage engine for the task
          4. Use views for faster results via the query cache
          5. Use constraints
          6. Use EXPLAIN, ANALYZE, and OPTIMIZE
      7. Best Practices for Improving Performance
        1. Everything Is Slow
        2. Slow Queries
        3. Slow Applications
        4. Slow Replication
      8. Conclusion
    3. 12. Storage Engine Monitoring
      1. InnoDB
        1. Using the SHOW ENGINE Command
        2. Using InnoDB Monitors
        3. Monitoring Logfiles
        4. Monitoring the Buffer Pool
        5. Monitoring Tablespaces
        6. Using INFORMATION_SCHEMA Tables
        7. Using PERFORMANCE_SCHEMA Tables
        8. Other Parameters to Consider
        9. Troubleshooting Tips for InnoDB
          1. Errors
          2. Deadlocks
          3. Data dictionary problems
          4. Observe console messages
          5. I/O problems
          6. Corrupted databases
      2. MyISAM
        1. Optimizing Disk Storage
        2. Repairing Your Tables
        3. Using the MyISAM Utilities
        4. Storing a Table in Index Order
        5. Compressing Tables
        6. Defragmenting Tables
        7. Monitoring the Key Cache
        8. Preloading Key Caches
        9. Using Multiple Key Caches
        10. Other Parameters to Consider
      3. Conclusion
    4. 13. Replication Monitoring
      1. Getting Started
      2. Server Setup
      3. Inclusive and Exclusive Replication
      4. Replication Threads
      5. Monitoring the Master
        1. Monitoring Commands for the Master
        2. Master Status Variables
      6. Monitoring Slaves
        1. Monitoring Commands for the Slave
        2. Slave Status Variables
      7. Replication Monitoring with MySQL Workbench
      8. Other Items to Consider
        1. Networking
        2. Monitor and Manage Slave Lag
        3. Causes and Cures for Slave Lag
        4. Working with GTIDs
      9. Conclusion
    5. 14. Replication Troubleshooting
      1. What Can Go Wrong
      2. Problems on the Master
        1. Master Crashed and Memory Tables Are in Use
        2. Master Crashed and Binary Log Events Are Missing
        3. Query Runs Fine on the Master but Not on the Slave
        4. Table Corruption After a Crash
        5. Binary Log Is Corrupt on the Master
        6. Killing Long-Running Queries for Nontransactional Tables
        7. Unsafe Statements
      3. Problems on the Slave
        1. Slave Server Crashed and Replication Won’t Start
        2. Slave Connection Times Out and Reconnects Frequently
        3. Query Results Are Different on the Slave than on the Master
        4. Slave Issues Errors when Attempting to Restart with SSL
        5. Memory Table Data Goes Missing
        6. Temporary Tables Are Missing After a Slave Crash
        7. Slave Is Slow and Is Not Synced with the Master
        8. Data Loss After a Slave Crash
        9. Table Corruption After a Crash
        10. Relay Log Is Corrupt on the Slave
        11. Multiple Errors During Slave Restart
        12. Consequences of a Failed Transaction on the Slave
        13. I/O Thread Problems
        14. SQL Thread Problems: Inconsistencies
        15. Different Errors on the Slave
      4. Advanced Replication Problems
        1. A Change Is Not Replicated Among the Topology
        2. Circular Replication Issues
        3. Multimaster Issues
        4. The HA_ERR_KEY_NOT_FOUND Error
        5. GTID Problems
      5. Tools for Troubleshooting Replication
      6. Best Practices
        1. Know Your Topology
        2. Check the Status of All of Your Servers
        3. Check Your Logs
        4. Check Your Configuration
        5. Conduct Orderly Shutdowns
        6. Conduct Orderly Restarts After a Failure
        7. Manually Execute Failed Queries
        8. Don’t Mix Transactional and Nontransactional Tables
        9. Common Procedures
          1. Troubleshooting replication failures
          2. Pausing replication
      7. Reporting Replication Bugs
      8. Conclusion
    6. 15. Protecting Your Investment
      1. What Is Information Assurance?
        1. The Three Practices of Information Assurance
        2. Why Is Information Assurance Important?
      2. Information Integrity, Disaster Recovery, and the Role of Backups
        1. High Availability Versus Disaster Recovery
        2. Disaster Recovery
          1. Disaster recovery planning
          2. Disaster recovery workflow
          3. Tools for disaster recovery
        3. The Importance of Data Recovery
          1. Terminology
        4. Backup and Restore
          1. Why back up?
          2. Expectations for backups
          3. Expectations for the restore process
          4. Logical versus physical backup
          5. Forming an archival plan
      3. Backup Tools and OS-Level Solutions
        1. MySQL Enterprise Backup
          1. How MEB works
          2. Performing a full backup
          3. Performing an incremental backup
          4. Restoring data
          5. Backing up a downed server
        2. MySQL Utilities Database Export and Import
        3. The mysqldump Utility
        4. Physical File Copy
        5. Logical Volume Manager Snapshots
          1. Getting started with LVM
          2. LVM in a backup and restore
        6. XtraBackup
        7. Comparison of Backup Methods
      4. Backup and MySQL Replication
        1. Backup and Recovery with Replication
        2. PITR
          1. Restoring after an error is replicated
          2. Recovery example
          3. Recovery images
          4. Backup procedure
          5. PITR in Python
      5. Automating Backups
      6. Conclusion
    7. 16. MySQL Enterprise Monitor
      1. Getting Started with MySQL Enterprise Monitor
        1. Commercial Offerings
        2. Anatomy of MySQL Enterprise Monitor
        3. Installation Overview
          1. Installing the MEM Service Manager
          2. Installing the MEM Agent
          3. Fixing monitoring agent problems
          4. Configuring the dashboard
      2. MySQL Enterprise Monitor Components
        1. Dashboard
        2. Monitoring Agent
        3. Advisors
        4. Query Analyzer
        5. MySQL Production Support
      3. Using MySQL Enterprise Monitor
        1. Monitoring
          1. Critical issues details
          2. Consolidated server graphs
          3. Server details
          4. Replication details
          5. Advisors
        2. Query Analyzer
        3. Further Information
      4. Conclusion
    8. 17. Managing MySQL Replication with MySQL Utilities
      1. Common MySQL Replication Tasks
        1. Checking Status
        2. Stopping Replication
        3. Adding Slaves
      2. MySQL Utilities
        1. Getting Started
        2. Using the Utilities Without Workbench
        3. Using the Utilities via Workbench
      3. General Utilities
        1. Comparing Databases for Consistency: mysqldbcompare
          1. Use with replication
          2. Examples of mysqldbcompare
        2. Copying Databases: mysqldbcopy
          1. Use with replication
          2. Example of mysqldbcopy
        3. Exporting Databases: mysqldbexport
          1. Example of mysqldbexport
        4. Importing Databases: mysqldbimport
          1. Use with replication
          2. Examples of mysqldbimport
        5. Discovering Differences: mysqldiff
          1. Use with replication
          2. Example of mysqldiff
        6. Showing Disk Usage: mysqldiskusage
          1. Use with replication
          2. Example of mysqldiskusage
        7. Checking Tables Indexes: mysqlindexcheck
          1. Use with replication
          2. Example of mysqlindexcheck
        8. Searching Metadata: mysqlmetagrep
          1. Use with replication
          2. Example of mysqlmetagrep
        9. Searching for Processes: mysqlprocgrep
          1. Use with replication
          2. Example of mysqlprocgrep
        10. Cloning Servers: mysqlserverclone
          1. Use with replication
          2. Example of mysqlserverclone
        11. Showing Server Information: mysqlserverinfo
          1. Use with replication
          2. Example of mysqlserverinfo
        12. Cloning Users: mysqluserclone
          1. Use with replication
          2. Example of mysqluserclone
        13. Utilities Client: mysqluc
          1. Use with replication
          2. Example of mysqluc
      4. Replication Utilities
        1. Setting Up Replication: mysqlreplicate
          1. Example of mysqlreplicate
        2. Checking Replication Setup: mysqlrplcheck
          1. Example of mysqlrplcheck
        3. Showing Topologies: mysqlrplshow
          1. Example of mysqlrplshow
      5. High Availability Utilities
        1. Concepts
          1. Global Transaction Identifiers
          2. Candidate slave
          3. Slave election
          4. Failover
          5. Switchover
        2. mysqlrpladmin
          1. Examples of mysqlrpladmin
        3. mysqlfailover
          1. Failover modes
          2. The failover event
          3. Extension points
      6. Creating Your Own Utilities
        1. Architecture of MySQL Utilities
        2. Custom Utility Example
          1. Operations
          2. Sample code
          3. Putting it all together
          4. Suggestions
      7. Conclusion
  6. A. Replication Tips and Tricks
    1. Examining the Binary Log with Verbose
    2. Using Replication to Repopulate a Table
      1. Statement-Based Logging
      2. Row-Based Logging
    3. Using MySQL Proxy to Perform Multimaster Replication
    4. Using a Default Storage Engine
    5. MySQL Cluster Multisource Replication
    6. Multichannel Replication with Failover
    7. Using the Current Database to Filter
    8. More Columns on Slave Than Master
    9. Fewer Columns on Slave Than Master
    10. Replicate Selected Rows to Slave
    11. Replication Heartbeat (5.4.4 or Later)
    12. Ignoring Servers in Circular Replication (5.5 or Later)
    13. Time-Delayed Replication (5.6 or Later)
    14. Shell Commands for Common Tasks
    15. Multisource Replication in a Shell Script
    16. Stored Procedure to Change Master
    17. Implementing Multisource Replication in Pure SQL (5.6 or Later)
  7. B. A GTID Implementation
    1. Adding GTIDs to a Server
    2. Transaction Handling Using GTIDs
    3. Finding Positions of GTIDs
  8. Index
  9. Colophon
  10. Copyright