WINDOWS平台下MySQL的安装和配置
MySQL的安装
下载MySQL
使用百度搜索关键字“MySQL 官网”
![](https://img.haomeiwen.com/i7445074/42fb27c458318acb.png)
或者直接打开网址:https://www.mysql.com/
选择Downloads > Community > MySQL Community Server
打开下载界面
![](https://img.haomeiwen.com/i7445074/b9f1ab3da14ca10e.png)
选择5.6的版本,因为5.7的安装比较坑
![](https://img.haomeiwen.com/i7445074/4e34078cca851dfe.png)
选择对应的平台,进行下载
![](https://img.haomeiwen.com/i7445074/c5d1a6674e0ca539.png)
点击下载后,这里会看到一个准备开始下载的界面,点击No thanks, just start my download.
就可以开始下载MySQL了
![](https://img.haomeiwen.com/i7445074/d074e4d5dc4521da.png)
当前最新GA版下载地址:v5.6.37
等待下载完成(由于时间关系,我就使用已经下载过的v5.6.31进行演示),将下载的压缩包解压,例如我将压缩包解压到“D盘”
![](https://img.haomeiwen.com/i7445074/faedbafc4fffe4f1.png)
![](https://img.haomeiwen.com/i7445074/d6ee2fbf806ba12c.png)
配置环境变量
右键单击我的电脑,选择属性,打开系统设置
![](https://img.haomeiwen.com/i7445074/c80eafe668ac1575.png)
打开高级系统设置
![](https://img.haomeiwen.com/i7445074/0b63fdb3478781a8.png)
打开环境变量配置
![](https://img.haomeiwen.com/i7445074/5f68e98562087941.png)
新建变量名“MYSQL_HOME”,变量值为MySQL解压后的根路径
例如:
变量名:MYSQL_HOME
变量值:D:\mysql-5.6.31-winx64
![](https://img.haomeiwen.com/i7445074/b99e1d529b46b876.png)
将MYSQL_HOME配置到Path中
例如:%MYSQL_HOME%\bin
(注意,Path变量值如果是在最后,则不用“;
”)
![](https://img.haomeiwen.com/i7445074/c4297edcb764110a.png)
配置MySQL
编辑MySQL根目录下的my-default.ini
文件
![](https://img.haomeiwen.com/i7445074/bc93c9d915d782f5.png)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
# 设置客户端默认字符集
default-character-set = utf8
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# 设置服务端字符集
character-set-server = utf8
# 设置安装目录
basedir = %MYSQL_HOME%
# 设置数据存放目录
datadir = %MYSQL_HOME%\data
# 设置3306端口
port = 3306
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# 设置数据模式
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
![](https://img.haomeiwen.com/i7445074/072013ddb626538d.png)
初始化MySQL
打开拥有管理员权限的终端
![](https://img.haomeiwen.com/i7445074/df6af6c517512fb5.png)
切换到MySQL的bin目录下
![](https://img.haomeiwen.com/i7445074/06d59b0067af946d.png)
执行命令mysqld --install
,看见Service successfully installed.
代表MySQL初始化成功,否则初始化不成功
![](https://img.haomeiwen.com/i7445074/213d4f9bd51c1b9d.png)
卸载MySQL
首先需要停止MySQL服务(如何停止MySQL的服务下面会将),然后打开拥有管理员权限的终端,切换到MySQL的bin目录下
![](https://img.haomeiwen.com/i7445074/06d59b0067af946d.png)
执行命令mysqld --remove
,看见Service successfully removed.
代表MySQL卸载成功,否则卸载不成功
![](https://img.haomeiwen.com/i7445074/bd6d25095e4f3427.png)
启动MySQL
使用Win+R
键,打开运行界面,输入cmd
,打开终端,输入net start mysql
,看见“MySQL 服务启动成功时”,就代表MySQL服务启动成功了。
![](https://img.haomeiwen.com/i7445074/dfdbd28309f36153.png)
停止MySQL
使用Win+R
键,打开运行界面,输入cmd
,打开终端,输入net stop mysql
,看见“MySQL 服务已成功停止”时,就代表MySQL服务停止成功了。
![](https://img.haomeiwen.com/i7445074/ee56093f5447e377.png)
使用小技巧
登录MySQL
使用Win+R键,打开运行界面,输入cmd,打开终端,输入mysql -u 你的用户名 -p
,MySQL的默认用户是root,密码为空
例如:mysql -u root -p
,
![](https://img.haomeiwen.com/i7445074/49c414be9806fa50.png)
输入密码,若密码为空,直接回车即可
![](https://img.haomeiwen.com/i7445074/05ab35c500b0361c.png)
修改root用户的密码
使用root用户登录后,使用use mysql;
命令切换到mysql数据库
![](https://img.haomeiwen.com/i7445074/6af102d6d81527f8.png)
使用命令行update user set password=password('你的密码') where user='root';
来修改root用户的密码
例如:update user set password=password('1') where user='root';
![](https://img.haomeiwen.com/i7445074/8019e71efe87c713.png)
使用命令行flush privileges;
使新配置的用户信息立即生效
![](https://img.haomeiwen.com/i7445074/908e71fcab4a1d2f.png)
允许任何IP访问MySQL的root用户
使用root用户登录后,使用use mysql;
命令切换到mysql数据库
![](https://img.haomeiwen.com/i7445074/6af102d6d81527f8.png)
使用命令行select user,host from user;
查询root用户的访问权限
![](https://img.haomeiwen.com/i7445074/1107d3a48429912f.png)
使用命令行grant all privileges on *.* to '需要设置的用户'@'允许访问的IP' identified by '访问的密码' with grant option;
为MySQL增加一行访问权限
例如:grant all privileges on *.* to 'root'@'%' identified by '1' with grant option;
IP设置成%
的意思是允许任何IP的访问
![](https://img.haomeiwen.com/i7445074/5a06b64ea9a24419.png)
使用命令行flush privileges;
使新配置的权限立即生效
![](https://img.haomeiwen.com/i7445074/908e71fcab4a1d2f.png)
再次查询root用户的权限,这次已经多了一个%
的权限了
![](https://img.haomeiwen.com/i7445074/3dc51425a4ab8787.png)