首先明确一下 mysql server只是一个服务,一般是通过终端命令行进行操作,如果需要可视化界面的话需要另外安装,个人推荐DBeaver。
1、下载、解压
mysql server不需要付费,直接在官网就能下载 https://www.mysql.com/
下载下来后直接解压到指定目录就行。本文以路径 D:\mysql-8.0.27-winx64\mysql-8.0.27-winx64为例。
2、配置环境变量
我的电脑-》属性-》高级系统设置-》环境变量,编辑系统变量内的path,在最后面添加 D:\mysql-8.0.27-winx64\mysql-8.0.27-winx64\bin;
3、配置mysql
1、在D:\mysql-8.0.27-winx64\mysql-8.0.27-winx64\目录下新增data文件夹(初始化mysql时会用到,我当时不知道,导致后面启动服务一直报错)
2、新建my.ini文件,内容如下,注意!basedir和datadir要改成你自己的路径:
# 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.
[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 = .....
# 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
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\mysql-8.0.27-winx64\mysql-8.0.27-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:\mysql-8.0.27-winx64\mysql-8.0.27-winx64\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
完成后结构大概是这个样子
4、安装mysql
1、管理员身份打开cmd,进入bin目录下。
2、执行命令 mysql -install ,看到提示 Service successfully installed, 说明安装成功。不成功请自行百度报错。
5、启动mysql
1、cmd输入命令:net start mysql
重点来了,8.*的mysql在输入初始化密码后会报错 ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
,了解了一下好像是因为caching_sha2_password加密的问题,而修改密码后加密方法是:mysql_native_password。
解决方法:
1、先关闭mysql服务: net stop mysql
2、设置跳过权限表启动服务: mysqld --console --skip-grant-tables --shared-memory;
3、打开一个新的cmd窗口,原来的那个不要关闭!
4、新cmd进入mysql:mysql -u root -p
5、提示输入密码时直接回车
成功进入mysql
6、查询所有用户的部分信息,可以发现plugin列都是caching_sha2_password
select host,user,plugin,authentication_string from mysql.user;
7、修改密码和plugin
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
8、刷新权限
flush privileges;
9、退出后重新登录,能够成功登录。