下载步骤:

  1. 下载MySQL数据库可以访问官方网站:https://www.mysql.com/

image.png

  1. 点击DOWNLOADS模块下的Community模块下的MySQL Community Server进行下载。

image.png

  1. 根据自己的电脑选择相应的位数进行下载,本机是64位,点击Download。

image.png

  1. 进入下一个页面,该页面需要注册MySQL账户,也可不进行注册,直接下载。点击No thanks, just start my download,选择自己要下载的目录。

image.png

配置MySQL数据库,配置步骤如下:

MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的。zip格式是自己解压,解压缩之后其实MySQL就可以使用了,但是要进行配置。
如果用户没配置直接使用MySQL,但会出现图示的错误。这是因为没有配置环境变量所致。配置环境变量很简单:
我的电脑->属性->高级->环境变量
选择PATH,在其后面添加: 你的mysql bin文件夹的路径 :D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64\bin
image.png
配置完环境变量之后,还需要修改一下配置文件:
配置文件是在D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64\my-default.ini

  1. # 设置mysql的安装目录
  2. basedir=D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64
  3. # 设置mysql数据库的数据的存放目录
  4. datadir=D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64\data

或者自己建立一个my.ini文件。

  1. [mysqld]
  2. # 设置3306端口
  3. port=3306
  4. # 设置mysql的安装目录
  5. basedir=D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64
  6. # 设置mysql数据库的数据的存放目录
  7. datadir=D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64\data
  8. # 允许最大连接数
  9. max_connections=200
  10. # 允许连接失败的次数。
  11. max_connect_errors=10
  12. # 服务端使用的字符集默认为utf8mb4
  13. character-set-server=utf8mb4
  14. # 创建新表时将使用的默认存储引擎
  15. default-storage-engine=INNODB
  16. # 默认使用“mysql_native_password”插件认证
  17. #mysql_native_password
  18. default_authentication_plugin=mysql_native_password
  19. [mysql]
  20. # 设置mysql客户端默认字符集
  21. default-character-set=utf8mb4
  22. [client]
  23. # 设置mysql客户端连接服务端时默认使用的端口
  24. port=3306
  25. default-character-set=utf8mb4

win+R 输入 cmd 打开命令行
cd D:\Program Files (x86)\MySQL\mysql-8.0.18-winx64\bin
输入 mysqld --initialize-insecure ——-初始化数据库,并设置默认root密码为空,这时候那个data文件夹就会出来了
安装mysql服务
mysqld install mysql
开启mysql服务
net start mysql
停止mysql服务
net stop mysql

  1. PS C:\WINDOWS\system32> mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 7
  5. Server version: 8.0.18 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> select host,user,authentication_string from mysql.user;
  12. +-----------+------------------+------------------------------------------------------------------------+
  13. | host | user | authentication_string |
  14. +-----------+------------------+------------------------------------------------------------------------+
  15. | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  16. | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  17. | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  18. | localhost | root | $A$005$k3xaSo%xW|rDs;nfjTYJxsM3Fg296JP5m.8B4CY92QdSKCobo6m3JICHwQrB |
  19. +-----------+------------------+------------------------------------------------------------------------+
  20. 4 rows in set (1.24 sec)
  21. mysql> use mysql;
  22. Database changed
  23. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysql*';
  24. Query OK, 0 rows affected (0.04 sec)
  25. mysql> select host,user,authentication_string from mysql.user;
  26. +-----------+------------------+------------------------------------------------------------------------+
  27. | host | user | authentication_string |
  28. +-----------+------------------+------------------------------------------------------------------------+
  29. | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  30. | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  31. | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  32. | localhost | root | *81C45E003D67C601301B5129F1AA8C98271E74C4 |
  33. +-----------+------------------+------------------------------------------------------------------------+
  34. 4 rows in set (0.00 sec)

  1. //开启MySQL
  2. C:\WINDOWS\system32>net start mysql
  3. MySQL 服务正在启动 ...
  4. MySQL 服务已经启动成功。
  5. //登陆报错
  6. C:\WINDOWS\system32>mysql -uroot -p
  7. Enter password:
  8. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  9. //关闭MySQL
  10. C:\WINDOWS\system32>net stop mysql
  11. MySQL 服务正在停止.
  12. MySQL 服务已成功停止。
  13. //无密码启动MySQL服务
  14. C:\WINDOWS\system32>mysqld --console --skip-grant-tables --shared-memory
  15. 2018-11-22T14:06:27.964267Z 0 [System] [MY-010116] [Server]
  16. D:\mysql-8.0.13-winx64\bin\mysqld.exe (mysqld 8.0.13) starting as process 2972
  17. 2018-11-22T14:06:30.981556Z 0 [Warning] [MY-010068] [Server]
  18. CA certificate ca.pem is self signed.
  19. 2018-11-22T14:06:31.006330Z 0 [System] [MY-010931] [Server]
  20. D:\mysql-8.0.13-winx64\bin\mysqld.exe: ready for connections.
  21. Version: '8.0.13' socket: '' port: 0 MySQL Community Server - GPL.
  22. 2018-11-22T14:06:31.209604Z 0 [Warning] [MY-011311] [Server]
  23. Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'
  1. //无密码【登陆】(密码处直接enter)
  2. C:\WINDOWS\system32>mysql -uroot -p
  3. Enter password:
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 7
  6. Server version: 8.0.13 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. //-------------------------------------------------免密码登陆设置空密码-------------------------------------------------------
  13. //设置【空密码】
  14. mysql> UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';
  15. Query OK, 1 row affected (0.08 sec)
  16. Rows matched: 1 Changed: 1 Warnings: 0
  17. //刷新
  18. mysql> flush privileges;
  19. Query OK, 0 rows affected (0.06 sec)
  20. //查询
  21. mysql> select host,user,plugin,authentication_string from mysql.user;
  22. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  23. | host | user | plugin | authentication_string |
  24. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  25. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  26. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  27. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  28. | localhost | root | caching_sha2_password | |
  29. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  30. 4 rows in set (0.00 sec)
  31. //---------------------------------------------设置加密的密码---------------------------------------------
  32. //以caching_sha2_password加密密码并设置
  33. mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'admin';
  34. Query OK, 0 rows affected (0.17 sec)
  35. //刷新
  36. mysql> flush privileges;
  37. Query OK, 0 rows affected (0.01 sec)
  38. //查询
  39. mysql> select host,user,plugin,authentication_string from mysql.user;
  40. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  41. | host | user | plugin | authentication_string |
  42. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  43. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  44. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  45. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  46. | localhost | root | caching_sha2_password | $A$005$r/r8"We_EpPb9584lw2cALUsOsvkB/hHg1qUqocVxDMMkFQ8RyQcXASZoff5 |
  47. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  48. 4 rows in set (0.00 sec)
  1. //登陆成功
  2. C:\WINDOWS\system32>mysql -uroot -padmin
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 10
  6. Server version: 8.0.13 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. //修改加密方式
  13. mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'admin';
  14. Query OK, 0 rows affected (0.12 sec)
  15. //刷新
  16. mysql> flush privileges;
  17. Query OK, 0 rows affected (0.03 sec)
  18. //查询
  19. mysql> select host,user,plugin,authentication_string from mysql.user;
  20. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  21. | host | user | plugin | authentication_string |
  22. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  23. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  24. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  25. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
  26. | localhost | root | mysql_native_password | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
  27. +-----------+------------------+-----------------------+------------------------------------------------------------------------+
  28. 4 rows in set (0.00 sec)
  29. mysql>
  1. //---------------------------------------常用语句------------------------------------------------------------
  2. //修改加密规则
  3. mysql> UPDATE mysql.user SET plugin='mysql_native_password' WHERE user='root';
  4. //查询
  5. mysql> select host,user,plugin,authentication_string from mysql.user;
  6. //对密码进行加密
  7. mysql> update mysql.user set password=password('admin') where user='root' and host='localhost';
  8. //刷新
  9. flush privileges;
  10. //退出
  11. mysql> exit
  1. mysql> use mysql;
  2. mysql> update user set password=password('123') where user='root' and host='localhost';
  3. //等价于
  4. mysql> update mysql.user set password=password('123') where user='root' and host='localhost';
  1. C:\WINDOWS\system32>mysql -uroot -padmin
  2. //等价于
  3. C:\WINDOWS\system32>mysql -u root -p admin