Windows10 安装 MySQL 并开启远程访问

首先去官网下载MySQL

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

我是下载的5.7.23 zip版本,解压到C:mysql-5.7.23-winx64

然后配置环境变量:

变量名:MYSQL_HOME

变量值:C:mysql-5.7.23-winx64

在Path变量值最前面添加:

%MYSQL_HOME%in;

 在根目录下建立my.ini配置文件

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
 
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=C:mysql-5.7.23-winx64
# 设置mysql数据库的数据的存放目录
datadir=C:mysql-5.7.23-winx64data
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

以管理员权限打开CMD,切换到C:mysql-5.7.23-winx64in目录

使用下面的命令进行安装:

mysqld install 

初始化

mysqld --initialize --console

使用下面的命令启动MySQL:

net start mysql 

然后使用下面的命令连接MySQL:

mysql -uroot -p

提示需要输入密码的时候直接回车

C:WINDOWSsystem32>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.23 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.

登陆成功之后修改密码(密码修改为admin):

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'admin';

 或者

update mysql.user set authentication_string=password('admin') where user='root' and Host = 'localhost';

默认不允许远程访问MySQL,如果需要远程访问,下面是开启远程访问的命令:

use mysql;

update user set host = '%' where user = 'root';

FLUSH PRIVILEGES;

忘记 root 密码重置方法:

以管理员身份运行 CMD,停止服务

net stop mysql

无密码启动 MySQL:

mysqld --console --skip-grant-tables --shared-memory

新开一个 cmd 连接 MySQL:

mysql -uroot

修改密码:

update mysql.user set authentication_string=password('admin') where user='root' and Host = 'localhost';

切换到第一个管理员身份运行的 CMD,Ctrl + C 结束服务,然后以正常方式开启 MySQL:

net start mysql
原文地址:https://www.cnblogs.com/1x11/p/9508222.html