MySQL管理
概述
MySQL管理涉及管理MySQL服务器、数据库、用户和安全。本章涵盖维护健康MySQL环境的基本管理任务。
管理员职责
服务器配置和监控 用户管理和安全 数据库备份和恢复 性能优化 访问控制
服务器管理
启动和停止MySQL
bash
# Linux (systemd)
# Linux (init.d)
# macOS (Homebrew)
# Windows (command line)
# As service服务器配置
MySQL配置文件位置:
| 平台 | Location / 位置 |
|---|---|
| Linux (Ubuntu/Debian) | /etc/mysql/mysql.conf.d/mysqld.cnf |
| Linux (CentOS/RHEL) | /etc/my.cnf |
| macOS | /usr/local/etc/my.cnf |
| Windows | C:\ProgramData\MySQL\MySQL Server 8.0\my.ini |
配置文件结构
ini
# Server settings
# Character set
# Performance settings
# Security运行时配置
sql
-- Show all variables
-- Show specific variable
-- Set variable at runtime
-- Persist changes (MySQL 8.0+)
-- View persisted variables服务器状态
sql
-- Show server status
-- Connection information
-- Query cache
-- Table locks
-- Temporary tables
-- Slow queries用户管理
创建用户
sql
-- Create user with password
-- Create user with specific host
-- Create user without password (not recommended)
-- Create user with authentication plugin
-- Create user with resource limits授予权限
sql
-- Grant all privileges
-- Grant specific privileges
-- Grant with grant option
-- Grant replication privileges
-- Grant backup privileges
-- Grant process privilege撤销权限
sql
-- Revoke specific privileges
-- Revoke all privileges
-- Revoke grant option管理用户
sql
-- Show all users
-- Show user privileges
-- Change user password
-- Rename user
-- Lock/unlock user
-- Expire password
-- Drop user基于角色的访问控制
sql
-- Create role
-- Grant privileges to role
-- Assign role to user
-- Set default role
-- Activate role
-- View assigned roles安全管理
安全安装
bash
# Run MySQL secure installation此脚本执行以下操作:
设置root密码 删除匿名用户 禁止root远程登录 删除测试数据库 重新加载权限表
TLS Configuration / SSL/TLS配置
bash
# Generate SSL certificatessql
-- Check SSL status
-- Require SSL for user
-- Require specific cipher防火墙配置
bash
# Linux (ufw)
# Linux (firewalld)MySQL企业防火墙
sql
-- Enable firewall
-- Create firewall rule
-- Create whitelist rule
-- Check firewall status备份和恢复
逻辑备份
bash
# Backup single database
# Backup multiple databases
# Backup all databases
# Backup with options
--databases myapp \
--single-transaction \
--routines \
--triggers \
--events \
# Backup structure only (no data)
# Backup data only (no structure)
# Compressed backup物理备份
bash
# Copy data files (when MySQL is stopped)
# Using MySQL Enterprise Backup恢复
bash
# Restore from SQL dump
# Restore from compressed backup
# Restore to different database
# Restore all databases
# Using source command in MySQL时间点恢复
bash
# Backup binlog position
# Restore full backup
# Apply binlog to point in time性能监控
性能模式
sql
-- Enable performance schema (enabled by default)
-- View available instruments
-- Check table I/O
-- Check index usage
-- Check statements慢查询日志
sql
-- Enable slow query log
-- Slow query log file location
-- Analyze slow queries系统变量
sql
-- Key performance variables
-- Key status variables数据库健康检查
sql
-- Check table status
-- Check for corrupted tables
-- Repair table (MyISAM)
-- Optimize table
-- Analyze table
-- Check disk usage数据库维护
定期维护任务
sql
-- Optimize all tables
-- Analyze tables for better query plans
-- Check all tables in database
-- Repair MyISAM tables计划维护
创建维护脚本:
bash
#!/bin/bash
# mysql_maintenance.sh
# Optimize all databases添加到cron:
bash
# Run maintenance daily at 3 AM日志管理
错误日志
sql
-- Error log location
-- Check error log通用查询日志
sql
-- Enable general query log
-- Disable when not needed二进制日志
sql
-- Enable binary logging
-- Binary log format
-- View binary log files
-- View current binary log position
-- View binary log events复制设置
主服务器配置
ini
sql
-- Create replication user
-- Get master status从服务器配置
ini
sql
-- Configure replication
-- Start replication
-- Check replication status小结
MySQL管理包括:
stop, configuration, status / 服务器管理:启动/停止、配置、状态 用户管理:创建、授予权限、安全 Recovery**: Logical and physical backups / 备份/恢复:逻辑和物理备份 监控:性能模式、慢查询 维护:优化、健康检查 复制:主从设置
上一个:安装
下一个:连接