一、下载MySQL
首先,请前往下列地址下载对应版本的MySQL安装包:https://dev.mysql.com/downloads/mysql/
二、安装MySQL
很多网上教程都说,下载好mysql-8.0.19安装文件之后,在安装之前需要手动创建my.ini
配置文件,实际上至少从mysql-8.0.11往后就不再需要这一步了。
1. 初始化
- 对于Windows 7:
- 先进入mysql安装包解压后的文件夹中,然后进入名称为
bin
的目录; - 然后,在空白处按住
shift
后点右键点击在此处点开命令窗口(W)
; - 最后,敲入:
mysqld --initialize --console
,然后你会看到下列输出:
- 先进入mysql安装包解压后的文件夹中,然后进入名称为
D:\software\mysql\bin>mysqld --initialize --console
2020-08-12T07:49:11.417102Z 0 [System] [MY-013169] [Server] D:\software\mysql\bin\mysqld.exe (mysqld 8.0.19) initializing of server in progress as process 10872
2020-08-12T07:49:14.356600Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: *qi8djiXzUBb
请务必注意,上述
*qi8djiXzUBb
是系统生成的默认密码,后续首次登录前要用到!
- 对于Windows 10,上述步骤基本一致,只是一开始打开cmd的步骤需要注意一下:
在当前文件打开CMD的方式请见:Win10在当前目录快速打开cmd的方法(推荐方法二)
2. 安装服务
当使用下列命令看到Service successfully installed.
字样就说明服务已成功安装。
D:\software\mysql\bin>mysqld --install
Service successfully installed.
3. 启动服务
使用下列命令启动MySQL服务:
D:\software\mysql\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。
4. 登录
下面首次登陆MySQL时需要用到初始化时系统生成的默认密码:
D:\software\mysql\bin>mysql -uroot -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19
5. 修改密码
要正常使用MySQL之前,还需要设置MySQL的密码,下面将密码设置成了root
。
mysql> set password for root@localhost='root';
Query OK, 0 rows affected (0.02 sec)
接着,执行下列命令,可知密码已成功修改:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.19 |
+-----------+
1 row in set (0.00 sec)
mysql> quit;
Bye
D:\software\mysql\bin>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
三、VS Code连接MySQL
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.01 sec)