MySQL 的安装 For Windows(存档版本)

2024 年 6 月 3 日 星期一(已编辑)
/
65
这篇文章上次修改于 2024 年 6 月 22 日 星期六,可能部分内容已经不适用,如有疑问可询问作者。

MySQL 的安装 For Windows(存档版本)

目标

  • 找到官网并安装当前最新版MySQL
  • 解决安装时遇到的问题
  • 解决DBeaverNavicat等数据库连接工具连接数据库时遇到的部分问题

找官网

MySQL

https://www.mysql.com

下载所需的版本

教程视频

安装

解压

将下载好的mysql-x.x.x-winx64.zip文件解压,将解压到的mysql-x.x.x-winx64文件夹移动或复制到其他你想要安装到的位置,比如我打算安装在D:\Program Files下,那么MySQL的根目录为D:\Program Files\mysql-8.4.0-winx64

目录结构

D:\PROGRAM FILES\MYSQL-8.4.0-WINX64
├─ LICENSE
├─ my.ini
├─ README
├─bin
│  └─sasl2
├─data
│  ├─#innodb_redo
│  ├─#innodb_temp
│  ├─mysql
│  ├─performance_schema
│  └─sys
├─docs
├─include
│  ├─mysql
│  └─openssl
├─lib
│  ├─mecab
│  │  ├─dic
│  │  │  ├─ipadic_euc-jp
│  │  │  ├─ipadic_sjis
│  │  │  └─ipadic_utf-8
│  │  └─etc
│  ├─plugin
│  │  └─debug
│  └─private
│      └─icudt73l
│          └─brkitr
└─share
    ├─bulgarian
    ├─charsets
    ├─czech
    └─···

配置my.ini

my.ini文件写入到MySQL的根目录下(默认是没有这个my.ini文件的,需要自己新建)

[mysql]
  # 设置mysql客户端默认编码
default-character-set=utf8
[mysqld]
  # 设置mysql8.0的安装目录,填写`MySQL`的根目录,将路径`\`换为`\\`
basedir=D:\\Program Files\\mysql-8.4.0-winx64
[client]
  #设置客户端编码
default-character-set=utf8

配置环境变量

[视频(还没录)]

初始化MySQL

使用 管理员身份(必须,不然后面会报权限错误) 打开cmd或者powershell,输入以下初始化命令,会获得一个临时root账户的密码,需要复制下来,对后面的步骤很重要

mysqld --initialize --console

输入与输出:

C:\Users\Administrator>mysqld --initialize --console
2024-06-04T01:57:59.068475Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2024-06-04T01:57:59.074681Z 0 [System] [MY-013169] [Server] C:\Program Files\mysql-8.4.0-winx64\bin\mysqld.exe (mysqld 8.4.0) initializing of server in progress as process 21460
2024-06-04T01:57:59.115670Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-06-04T01:57:59.599542Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-06-04T01:58:01.104784Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: kq7vybL/fJ<j
2024-06-04T01:58:03.177166Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.

日志中可见,我的root用户临时密码为kq7vybL/fJ<j

安装mysql

在刚打开的cmd或者powershell,输入以下命令

mysqld --install

输入与输出

C:\Users\Administrator>mysqld --install
Service successfully installed.

启动服务

继续输入以下命令

net start mysql

输入与输出

C:\Users\Administrator>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。

登录MySQL

C:\Users\Administrator>mysql -uroot -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.4.0

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
C:\Users\Administrator>mysql -uroot -p
Enter password:

设置新密码

我想将root用户的密码设置为root1234,输入以下命令

set password for 'root'@'localhost'='root1234';

输入与输出:

mysql> set password for 'root'@'localhost'='root1234';
Query OK, 0 rows affected (0.02 sec)

卸载MySQL

退出登录状态

\q

停止服务

net stop mysql

卸载

mysqld -remove mysql

然后再删掉安装目录的文件就可以了

解决疑难杂症

使用DBeaverNavicat等数据库连接工具连接数据库时发生的症状

问题1:Public Key Retrieval is not allowed

解决方法1

可以采用URL连接,在链接末尾加上?allowPublicKeyRetrieval=true

?表示要连接到的库为空,此时可以在连接工具中连接所有库

jdbc:mysql://localhost:3306/?allowPublicKeyRetrieval=true

问题2:null, message from server: "Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server"

解决方法

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...