Skip to content

MySQL命令大全

概述

本章提供了按类别组织的MySQL命令综合参考。这些命令对于数据库管理和维护至关重要。

命令分类

stop, configuration / 服务器管理:启动/停止、配置 数据库操作:创建、删除、使用 表操作:创建、删除、修改 数据操作:CRUD操作 用户管理:授权、撤销、用户 Restore**: Export, import / 备份/恢复:导出、导入 显示命令:显示信息

服务器管理

启动和停止

bash
# Start MySQL server

# Stop MySQL server

# Restart MySQL server

# Check status

# Enable on boot

连接命令

bash
# Connect to MySQL

# Connect to specific database

# Connect to remote server

# Connect with specific port

# Execute command without interactive mode

数据库命令

创建和删除

sql
-- Create database

-- Create with character set

-- Drop database

-- Drop if exists

选择数据库

sql
-- Use database

-- Show current database

-- Show all databases

数据库信息

sql
-- Show database size

-- Show database character set

表命令

创建和删除

sql
-- Create table

-- Create from SELECT

-- Drop table

-- Drop if exists

-- Truncate table (remove all data)

修改表

sql
-- Add column

-- Drop column

-- Modify column

-- Rename table

-- Add index

表信息

sql
-- Show table structure

-- Show all tables

-- Show create statement

-- Show table status

数据操作

插入

sql
-- Insert single row

-- Insert multiple rows

-- Insert from SELECT

查询

sql
-- Select all columns

-- Select specific columns

-- Select with WHERE

-- Select with ORDER BY

-- Select with LIMIT

更新

sql
-- Update single row

-- Update multiple rows

-- Update multiple columns

-- Update from SELECT

删除

sql
-- Delete single row

-- Delete multiple rows

-- Delete with LIMIT

-- Delete all rows

用户管理

创建和删除用户

sql
-- Create user

-- Create with plugin

-- Drop user

-- Drop user from all hosts

授予权限

sql
-- Grant all privileges

-- Grant specific privileges

-- Grant with GRANT OPTION

-- Apply changes

撤销权限

sql
-- Revoke specific privilege

-- Revoke all privileges

-- Revoke GRANT OPTION

显示权限

sql
-- Show user grants

-- Show current user grants

-- Show all users

备份命令

mysqldump / mysqldump

bash
# Export single database

# Export all databases

# Export specific tables

# Export with compression

# Export with routines

mysqlhotcopy / mysqlhotcopy

bash
# Hot copy (InnoDB only)
    --quiet /path/to/backup database_name

# Copy with regex

恢复命令

导入SQL文件

bash
# Import from file

# Import with source

-- Import with compression

LOAD DATA INFILE / LOAD DATA INFILE

sql
-- Import from CSV

显示命令

信息显示

sql
-- Show databases

-- Show tables

-- Show columns

-- Show indexes

-- Show create statement

-- Show grants

-- Show variables

-- Show status

-- Show processlist

-- Show engines

-- Show character sets

-- Show collations

-- Show events

-- Show triggers

-- Show procedures

分析命令

EXPLAIN / EXPLAIN

sql
-- Explain query

-- Extended explain

-- Analyze query plan

性能模式

sql
-- Use performance schema

-- Show wait events

-- Show statement performance

-- Show table I/O

管理命令

系统变量

sql
-- Show all variables

-- Show specific variable

-- Set global variable

-- Set session variable

-- Set persistent variable (MySQL 8.0+)

系统状态

sql
-- Show all status

-- Show specific status

-- Show engine status

-- Show master status

-- Show slave status

优化命令

表优化

sql
-- Optimize table

-- Optimize multiple tables

-- Analyze table

-- Check table

-- Repair table (MyISAM)

索引管理

sql
-- Create index

-- Drop index

-- Rebuild indexes

-- Check index usage

维护命令

定期维护

bash
# Daily backup

# Weekly optimization

# Monthly analysis

检查和修复

bash
# Check all databases

# Check specific database

# Check and repair

# Analyze tables

# Optimize tables

小结

MySQL命令包括:

服务器管理:启动、停止、配置 数据库操作:CRUD、备份、恢复 表操作:创建、修改、删除、优化 用户管理:创建用户、授予权限 显示命令:显示数据库信息 分析工具:EXPLAIN、性能监控 维护:备份、检查、优化

使用这些命令进行高效的数据库管理和故障排除。


上一个:MySQL运算符

下一个:MySQL测验