windows 安装mysql

create-time : 2019-1-9 22:55:40

1.下载MySQL Community Server
  • 根据自己的需要下载不同类型的MySQL

MySQL Community Server

根据自身需要,下载版本。Windows(x86,64-bit),ZIP Archive

2.配置环境变量

解压包,配置包的路径到环境变量中。如F:\MySQL\mysql-8.0.13-winx64\bin

  • [环境变量添加]: windows搜索栏搜环境变量 --> 编辑环境变量 --> 环境变量 --> 点 path,编辑 --> 添加 --> 添加路径–> 完成。
3.生成data 文件
  • 管理员身份进入cmd, 进入上述的mysql路径/bin 下执行命令
    mysqld --initialize-insecure --user=mysql
# system cmd
F:\MySQL\mysql-8.0.13-winx64\bin>mysqld --initialize-insecure --user=mysql
4.安装MySQL
  • 管理员模式cmd 命令mysqld --install
# system cmd
F:\MySQL\mysql-8.0.13-winx64\bin>mysqld --install
Service successfully installed.

如果出现错误:Install/Remove of the Service Denied!,请确保是使用 管理员模式下的cmd

F:\MySQL\mysql-8.0.13-winx64\bin>mysqld --install
Install/Remove of the Service Denied!
5.启动MySQL
  • 管理员模式cmd 命令 :net start MySQL
# system cmd
F:\MySQL\mysql-8.0.13-winx64\bin>net start MySQL
MySQL 服务正在启动 ....
MySQL 服务已经启动成功。
6.登录MySQL
  • cmd : mysql -u root -p。 -u 用户名,-p 密码
  • 前面没有设置 mysql root 账户密码,直接回车即可。后面可以设置 密码
F:\MySQL\mysql-8.0.13-winx64\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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>
6.查看用户密码
mysql>  SHOW DATABASES;   # SQL: 显示数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

# 用户密码在mysql 数据库 user 表中
mysql> SELECT host,user,authentication_string FROM mysql.user;  
+-----------+------------------+------------------------------------------------------------------------+
| host      | user             | authentication_string                                                  |
+-----------+------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root             |                                                                        |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
7.更新密码
  • MySQL8.0 不能通过直接修改 mysql.user 表来更改密码。

因为authentication_string字段下只能是MySQL加密后的43位字符串密码,其他的导致错误。错误不报出,但是无法再登录mysql,总是会提示 无法认证。
参考:MySQL8.0

mysql> USE mysql;
Database changed
mysql> UPDATE user SET authentication_string="123456" WHERE user="root";
Query OK, 1 row affected (0.39 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH privileges;  # 刷新保存
Query OK, 0 rows affected (0.13 sec)

# mysql 5.7.9 之后取消了`password` 函数,`authentication_string=password("123456")` 会报错

C:\WINDOWS\system32>mysql -u root -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

如果 你已经这样更改密码,并且导致了无法进入mysql。本人表示同情之时,还为了你提供了详细的解决方案。请查看本文备注

- 正确更改密码的方式

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "your_password";

mysql> USE mysql;
Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIEED WITH mysql_native_password BY "markjun";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIEED WITH mysql_native_password BY "markjun"' at line 1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "markjun";
Query OK, 0 rows affected (0.18 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "123456";
Query OK, 0 rows affected (0.08 sec)

mysql> SELECT user, authentication_string FROM user;
+------------------+------------------------------------------------------------------------+
| user             | authentication_string                                                  |
+------------------+------------------------------------------------------------------------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9                              |
+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> FLUSH privileges;
Query OK, 0 rows affected (0.38 sec)

mysql> quit;
Bye

F:\MySQL\mysql-8.0.13-winx64\bin>mysql -u root -p
Enter password: *******
...

mysql>

备注: 清空root密码

    1. 停止 MySQL 任务 net stop MySQL
    1. mysqld 命令 mysqld --console --skip-grant-tables --shared-memory
    1. 无密码进入mysql mysql -u root
    1. 清空root 密码 UPDATE user SET authentication_string="" WHERE user="root";

参考:MySQL8下完了密码后重置的方法

C:\WINDOWS\system32>mysqld --console --skip-grant-tables --shared-memory
2019-01-09T16:14:55.317570Z 0 [System] [MY-010116] [Server] F:\MySQL\mysql-8.0.13-winx64\bin\mysqld.exe (mysqld 8.0.13) starting as process 10796
2019-01-09T16:14:59.243764Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-01-09T16:14:59.295223Z 0 [System] [MY-010931] [Server] F:\MySQL\mysql-8.0.13-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.13'  socket: ''  port: 0  MySQL Community Server - GPL.
2019-01-09T16:14:59.323591Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'

另一个终端无密码进入

F:\MySQL\mysql-8.0.13-winx64\bin>mysql -u root -p
Enter password: *******
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

F:\MySQL\mysql-8.0.13-winx64\bin>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
...
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> USE mysql;
Database changed

mysql> SELECT user, authentication_string FROM user;
+------------------+------------------------------------------------------------------------+
| user             | authentication_string                                                  |
+------------------+------------------------------------------------------------------------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | 123456                                                                |
+------------------+------------------------------------------------------------------------+
4 rows in set (0.34 sec)

mysql> UPDATE user SET authentication_string="" WHERE user="root";
Query OK, 1 row affected (0.20 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT user, authentication_string FROM user;
+------------------+------------------------------------------------------------------------+
| user             | authentication_string                                                  |
+------------------+------------------------------------------------------------------------+
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             |                                                                        |
+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> FLUSH privileges;
Query OK, 0 rows affected (0.10 sec)

mysql> quit;
Bye

停止 mysqld 任务,Ctrl+C 结束任务,或者直接关闭 运行 mysqld 的 cmd 终端。

# 需要先停止运行上述 mysqld 任务,否则报错
F:\MySQL\mysql-8.0.13-winx64\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务无法启动。

服务没有报告任何错误。

请键入 NET HELPMSG 3534 以获得更多的帮助。

# 先停止上述 mysqld 任务
F:\MySQL\mysql-8.0.13-winx64\bin>net start mysql
MySQL 服务正在启动 ...
MySQL 服务已经启动成功。

# 现在 mysql root 已经没有了密码
F:\MySQL\mysql-8.0.13-winx64\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.

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

mysql>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值