MySQL安装
系统要求
在安装MySQL之前,请确保您的系统满足最低要求:
硬件要求
| 组件 | Minimum / 最低 | Recommended / 推荐 |
|---|---|---|
| 处理器 | 1 GHz | 2 GHz or higher |
| 内存 | 512 MB | 2 GB or more |
| 磁盘空间 | 1 GB | 5 GB or more |
| 网络 | Not required | For remote access |
软件要求
MySQL可在多个平台上使用:
- Windows: Windows 10/11, Windows Server 2016+
- macOS: macOS 10.15+
- Linux: Most distributions (Ubuntu, Debian, CentOS, RHEL, etc.)
- Unix: FreeBSD, Solaris
Windows安装
方法1:MySQL安装程序(推荐)
下载MySQL安装程序
- Visit https://dev.mysql.com/downloads/installer/
- Download mysql-installer-community-8.0.x.msi
运行安装程序
- Double-click the downloaded .msi file
- If prompted by User Account Control, click "Yes"
选择安装类型
- Developer Default: Includes MySQL Server, MySQL Workbench
- Full: Includes all MySQL products
- Server Only: MySQL Server only
- Custom: Select specific components检查要求
- Installer will check for missing prerequisites
- Install any required components
安装
- Review components to install
- Click "Execute" to begin installation
- Wait for download and installation
配置
- Configuration type: Standalone
- Port: 3306 (default)
- Authentication method: Strong password encryption
- Set root password
- Create user accounts (optional)
完成安装
- Click "Finish" when complete
方法2:ZIP压缩包
下载MySQL ZIP压缩包
- Visit https://dev.mysql.com/downloads/mysql/
- Download mysql-8.0.x-winx64.zip
解压文件
powershell
# Extract to desired location初始化MySQL
cmd
启动MySQL服务
cmd
连接到MySQL
cmd
方法3:使用Docker
powershell
# Pull MySQL image
# Run MySQL container
-e MYSQL_ROOT_PASSWORD=my-secret-pw `
-p 3306:3306 `
-d mysql:8.0
# Connect to MySQLmacOS安装
方法1:Homebrew(推荐)
安装Homebrew(如果尚未安装)
bash
安装MySQL
bash
# Start MySQL service
# Or start manually初始设置
bash
# Secure installation
# Or connect directly添加到PATH
bash
方法2:DMG安装包
下载DMG安装包
- Visit https://dev.mysql.com/downloads/mysql/
- Download mysql-8.0.x-macos11-x86_64.dmg
运行安装
- Open the downloaded .dmg file
- Double-click mysql-8.0.x.pkg
- Follow installation wizard
- Set temporary password (shown in installer)
启动MySQL
bash
# Using System Preferences
# Open MySQL preference pane
# Click "Start MySQL Server"
# Or using command line更改root密码
bash
# Use temporary password from installation方法3:使用Docker
bash
# Pull MySQL image
# Run MySQL container
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-p 3306:3306 \
-d mysql:8.0
# Connect to MySQLLinux安装
Ubuntu / Debian
步骤1:更新软件包索引
bash
步骤2:安装MySQL
bash
# Install MySQL Server
# Install MySQL Client
# Install MySQL development files步骤3:安全安装
bash
安全安装脚本将引导您完成以下步骤:
步骤4:启动MySQL服务
bash
# Start MySQL service
# Enable MySQL to start on boot
# Check service status步骤5:连接到MySQL
bash
CentOS / RHEL / Rocky Linux
步骤1:添加MySQL仓库
bash
# Download MySQL repository package
# Enable MySQL 8.0 repository
# Or for CentOS 7步骤2:安装MySQL
bash
# Install MySQL community server
# Install MySQL client步骤3:启动MySQL服务
bash
# Start MySQL service
# Enable MySQL to start on boot
# Check temporary password步骤4:安全安装
bash
# Get temporary password
# Run secure installation
# Or run full secure installationFedora / Fedora
bash
# Install MySQL
# Start and enable service
# Secure installation验证安装
检查MySQL版本
bash
# Command line
# In MySQL测试连接
bash
# Local connection
# Test basic commands检查服务状态
bash
# Linux (systemd)
# macOS
# Check if MySQL is running安装后配置
设置root密码
sql
-- Method 1: ALTER USER
-- Method 2: SET PASSWORD
-- Method 3: Using mysqladmin创建新用户
sql
-- Create a user with full privileges
-- Create a user with specific privileges配置远程访问
sql
-- Allow remote connections
-- Exit MySQL and edit configuration
-- In my.cnf or my.ini配置文件位置
| 平台 | Location / 位置 |
|---|---|
| Windows | C:\ProgramData\MySQL\MySQL Server 8.0\my.ini |
| macOS | /etc/my.cnf or /usr/local/etc/my.cnf |
| Linux (Ubuntu/Debian) | /etc/mysql/mysql.conf.d/mysqld.cnf |
| Linux (CentOS/RHEL) | /etc/my.cnf |
Docker安装
安装Docker
Windows / macOS
Linux
bash
# Ubuntu
# Add user to docker group使用Docker运行MySQL
bash
# Pull MySQL image
# Run MySQL container
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-p 3306:3306 \
-v mysql_data:/var/lib/mysql \
-d mysql:8.0
# View running containers
# Connect to MySQL
# Stop container
# Start container
# Remove containerDocker Compose / Docker Compose
yaml
- "3306:3306"
- mysql_data:/var/lib/mysqlbash
故障排除
常见安装问题
MySQL服务无法启动
bash
# Check error logs
# Check port availability
# Try starting with different configuration访问被拒绝错误
bash
# Reset root password
# Stop MySQL service
# Start MySQL in safe mode
# Connect and reset password端口已被使用
bash
# Find process using port 3306
# Change MySQL port in my.cnf检查MySQL日志
bash
# Error log locations
# View logs卸载
Windows
macOS
bash
# Stop and remove service
# Remove filesLinux
bash
# Ubuntu/Debian
# CentOS/RHEL小结
MySQL安装因平台而异:
使用MySQL安装程序轻松设置 使用Homebrew或DMG安装包 使用软件包管理器 使用Docker获得一致的环境
安装后的关键步骤:
安全安装 - 运行mysql_secure_installation 设置root密码 - 创建强密码 创建用户 - 创建具有适当权限的用户 配置远程访问 - 如果需要 备份配置 - 保持配置文件安全
下一步
administration.md)以了解如何管理和配置MySQL服务器。
上一个:简介
下一个:管理