转自https://blog.csdn.net/hellboy0621/article/details/80458892
1、官网下载对应版本,下载地址为https://dev.mysql.com/downloads/mysql/5.7.html#downloads,我下载的文件为“mysql-5.7.22-winx64.zip”;
2、考虑到数据库容量会增加,建议解压缩到可用容量较大的盘符,我解压缩到D盘;
3、进入解压缩目录,新建my.ini文件,内容如下:
-
[mysqld]
-
# set basedir to your installation path
-
basedir=D:/mysql-5.7.22-winx64
-
# set datadir to the location of your data directory
-
datadir=D:/mysql-5.7.22-winx64/data
4、使用cmd进入解压缩目录下的bin/目录下
-
# 安装mysql服务,安装完成后,可在右键“计算机”-“管理”-“服务”中看到,默认服务名为“MySQL”,也可以在install后面指定服务名称
-
D:\mysql-5.7.22-winx64\bin>mysqld install
-
Service successfully installed.
-
# 初始化data目录
-
D:\mysql-5.7.22-winx64\bin>mysqld.exe --initialize
初始化后会在datadir路径下生成一个*.err的文件,打开如下:
-
2018-05-27T04:49:08.182952Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
-
2018-05-27T04:49:11.016957Z 0 [Warning] InnoDB: New log files created, LSN=45790
-
2018-05-27T04:49:11.546958Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
-
2018-05-27T04:49:11.676958Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4c70979b-6169-11e8-9db4-00ff13a6d817.
-
2018-05-27T04:49:11.676958Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
-
2018-05-27T04:49:11.696958Z 1 [Note] A temporary password is generated for [email protected]: q&10*I#<fi-w
最后一行“A temporary password is generated for [email protected]: q&10*I#<fi-w”冒号后面的就是临时密码;
-
# 启动MySQL服务
-
D:\mysql-5.7.22-winx64\bin>net start mysql
-
MySQL 服务正在启动 .
-
MySQL 服务已经启动成功。
5、修改密码
后面有两种方式可以修改密码,一种是使用刚才生成的临时密码登录修改,另一种是修改配置文件跳过密码登录然后修改密码;
5.1 通过临时密码登录
-
# 使用临时密码登录
-
E:\mysql-5.7.22-winx64\bin>mysql -u root -p
-
Enter password: ************
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 4
-
Server version: 5.7.22
-
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> show global variables like ‘port‘;
-
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
-
# 通过ALTER语句修改密码
-
mysql> ALTER user ‘root‘@‘localhost‘ identified by ‘Jiubugaosuni_01‘;
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> SHOW GLOBAL VARIABLES LIKE ‘port‘;
-
+---------------+-------+
-
| Variable_name | Value |
-
+---------------+-------+
-
| port | 3306 |
-
+---------------+-------+
-
1 row in set, 1 warning (0.01 sec)
5.2 跳过密码登录
5.2.1 跳过密码登录MySQL
-
# 停止MySQL服务
-
D:\mysql-5.7.22-winx64\bin>net stop mysql
-
MySQL 服务正在停止.
-
MySQL 服务已成功停止。
-
# 卸载MySQL服务
-
D:\mysql-5.7.22-winx64\bin>mysqld remove
-
Service successfully removed.
-
# 编辑my.ini配置文件,在[mysqld]下添加skip-grant-tables
-
[mysqld]
-
# set basedir to your installation path
-
basedir=D:/mysql-5.7.22-winx64
-
# set datadir to the location of your data directory
-
datadir=D:/mysql-5.7.22-winx64/data
-
skip-grant-tables
-
# 安装MySQL服务
-
D:\mysql-5.7.22-winx64\bin>mysqld install
-
Service successfully installed.
-
# 启动MySQL服务
-
D:\mysql-5.7.22-winx64\bin>net start mysql
-
MySQL 服务正在启动 .
-
MySQL 服务已经启动成功。
5.2.2 无密码登录
-
D:\mysql-5.7.22-winx64\bin>mysql -u root -p
-
Enter password:(回车,无需输入密码)
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.22 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>
-
# 在这里不能使用ALTER语句,否则报错
-
mysql> ALTER user ‘root‘@‘localhost‘ identified by ‘Jiubugaosuni_01‘;
-
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this
-
statement
-
# 而应该使用如下命令,设置临时密码
-
mysql> use mysql;
-
Database changed
-
mysql> UPDATE user SET authentication_string = password("Jiubugaosuni_01") WHERE user = "root";
-
Query OK, 1 row affected, 1 warning (0.07 sec)
-
Rows matched: 1 Changed: 1 Warnings: 1
-
mysql> FLUSH PRIVILEGES;
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> QUIT;
-
Bye
5.2.3 恢复密码登录
-
# 停止MySQL服务
-
D:\mysql-5.7.22-winx64\bin>net stop mysql
-
MySQL 服务正在停止.
-
MySQL 服务已成功停止。
-
# 卸载MySQL服务
-
D:\mysql-5.7.22-winx64\bin>mysqld remove
-
Service successfully removed.
-
# 修改my.ini配置文件,注释或者删除skip-grant-tables
-
[mysqld]
-
# set basedir to your installation path
-
basedir=D:/mysql-5.7.22-winx64
-
# set datadir to the location of your data directory
-
datadir=D:/mysql-5.7.22-winx64/data
-
# skip-grant-tables
-
# 安装MySQL服务
-
D:\mysql-5.7.22-winx64\bin>mysqld install
-
Service successfully installed.
-
# 启动MySQL服务
-
D:\mysql-5.7.22-winx64\bin>net start mysql
-
MySQL 服务正在启动 .
-
MySQL 服务已经启动成功。
5.2.4 登录并查看端口号
-
# 使用修改的临时密码登录
-
D:\mysql-5.7.22-winx64\bin>mysql -u root -p
-
Enter password: ***************
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 3
-
Server version: 5.7.22
-
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.
-
# 必须通过ALTER语句修改密码才算正式修改密码成功
-
mysql> show global variables like ‘port‘;
-
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
-
mysql> ALTER user ‘root‘@‘localhost‘ identified by ‘Jiubugaosuni_01‘;
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> SHOW GLOBAL VARIABLES LIKE ‘port‘;
-
+---------------+-------+
-
| Variable_name | Value |
-
+---------------+-------+
-
| port | 3306 |
-
+---------------+-------+
-
1 row in set, 1 warning (0.04 sec)
-
mysql> show databases;
-
+--------------------+
-
| Database |
-
+--------------------+
-
| information_schema |
-
| mysql |
-
| performance_schema |
-
| sys |
-
+--------------------+
-
4 rows in set (0.00 sec)
-
mysql>
6、配置远程登录
-
# 赋予全部权限在所有数据库和所有表上给root用户在任何主机上使用Jiubugaosuni_01这个密码登录
-
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘Jiubugaosuni_01‘ WITH GRANT OPTION;
-
Query OK, 0 rows affected, 1 warning (0.00 sec)
-
mysql> FLUSH PRIVILEGES;
-
Query OK, 0 rows affected (0.02 sec)
原文地址:https://www.cnblogs.com/Gru-blog/p/9307981.html