MySQL安装与配置

一、Windows安装MySQL

1.1 下载MySQL

1.1.1 访问官网

访问MySQL官方网站下载页面:https://dev.mysql.com/downloads/mysql/

选择版本:

  • MySQL Community Server(社区版):免费
  • MySQL Enterprise Edition(企业版):收费

选择操作系统:

  • Windows (x86, 64-bit), ZIP Archive
  • Windows (x86, 64-bit), MSI Installer

1.1.2 下载安装包

推荐下载:

  • MSI Installer:图形化安装,适合初学者
  • ZIP Archive:解压即用,适合高级用户

下载步骤:

  1. 选择操作系统:Microsoft Windows
  2. 选择版本:MySQL Community Server 8.0
  3. 选择文件:mysql-installer-community-8.0.xx.msi
  4. 点击Download
  5. 选择"No thanks, just start my download"

1.2 安装MySQL

1.2.1 运行安装程序

双击下载的MSI文件,启动安装向导。

安装类型:

类型 说明
Developer Default 开发者默认安装
Server only 仅安装服务器
Client only 仅安装客户端
Full 完整安装
Custom 自定义安装

推荐选择:Developer Default

1.2.2 安装步骤

步骤1:选择安装类型

Choosing a Setup Type
○ Developer Default
○ Server only
○ Client only
○ Full
● Custom

步骤2:选择产品

MySQL Server 8.0
MySQL Workbench
MySQL Shell
MySQL Router
Connector/ODBC
Connector/NET

步骤3:安装 点击Execute开始安装,等待安装完成。

步骤4:配置

  • 配置类型:Development Computer
  • 端口:3306(默认)
  • MySQL Root密码:设置root用户密码
  • 创建用户:可选

1.2.3 配置环境变量

添加PATH环境变量:

  1. 右键"此电脑" → "属性"
  2. 点击"高级系统设置"
  3. 点击"环境变量"
  4. 在"系统变量"中找到"Path"
  5. 点击"编辑"
  6. 添加MySQL bin目录路径:C:\Program Files\MySQL\MySQL Server 8.0\bin

验证安装:

mysql --version

输出示例:

mysql  Ver 8.0.33 for Win64 on x86_64 (MySQL Community Server - GPL)

1.3 服务管理

1.3.1 Windows服务

查看服务状态:

net start | findstr MySQL

启动服务:

net start MySQL80

停止服务:

net stop MySQL80

重启服务:

net stop MySQL80
net start MySQL80

1.3.2 使用服务管理器

  1. Win + R,输入services.msc
  2. 找到MySQL服务
  3. 右键选择启动/停止/重启

二、Linux安装MySQL

2.1 CentOS/RHEL安装

2.1.1 YUM安装

添加MySQL YUM源:

# 下载MySQL YUM源
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

# 安装YUM源
rpm -ivh mysql80-community-release-el7-3.noarch.rpm

# 验证YUM源
yum repolist enabled | grep "mysql.*-community.*"

安装MySQL:

# 安装MySQL服务器
yum install mysql-community-server -y

# 查看安装版本
mysql --version

2.1.2 启动服务

启动MySQL:

# 启动服务
systemctl start mysqld

# 设置开机自启
systemctl enable mysqld

# 查看服务状态
systemctl status mysqld

查看临时密码:

grep 'temporary password' /var/log/mysqld.log

输出示例:

2024-01-01T00:00:00.000000Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Ab3#xYz9Kp

2.2 Ubuntu/Debian安装

2.2.1 APT安装

更新软件源:

apt update

安装MySQL:

# 安装MySQL服务器
apt install mysql-server -y

# 查看安装版本
mysql --version

2.2.2 安全配置

运行安全脚本:

mysql_secure_installation

配置选项:

Securing the MySQL server deployment.

Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y

Please set the password for root here.
New password: 
Re-enter new password: 

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

2.3 服务管理

2.3.1 systemctl命令

启动服务:

systemctl start mysql

停止服务:

systemctl stop mysql

重启服务:

systemctl restart mysql

查看状态:

systemctl status mysql

设置开机自启:

systemctl enable mysql

取消开机自启:

systemctl disable mysql

三、macOS安装MySQL

3.1 DMG安装

3.1.1 下载DMG文件

访问MySQL官网下载页面,选择macOS版本:

  • macOS 11 (x86, 64-bit), DMG Archive
  • macOS 12 (ARM, 64-bit), DMG Archive

3.1.2 安装步骤

  1. 双击DMG文件
  2. 双击安装包开始安装
  3. 一路点击"Continue"
  4. 设置root密码
  5. 完成安装

3.2 Homebrew安装

3.2.1 安装Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3.2.2 安装MySQL

安装MySQL:

brew install mysql

启动服务:

brew services start mysql

停止服务:

brew services stop mysql

重启服务:

brew services restart mysql

四、配置文件详解

4.1 配置文件位置

Windows:

  • C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
  • C:\Program Files\MySQL\MySQL Server 8.0\my.ini

Linux:

  • /etc/my.cnf
  • /etc/mysql/my.cnf
  • /usr/etc/my.cnf

macOS:

  • /usr/local/etc/my.cnf
  • /opt/homebrew/etc/my.cnf

4.2 常用配置项

4.2.1 基本配置

[mysqld]
# 服务器端口
port = 3306

# 数据目录
datadir = /var/lib/mysql

# 套接字文件
socket = /var/lib/mysql/mysql.sock

# 字符集
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# 默认存储引擎
default-storage-engine = InnoDB

# 最大连接数
max_connections = 200

# 错误日志
log-error = /var/log/mysql/error.log

4.2.2 InnoDB配置

[mysqld]
# InnoDB缓冲池大小(建议设为物理内存的70-80%)
innodb_buffer_pool_size = 1G

# 日志文件大小
innodb_log_file_size = 256M

# 日志缓冲区大小
innodb_log_buffer_size = 16M

# 刷新日志策略
innodb_flush_log_at_trx_commit = 1

# 锁等待超时时间
innodb_lock_wait_timeout = 50

4.2.3 查询缓存配置

[mysqld]
# 查询缓存大小(MySQL 8.0已移除)
query_cache_size = 64M

# 查询缓存类型
query_cache_type = 1

4.2.4 日志配置

[mysqld]
# 错误日志
log-error = /var/log/mysql/error.log

# 慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2

# 二进制日志
log_bin = mysql-bin
binlog_format = ROW
expire_logs_days = 7

4.3 客户端配置

[client]
# 客户端端口
port = 3306

# 套接字文件
socket = /var/lib/mysql/mysql.sock

# 默认字符集
default-character-set = utf8mb4

[mysql]
# MySQL客户端工具配置
prompt = \\u@\\h [\\d]> 
no-auto-rehash

五、连接MySQL

5.1 命令行连接

5.1.1 本地连接

基本连接:

mysql -u root -p

指定密码:

mysql -u root -p'password'

指定主机和端口:

mysql -h localhost -P 3306 -u root -p

指定数据库:

mysql -u root -p school

5.1.2 远程连接

连接远程服务器:

mysql -h 192.168.1.100 -P 3306 -u root -p

连接参数:

参数 说明
-h 主机地址
-P 端口号
-u 用户名
-p 密码
-D 数据库名
-e 执行SQL语句

5.2 图形化工具

5.2.1 MySQL Workbench

MySQL官方提供的图形化管理工具。

主要功能:

  • 数据库设计
  • SQL开发
  • 数据库管理
  • 数据迁移
  • 性能监控

5.2.2 Navicat

商业数据库管理工具,功能强大。

主要功能:

  • 多数据库支持
  • 数据同步
  • 结构同步
  • 数据传输
  • 备份还原

5.2.3 phpMyAdmin

基于Web的MySQL管理工具。

主要功能:

  • Web界面管理
  • 数据导入导出
  • SQL查询
  • 用户管理

5.3 连接测试

5.3.1 测试连接

查看版本:

SELECT VERSION();

查看状态:

SHOW STATUS;

查看数据库:

SHOW DATABASES;

查看用户:

SELECT USER();

查看当前数据库:

SELECT DATABASE();

六、本章小结

6.1 核心要点

  • ✅ Windows使用MSI安装包安装MySQL
  • ✅ Linux使用YUM/APT安装MySQL
  • ✅ macOS使用DMG或Homebrew安装
  • ✅ 配置文件my.ini/my.cnf控制MySQL行为
  • ✅ 使用systemctl管理MySQL服务

6.2 验证清单

完成本章学习后,请确认您能够:

  • 在本机安装MySQL
  • 配置环境变量
  • 启动和停止MySQL服务
  • 使用命令行连接MySQL
  • 修改配置文件