Windows安装MySQL8(压缩包版)

Windows安装MySQL8(压缩包版)

下载压缩包


mysql社区版压缩包版zip下载地址在这里插入图片描述

mysql社区版安装包版windows安装包下载地址

解压到想要的位置,

此时目录中还没有data文件夹

将解压目录的bin文件夹配置到环境变量的path, 之后执行mysql或mysqld都用得到, 不配置的话,每次进目录执行都麻烦, 而且安装时要用到控制台的系统管理员权限

win+x → a键 打开带系统管理员权限的命令行

### 初始化,并允许root无密码登陆
mysqld --initialize-insecure --console
### 安装mysql服务, 默认服务名为 mysql , 也可以自定义服务名
mysqld --install
### 启动mysql服务
net start mysql

安装过程的三个命令

  1. mysqld --initialize
  2. mysqld --install
  3. net start mysqlsc start mysql

--initialize 的两横不能省略

--install 的两横可以省略, 可以写成 -installinstall



mysqld --initializemysqld --initialize-insecure 初始化, 生成data文件夹和root密码等

### 会为root生成随机密码, 控制台无显示
mysqld --initialize
### 会为root生成随机密码, 在控制台显示信息
mysqld --initialize --console
### root可以无密码登陆-> mysql -uroot ;  因为无密码, 可以不要 --console 选项, 看不到安装讯息
mysqld --initialize-insecure
### root可以无密码登陆-> mysql -uroot ;  加上 --console 可以看到安装信息
mysqld --initialize-insecure --console

执行 mysqld --initializemysqld --initialize-insecure 前, 必须没有 data 文件夹 , 否则报错, 执行后会生成data文件夹, 想再次初始化的话必须删除data文件夹

  • Use --initialize for “secure by default” installation (that is, including generation of a random initial root password). In this case, the password is marked as expired and you must choose a new one.
    --initialize 会生成随机的初始根密码,密码被标记为过期,必须选择一个新密码。

  • With --initialize-insecure, no root password is generated. This is insecure; it is assumed that you intend to assign a password to the account in a timely fashion before putting the server into production use.
    --initialize-insecure 不会为root生成密码, 可以无密码登陆 mysql -uroot回车 或 mysql -uroot -p回车回车

--console 选项

--initialize 会生成随机的初始根密码, 不会直接输出到控制台, 而是记录在生成的 data文件夹 下的, 以 主机名.err 命名的文件中, 控制台没有反馈信息. 如果想在控制台直接看到反馈信息, 可以加 --console 选项, 使得生成的密码在控制台输出.
加上--console选项后, 相当于反馈信息本截流到控制台了, 不会将信息输出到data文件夹 下的 主机名.err 文件中
经测试 :

  • 使用 mysqld --initialize 时, data文件夹 下会立刻出现名为 主机名.err 的文件 , 包含生成的密码
  • 使用 mysqld --initialize --console 时, data文件夹 不会立刻出现名为 主机名.err 的文件 , 之后才会出现, 并且不包含密码. 密码信息会出现在控制台, 而不会记录在文件,清屏就找不到了. 但也无所谓, 还有其它办法可以免密登陆修改密码
--user=mysql 选项

mysqld 程序在启动后将在给定 UNIX/Linux 账户下执行。mysqld 必须从 root 账户启动才能在启动后切换到另一个账户下执行。mysqld_safe 脚本将默认使用 user=mysql 选项来启动 mysqld 程序。

官方原文👇 2.9.1 Initializing the Data Directory — Data Directory Initialization Procedure

On Unix and Unix-like systems, it is important for the database directories and files to be owned by the mysql login account so that the server has read and write access to them when you run it later. To ensure this, start mysqld from the system root account and include the --user option as shown here:

bin/mysqld --initialize --user=mysql
bin/mysqld --initialize-insecure --user=mysql

Alternatively, execute mysqld while logged in as mysql, in which case you can omit the --user option from the command.

On Windows, use one of these commands:

bin\mysqld --initialize --console
bin\mysqld --initialize-insecure --console

想要看到安装信息, Windows加 --console

mysqld --initialize --console
mysqld --initialize-insecure --console

想要看到安装信息, Unix加--user=mysql

mysqld --initialize --user=mysql
mysqld --initialize-insecure --user=mysql

都加上好像也可以

mysqld --initialize --console --user=mysql
mysqld --initialize-insecure --console --user=mysql

官方对 --console 选项的说明
官方对 --user 选项的说明



mysqld --install 安装MySQL服务

--initialize 的两横不能省略

--install 的两横可以省略, 可以写成 -installinstall

mysqld install
mysqld -install
mysqld --install

执行 mysqld --install 必须要管理员权限 ! ,
在这里插入图片描述
有管理员权限时

不指定服务名时, mysqld --install 会生成名为 mysql 的 MySQL服务 , 相当于 mysqld --install mysql

mysqld --install 等效 mysqld --install mysql  ---安装了名为mysql的MySQL服务
net start mysql  --启动MySQL服务
net stop mysql   --停止MySQL服务
mysqld --remove 等效 mysqld --remove mysql    ---移除了名为mysql的MySQL服务
mysqld --install 可后接自定义的服务名

mysqld --install也可以 指定服务名, 例如指定服务名为 yousql : mysqld --install yousql

mysqld --install yousql   ---安装了名为yousql的MySQL服务
net start yousql --启动MySQL服务
net stop yousql  --停止MySQL服务
mysqld --remove yousql    ---移除了名为yousql的MySQL服务



启动MySQL服务的几种方法

Windows可以用 net start 服务名sc start 服务名sc.exe start 服务名 来启动服务

net start mysql

sc start mysql

sc 在cmd中可以写成scsc.exe
sc 在powershell中要写成sc.exe

sc.exe start mysql
Start-Service -Name "mysql"

mysql是MySQL默认的服务名, 可以在执行 mysqld --install 服务名 时指定,
省略服务名mysqld --install , 相当于 mysqld --install mysql
net start mysqlsc start mysql 启动MySQL服务

对应的,停止MySQL服务的几种方法

net stop mysql
sc stop mysql
sc.exe stop mysql
Stop-Service -Name mysql

sc query 服务名 查看服务

sc query mysql
sc.exe query mysql



安装完成后创建远程用户设置密码并授权


创建远程用户

MySQL5.7可以 (创建用户,设置密码,授权) 一步到位 👇

GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;

👆这样的语句在MySQL8.0中行不通,
mysql8必须 创设和授权 分步执行👇

CREATE USER '名'@'%' IDENTIFIED BY '密';  -- 创建用户并指定密码
GRANT ALL PRIVILEGES ON *.* TO '名'@'%' WITH GRANT OPTION;  --授权

也可以分三步 , 不含特殊字符的用户名可以不加单引号

CREATE USER@'%' ;  -- 创建用户
ALTER  USER@'%' IDENTIFIED BY '密';  -- 指定密码
GRANT ALL PRIVILEGES ON *.* TO@'%' WITH GRANT OPTION;  -- 授权

刷新权限设置

FLUSH PRIVILEGES;

可以写在一行,以分号分隔

比如创建一个名为remote的用户

CREATE USER IF NOT EXISTS 'remote'@'%' IDENTIFIED BY 'remote'; GRANT ALL ON *.* TO 'remote'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
创建远程root的语句模板

自带的 root@'localhost' 只能本地登陆,
创建 root@'%' , 并将 root@‘localhost’ 的权限授予 root@‘%’

CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '密码'; GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

创建 root@‘%’ , 并让 root@'%'扮演root@‘localhost’ 的角色 , 并设置为默认角色

CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '密码';
GRANT root@'localhost' TO 'root'@'%'; SET DEFAULT ROLE root@'localhost' TO 'root'@'%';
FLUSH PRIVILEGES;

权限和角色两者都加持

CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY '密码';
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;
GRANT root@'localhost' TO 'root'@'%';
SET DEFAULT ROLE root@'localhost' TO 'root'@'%';
FLUSH PRIVILEGES;

或者直接将 root@localhost 改为 root@%

update mysql.user set host='%' where user='root'; flush privileges;
UPDATE mysql.user SET host='%' WHERE user='root'; FLUSH PRIVILEGES;



停止服务,移除服务,卸载

停止服务
net stop mysql
sc.exe stop mysql
Stop-Service -Name mysql
查看服务状态
sc.exe query mysql
get-service -name mysql
卸载服务

卸载,删除,移除MySQL服务可以用 mysqld --remove MySQL服务名sc delete 服务名

  • mysqld --remove MySQL服务名
  • sc.exe delete 服务名

mysqld是MySQL提供的命令 , sc是Windows提供的命令

mysqld --remove MySQL服务名 移除MySQL服务, 如果不指定服务名, 则服务名=mysql

mysqld --remove相当于mysqld --remove mysql

mysqld --remove
mysqld --remove mysql
sc.exe delete mysql
### powershell版本必须大于等于6
Remove-Service -Name mysql
删除解压的文件夹



配置文件(可以没有)

没有配置文件也可以按默认设置运行,

想配置的话, 新建文本文件名 my.inimy.cnf 在解压目录, (不是bin目录, 放在bin目录下无效)

模板1:

[mysqld]

### 设置端口
port=3306

### 设置mysql的安装目录
basedir=D:\mysql8

### 设置mysql数据库的数据的存放目录
datadir=D:\mysql8\Data  

### 允许最大连接数
max_connections=200

### 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10

### 服务端使用的字符集默认为UTF8
character-set-server=utf8

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

### 使用5.7版“mysql_native_password”插件认证 , 默认是"caching_sha2_password"
# default_authentication_plugin=mysql_native_password
# default_authentication_plugin=caching_sha2_password




[mysql]

### 设置mysql客户端默认字符集
default-character-set=utf8mb4



[client]
### 设置mysql客户端连接服务端时,不指定对端口时,默认使用的端口
port=3306

default-character-set=utf8

模板2

########################################################################################################## mysqld
[mysqld]

### 跳过密码认证 5版只需skip-grant-tables ,  8.0版 要skip-grant-tables 和 shared-memory
#skip-grant-tables
#shared-memory


### 设置端口
port=3306


### 设置mysql的安装目录
# basedir=D:\mysql8


### 设置mysql数据库的数据的存放目录
#datadir=D:\mysql8\Data  


### 允许最大连接数
max_connections=200


### 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10


### 服务端使用的字符集默认为UTF8
character-set-server=utf8


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


### 使用5.7版“mysql_native_password”插件认证 , 默认是"caching_sha2_password"
# default_authentication_plugin=mysql_native_password
# default_authentication_plugin=caching_sha2_password





########################################################################################################## mysql
[mysql]

### 设置mysql客户端默认字符集
default-character-set=utf8mb4




########################################################################################################### client
[client]

### 设置mysql客户端连接服务端时,不指定对端口时,默认使用的端口
port=3306

default-character-set=utf8



在命令行用自带的客户端登陆 mysql -u root -p

用户名和密码, 在有特殊字符时, 要用’单引号’或"双引号"夹住,
没有特殊字符时,可以不要
-u和用户名之间可以没有空格, 也可以有
-p和密码之间不能有空格, -p空格后接的是数据库名
-h和主机名(IP地址)直接必须有空格,
-P和端口之间可以有空格,也可以没有空格,
(小写p对应密码,大写P对应端口)

例如

在本机登陆

以root为例,假设无密码

mysql -u root
mysql -u 'root'
mysql -u "root"
mysql -uroot
mysql -u'root'
mysql -u"root"
mysql -uroot -p 回车回车

假设用户名是: u , 密码是: 123

mysql -uu -p回车123回车
mysql -uu -p123
mysql -uu -p'123'
mysql -uu -p"123"
在远端登陆, -h指定主机名(ip) -P指定端口(大写P)
mysql -u'u' -p'p' -h 192.168.13.14 -P3306
mysql -u'u' -p'p' -h 192.168.13.14 -P"3306"
mysql -u'u' -p'p' -h'192.168.13.14' -P'3306'

加上单或双引号后, 都可以没有空格

mysql -u"name" -p"passwd" -h"hostname" -P"3306"



忘记密码怎么办

首先停止服务 net stop mysqlsc stop mysql , (win11的powershell用netsc.exe)

然后

方法1 用到两个窗口, 一个执行 mysqld --console --skip-grant-tables --shared-memory 命令

此方法需要用到 两个 控制台命令行窗口 , 需要管理员权限
在第一个窗口输入👇

mysqld --skip-grant-tables --shared-memory

mysqld --skip-grant-tables --shared-memory --console

此时不用启动服务, 也启动不了
此时,可以在另一个窗口无密码登陆了, 存在的用户都可以无密码登陆, 加上-p后乱输密码也可以

mysql -uroot

修改密码

ALTER USER u@'%' IDENTIFIED BY '密';

做完要做的事后, 把第一个窗口关闭, 然后就可以启动服务了

方法2 用到配置文件, 加入 skip-grant-tablesshared-memory选项

配置文件在安装目录, 可能没有,
没有的话,自己创建, 取名 my.ini 或者 my.cnf 都可以

MySQL5版本可能只需要加 skip-grant-tables,
MySQL8版本必须 skip-grant-tablesshared-memory 两项都加入

  • 如果已有配置文件,加到 [mysqld] 下面
### 跳过密码认证 5版只需skip-grant-tables ,  8.0版 要skip-grant-tables 和 shared-memory
skip-grant-tables
shared-memory
  • 如果没有配置文件, 则自己创建到安装目录, 取名 my.ini 或者 my.cnf 都可以 , 填写下面👇内容
[mysqld]
### 跳过密码认证 5版只需skip-grant-tables ,  8.0版 要skip-grant-tables 和 shared-memory
skip-grant-tables
shared-memory

启动服务, 修改密码, 停止服务, 取消或移除配置文件的skip-grant-tablesshared-memory 两项,
再次启动服务 , 完成



解决 VCRUNTIME140_1.dll 的方法

有时会缺少运行库,win7,8,10 ; 可能win10-22h2缺,可能win10-21h2却不却, 没深究

下载安装 微软常用运行库合集

微软常用运行库合集-果核剥壳版
微软常用运行库合集-果核剥壳版 – MSVBCRT.AIO.2022.10.21.zip

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值