Skip to content

MySQL安装

系统要求

在安装MySQL之前,请确保您的系统满足最低要求:

硬件要求

组件Minimum / 最低Recommended / 推荐
处理器1 GHz2 GHz or higher
内存512 MB2 GB or more
磁盘空间1 GB5 GB or more
网络Not requiredFor 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安装程序

运行安装程序

  • 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压缩包

解压文件

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 MySQL

macOS安装

方法1:Homebrew(推荐)

安装Homebrew(如果尚未安装)

bash

安装MySQL

bash

# Start MySQL service

# Or start manually

初始设置

bash
# Secure installation

# Or connect directly

添加到PATH

bash

方法2:DMG安装包

下载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 MySQL

Linux安装

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 installation

Fedora / 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 / 位置
WindowsC:\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 container

Docker Compose / Docker Compose

yaml

      - "3306:3306"
      - mysql_data:/var/lib/mysql
bash

故障排除

常见安装问题

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 files

Linux

bash
# Ubuntu/Debian

# CentOS/RHEL

小结

MySQL安装因平台而异:

使用MySQL安装程序轻松设置 使用Homebrew或DMG安装包 使用软件包管理器 使用Docker获得一致的环境

安装后的关键步骤:

安全安装 - 运行mysql_secure_installation 设置root密码 - 创建强密码 创建用户 - 创建具有适当权限的用户 配置远程访问 - 如果需要 备份配置 - 保持配置文件安全

下一步

administration.md)以了解如何管理和配置MySQL服务器。


上一个:简介

下一个:管理