MySQL Operations in Practice — From Optimization, Monitoring to Disaster Recovery
Introduction
This article documents MySQL database operations practices, covering performance optimization, the monitoring system, Binlog management, dual-master high-availability deployment, and recovery from data file corruption. The cases are drawn from production environments.
InnoDB Performance Optimization in Practice
Baseline Status Analysis
Before optimizing InnoDB, you first need a comprehensive understanding of the current database runtime status:
| |
These commands help identify performance bottlenecks and provide data to guide subsequent optimization.
I/O Thread Optimization
Properly configuring the number of read/write threads can effectively utilize CPU resources:
| |
Log Parameter Configuration
Important: Adjusting these configurations requires cleaning up ib_logfile files, otherwise MySQL will fail to start
| |
Parameter Details:
- innodb_log_buffer_size: Transaction log buffer, shaped like a “funnel,” continuously writes cached log records to disk. A larger buffer can temporarily hold complete transaction logs, reducing disk I/O.
- innodb_log_file_size: Controls transaction log file size. Larger log files mean fewer checkpoints, saving disk I/O, but increase recovery time during database crashes.
- innodb_flush_log_at_trx_commit: Controls the log write strategy on transaction commit. 1 means writing to disk on every commit, 2 means writing once per second, 0 means letting the OS control write timing.
Disk I/O Optimization
Key parameters for improving disk I/O utilization:
| |
⚠️ Danger Warning: This parameter is risky — improper settings will directly impact disk I/O capability and degrade performance.
Cache Optimization
| |
Asynchronous I/O Support
On Linux systems, it is recommended to enable MySQL’s native asynchronous I/O support:
| |
| |
Building a MySQL Monitoring System
Privilege Management Principles
Before building a monitoring system, establish sound privilege management principles:
- Principle of Least Privilege: Grant only the minimum privileges needed to prevent misuse
- Host Login Restrictions: When creating users, restrict login hosts — typically to specific IPs or internal IP ranges
- Clean Up Default Users: After initializing the database, remove passwordless default users
- Password Complexity: Set passwords that meet complexity requirements for every user
- Regular Cleanup: Periodically remove unnecessary users, revoke privileges, or delete accounts
Monitoring Account Authorization
Grant privileges separately for local and remote monitoring:
| |
Monitoring Principles and Common Commands
MySQL monitoring primarily retrieves system status through the following commands:
| |
You can also obtain detailed InnoDB storage engine information through the information_schema database.
Privilege Table Structure
MySQL privilege control is based on granular privilege levels:
| Privilege | Privilege Level | Description |
|---|---|---|
| CREATE | Database, table, or index | Create databases, tables, or indexes |
| DROP | Database or table | Drop databases or tables |
| GRANT OPTION | Database, table, or stored programs | Grant privileges option |
| REFERENCES | Database or table | Foreign key references |
| ALTER | Table | Alter table structure |
| DELETE | Table | Delete data |
| INDEX | Table | Index operations |
| INSERT | Table | Insert data |
| SELECT | Table | Query data |
| UPDATE | Table | Update data |
| CREATE VIEW | View | Create views |
| SHOW VIEW | View | Show views |
| ALTER ROUTINE | Stored procedure | Alter stored procedures |
| CREATE ROUTINE | Stored procedure | Create stored procedures |
| EXECUTE | Stored procedure | Execute stored procedures |
| FILE | File access on server host | File access |
| CREATE TEMPORARY TABLES | Server administration | Create temporary tables |
| LOCK TABLES | Server administration | Lock tables |
| CREATE USER | Server administration | Create users |
| PROCESS | Server administration | View processes |
| RELOAD | Server administration | Execute flush commands |
| REPLICATION CLIENT | Server administration | Replication client |
| REPLICATION SLAVE | Server administration | Replication slave |
| SHOW DATABASES | Server administration | Show databases |
| SHUTDOWN | Server administration | Shutdown database |
| SUPER | Server administration | Kill threads |
Binlog Operations Guide
Binlog Fundamentals
MySQL’s binary log is one of the most important MySQL logs. It records all DDL and DML statements (except data queries) as events, including the execution time of each statement. MySQL’s binary log is transaction-safe.
Enabling binary logs incurs approximately 1% performance overhead. Its two main purposes are:
- MySQL Replication: Enable binlog on the Master side; the Master passes its binary logs to slaves for data synchronization
- Data Recovery: Recover data using the mysqlbinlog tool
Binary logs consist of two types of files:
- Binary log index file (suffix .index): Records all binary log files
- Binary log file (suffix .00000*): Records all DDL and DML statement events in the database
Enabling Binlog
Edit the MySQL configuration file:
| |
Set in the [mysqld] section:
| |
Restart the MySQL service for the configuration to take effect:
| |
Checking Binlog Status
Log into the MySQL server and confirm binary logging is enabled by checking variable configuration:
| |
| |
Common Binlog Commands
1. List All Binlog Files
| |
2. Check Current Master Status
| |
3. Flush Logs
Flush the log — starting from this moment, a new numbered binlog file is generated:
| |
Note: When using mysqldump for backups, the -F option also flushes the log.
4. Reset All Binlog Files
| |
MySQL Dual-Master High Availability Deployment
Deployment Architecture
The dual-master deployment architecture provides high availability — two MySQL instances serve as both master and slave to each other, ensuring service continuity during single-point failures.
Dual-Master Configuration Highlights
Server 1 Configuration (192.168.1.10):
| |
Server 2 Configuration (192.168.1.11):
| |
Establishing Master-Slave Relationships
1. Create Replication Users
| |
2. Configure Master-Slave Relationships
Server 1 Configuration (pointing to Server 2):
| |
Server 2 Configuration (pointing to Server 1):
| |
3. Start Replication
| |
Data Synchronization
1. Initial Data Synchronization
Export data from the master server:
| |
Import to the standby server:
| |
2. Application Access Authorization
| |
Status Checks
1. Check Replication Status
| |
Key indicators:
Slave_IO_Running: YesSlave_SQL_Running: YesSeconds_Behind_Master: Should be 0 or a small value
2. Monitor Replication Lag
| |
Emergency Recovery from Data File Corruption
Fault Diagnosis
When database file corruption is detected, quickly assess the extent and impact:
| |
Emergency Repair Steps
1. Temporary Fix
If storage anomalies cause the partition to become read-only, add the following configuration in my.cnf:
| |
⚠️ Important Reminders:
- Try not to set this parameter above 3, otherwise it may cause permanent data loss
- Make a full backup before proceeding
- innodb_force_recovery can be set from 1-6, where larger numbers include the effects of all smaller numbers
2. Recovery Parameter Details
| |
3. Complete Recovery Procedure
Step 1: Stop Write Operations
| |
Step 2: Export Data
| |
Step 3: Backup Existing Data
| |
Step 4: Rebuild the Database
| |
Step 5: Skip Grant Tables on Startup
| |
Step 6: Set Password and Privileges
| |
Step 7: Import Data
| |
Step 8: Restart Business Systems
| |
4. Set Up Regular Backups
| |
| |
Summary
This article covered practical MySQL database operations experience from multiple dimensions:
Performance Optimization Highlights
- I/O Optimization: Properly configure read/write thread counts to improve disk I/O utilization
- Cache Optimization: Reasonably set buffer pool size and instance count
- Log Optimization: Adjust transaction log parameters to balance performance and data safety
- Async I/O: Enable Linux native async I/O support to boost I/O performance
Monitoring System Development
- Privilege Management: Follow the principle of least privilege to establish a comprehensive privilege system
- Monitoring Accounts: Distinguish between local and remote monitoring, configure privileges appropriately
- Status Checks: Monitor system status in real time through various commands
- Privilege Tables: Understand MySQL privilege levels and privilege types
High Availability Deployment
- Dual-Master Architecture: Achieve high availability, ensuring service continuity
- Configuration Synchronization: Sound dual-master configuration parameters
- Data Synchronization: Complete initial data synchronization workflow
- Status Monitoring: Real-time monitoring of replication status and lag
Disaster Recovery
- Emergency Response: Rapid response when data files are corrupted
- Recovery Strategy: Complete process from temporary fix to full recovery
- Backup Mechanism: Establish a comprehensive regular backup mechanism
- Preventive Measures: Prevent failures through proper parameter configuration
MySQL operations requires holistic consideration across architecture design, performance optimization, monitoring and maintenance, and disaster recovery. Parameter configuration and backup mechanisms are the key levers for preventing failures.