Windows 安装 MySQL 5.7

MySQL 5.7.xx ZIP版本配置方案

MySQL 5.7.22 官方下载地址
MySQL 官方配置参数查询

一、下载 MySQL 5.7.xx zip 安装包

本机以 Windows10 64 位系统为例
进入 Oracel 官网,选择 MySQL Windows (x86, 64-bit), ZIP Archive 下载包
点此下载


二、解压安装包

将MySQL数据库解压至目录 D:\Program Files\MySQL\mysql-5.7.xx-winx64
D:\Program Files\MySQL\mysql-5.7.xx-winx64\bin 添加至环境变量 Path


三、创建一个空文件夹,命名为“data”,用来存放数据

本机创建在 D:\Program Files\MySQL\mysql-5.7.xx-winx64 目录下,所以最后会看到D:\Program Files\MySQL\mysql-5.7.xx-winx64\data,此文件夹后面会有用到


四、配置启动配置文件

把 D:\Program Files\MySQL\mysql-5.7.xx-winx64\my-default.ini 文件复制一份,重新命名为 my.ini,修改里面的几个参数, 配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/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.

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
character-set-server=utf8

# 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.
# server_id = .....
# MySQL 安装目录
basedir = D:\Program Files\MySQL\mysql-5.7.22-winx64
# MySQL 数据库数据存放目录
datadir = D:\Program Files\MySQL\mysql-5.7.22-winx64\data
# MySQL 端口
port = 3306
# 允许最大连接数,默认200
max_connections=200


# 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

# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

# explicit_defaults_for_timestamp=true

五、初始化数据目录

  • 管理员身份打开 CMD;
  • 当前路径定位到 D:\Program Files\MySQL\mysql-5.7.xx-winx64\bin
  • 按照mysql官方文档给出的步骤还有一步 Selecting a MySQL Server Type,就是选择用 mysqld 还是 mysqld-debug.

一般使用如下命令:

mysqld –defaults-file=”D:\Program Files\MySQL\mysql-5.7.xx-winx64\my.ini” –initialize-insecure

注:

  • 这个命令很重要,它会初始化 data 目录,在执行此命令前请先把 data 目录下的所有文件先删除,否则会失败;
  • 可以选择用 –initialize-insecure 或者 –initialize 来初始化,–initialize-insecure 初始化 root 密码为空,如果用 –initialize 来初始化,会产生一个随机密码;
  • 执行成功后你会在 data 目录 下看到 mysql,perofrmance_schema,sys 目录,同时还会有一些文件。

六、安装 mysql 服务

mysqld -install
mysqld -remove (出问题重新配置前需要卸载服务)


七、启动 mysql 服务、添加初始密码

net start mysql
mysqladmin -u root password 123456
mysql -u root -p


八、设置默认编码

先确保my.ini添加以下代码

1
2
3
4
5
6
7
8
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8 或者 character-set-server=utf8

通过命令查看数据库当前编码

mysql -u root -p
show variables like ‘character%’;

如果不是以下编码,则设置编码

1
2
3
4
5
6
7
8
9
10
11
12
13
+--------------------------+------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:\Program Files\MySQL\mysql-5.7.22-winx64\share\charsets\ |
+--------------------------+------------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

设置编码:

SET character_set_client = utf8;
SET character_set_connection = utf8;
SET character_set_database = utf8;
SET character_set_results = utf8;
SET character_set_server = utf8;

  • character_set_client为客户端编码方式;
  • character_set_connection为建立连接使用的编码;
  • character_set_database数据库的编码;
  • character_set_results结果集的编码;
  • character_set_server数据库服务器的编码;

九、mysql 开启远程连接

grant all privileges on *.* to ‘root‘@’%’ identified by ‘123456’ with grant option;

后记:
如果中途有什么步骤失败,一定要执行 mysql 移除命令,然后删除 data 目录下的所有文件,然后删除服务 mysqld -remove
特别提醒:5.7 和 5.6 不太相同,要自己初始化数据 data 目录,不然启动不会成功


十、后记

mysql 安装完后可能会用到的语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
alter user 'root'@'localhost' identified by '123';
create database victor;
create user 'victor'@'localhost' identified by 'victor@123';
flush privileges;
creaet user 'victorremote'@'%' identified by 'victorremote@123';
flush privileges;
grant all on victor.* to 'victor'@'localhost' identified by 'victor@123';
flush privileges;
grant all on victor.* to 'victorremote'@'%' identified by 'victorremote@123';
flush privileges;

################################
#连接最大空闲时长
SHOW GLOBAL VARIABLES LIKE 'wait_timeout';
#最大连接数
SHOW VARIABLES LIKE '%max_connections%';
SET GLOBAL max_connections = 200;
#字符编码
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
SHOW GLOBAL STATUS LIKE 'innodb_buffer_pool_pages_%';
SHOW PROCESSLIST;
SHOW STATUS LIKE 'Table%';
SHOW STATUS LIKE '%lock%';
################################

If these articles are helpful to you, you can donate comment here.