834 字
约 2 分钟
18
🔧 MySQL 安装
2026-09-19
无标签
🔧 MySQL 安装
⚠️ 版本选择建议
**推荐使用 MySQL 8.x:**MySQL 5.7 已结束扩展支持,建议新项目使用 MySQL 8.x 版本。如果必须使用 MySQL 5.7,请确保及时关注安全更新。
系统要求
- 操作系统:Windows 10+、Linux(Ubuntu 18.04+、CentOS 7+)、macOS 10.14+
- 内存:最少 512MB RAM(推荐 2GB+)
- 磁盘空间:最少 4GB 可用空间
- 处理器:x86_64 架构
1Windows 安装
MySQL 8.x 安装步骤
- 访问MySQL 官方下载页面
- 选择 "MySQL Installer for Windows"
- 下载 mysql-installer-web-community 或 mysql-installer-community
- 运行安装程序,选择 "Developer Default" 或 "Server only"
- 配置 MySQL Server:选择配置类型:Development Computer设置 root 密码(MySQL 8.x 要求强密码)选择身份验证方法:推荐使用 "Strong Password Encryption"
MySQL 8.x 新的身份验证
MySQL 8.x 默认使用 caching_sha2_password 身份验证插件,提供更强的安全性。如需兼容旧版本客户端,可选择 mysql_native_password。
验证安装
# 打开命令提示符,测试连接
mysql -u root -p
# 查看版本信息
SELECT VERSION();
# MySQL 8.x 输出示例
# 8.0.35
2Linux 安装
Ubuntu/Debian 安装
# 更新包列表
sudo apt update
# 安装 MySQL 8.x
sudo apt install mysql-server
# 启动 MySQL 服务
sudo systemctl start mysql
sudo systemctl enable mysql
# 安全配置(设置 root 密码等)
sudo mysql_secure_installation
CentOS/RHEL 安装
# 添加 MySQL 官方仓库
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# 安装 MySQL 8.x
sudo yum install mysql-community-server
# 启动服务
sudo systemctl start mysqld
sudo systemctl enable mysqld
# 查看临时密码
sudo grep 'temporary password' /var/log/mysqld.log
# 安全配置
sudo mysql_secure_installation
如果需要安装 MySQL 5.7
# Ubuntu 安装 MySQL 5.7
sudo apt update
sudo apt install mysql-server-5.7
# CentOS 安装 MySQL 5.7
sudo yum install mysql57-community-release
sudo yum install mysql-community-server
3macOS 安装
使用 Homebrew(推荐)
# 安装 Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 MySQL 8.x
brew install mysql
# 启动 MySQL 服务
brew services start mysql
# 安全配置
mysql_secure_installation
使用官方安装包
- 下载 macOS 版本的 MySQL DMG 文件
- 双击安装包,按照向导完成安装
- 在系统偏好设置中启动 MySQL
- 配置环境变量:
# 添加到 ~/.bash_profile 或 ~/.zshrc
export PATH="/usr/local/mysql/bin:$PATH"
# 重新加载配置
source ~/.bash_profile
配置文件位置
| 操作系统 | 配置文件路径 | 数据目录 |
|---|---|---|
| Windows | C:\ProgramData\MySQL\MySQL Server 8.x\my.ini | C:\ProgramData\MySQL\MySQL Server 8.x\Data |
| Linux | /etc/mysql/mysql.conf.d/mysqld.cnf | /var/lib/mysql |
| macOS | /usr/local/mysql/my.cnf | /usr/local/mysql/data |
基本配置优化
# my.cnf 或 my.ini 基本配置示例
[mysqld]
# 基本设置
port = 3306
bind-address = 127.0.0.1
# 字符集设置(MySQL 8.x 默认为 utf8mb4)
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# 内存设置
innodb_buffer_pool_size = 1G
key_buffer_size = 256M
# 连接设置
max_connections = 200
max_connect_errors = 10
# 日志设置
log-error = /var/log/mysql/error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
# MySQL 8.x 特定设置
default_authentication_plugin = mysql_native_password
常见问题解决
1. 忘记 root 密码
# 停止 MySQL 服务
sudo systemctl stop mysql
# 跳过权限验证启动
sudo mysqld_safe --skip-grant-tables &
# 连接并重置密码
mysql -u root
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
# 重启 MySQL 服务
sudo systemctl restart mysql
2. 连接被拒绝
# 检查服务状态
sudo systemctl status mysql
# 检查端口监听
sudo netstat -tlnp | grep :3306
# 检查防火墙设置
sudo ufw status
sudo ufw allow 3306
评论
0 条
还没有评论,先写一条吧。