一、下载解压
压缩包下载:MySQL :: Download MySQL Community Server
MSI版本不建议,容易出现各种问题,解决也很麻烦。
二、配置系统变量
MySQL_HOME:D:\DevSvr\dbSvr\mysql
Path变量中追加值:%MySQL_HOME%\bin
三、配置mysql
安装的应用目录下创建Data文件夹(用于存放mysql数据)和my.ini文件(创建文本文件将txt后缀改为ini即可)。 初始化Mysql后,Mysql8.0之后自动生成data文件夹。
在my.ini中编辑知名路径,设置配置参数:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\DevSvr\dbSvr\mysql
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
# datadir=D:\DevSvr\dbSvr\mysql\data
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
四、以管理员身份执行CMD下的数据库服务操作、数据库操作
简洁起见,安装为服务前, 初始化为 (root)无密码 方式。
否则,将在bin下生产data下有一个err后缀的文件,里面保存着临时密码,可以用记事本打开查看
命令提示符窗口下:
D:\DevSvr\dbSvr\mysql>cd bin
D:\DevSvr\dbSvr\mysql\bin>mysqld --initialize
D:\DevSvr\dbSvr\mysql\bin>mysqld --install
Service successfully installed.
D:\DevSvr\dbSvr\mysql\bin>mysql -u root -p
Enter password: ************
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
D:\DevSvr\dbSvr\mysql\bin>mysqld -remove
Service successfully removed.
D:\DevSvr\dbSvr\mysql\bin>mysqld --initialize-insecure
D:\DevSvr\dbSvr\mysql\bin>mysqld --install
Service successfully installed.
D:\DevSvr\dbSvr\mysql\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。
D:\DevSvr\dbSvr\mysql\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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 user 'root'@'localhost' identified with mysql_native_passwordjb123456';
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
D:\DevSvr\dbSvr\mysql\bin>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
其中,
mysqld --initialize --console命令
mysqld --initialize-insecure
2022-12-06T15:03:33.650703Z 0 [System] [MY-013169] [Server] D:\DevSvr\dbSvr\mysql\bin\mysqld.exe (mysqld 8.0.31) initializing of server in progress as process 4744
2022-12-06T15:03:33.651813Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2022-12-06T15:03:33.684659Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-12-06T15:03:34.580056Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-12-06T15:03:35.856266Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
windows下mysql服务操作:
//安装mysql服务
mysqld install mysql
//卸载mysql服务
sc delete mysql(需要管理员权限)
//移除mysql服务(需要停止mysql)
mysqld -remove
跳过授权表进行免密登录
mysqld --console --skip-grant-tables --shared-memory
添加外网访问权限
//切换数据库
use mysql
//更新用户的host
update user set host='%' where user='root';
//授权
grant all privileges on *.* to 'root'@'%' with grant option;
//刷新
flush privileges;
创建用户waggag并授远程访问权
创建用户
create user 'waggag'@'%' identified by '225514';
授予权限
GRANT ALL ON *.* TO 'waggag'@'%' WITH GRANT OPTION;
刷新权限
//刷新权限,一般修改密码或授权用户的时候需要使用
flush privileges;
查看与修改加密方式
mysql8.0 引入了新特性 caching_sha2_password;这种密码加密方式
查看加密方式:
select host,user,plugin from user;
修改root用户的加密方式:
alter user 'root'@localhost identified with mysql_native_password BY '225514';
更建议升级客户端软件。
场景:mysql版本是8,安装完成修改root密码,或是长时间没用忘记root密码,服务名为:mysql。
1、关闭mysql服务,net stop mysql。
2、跳过root密码验证进行登录。管理员模式打开cmd命令窗口,输入:
mysqld --console --skip-grant-tables --shared-memory
解决:
不关闭以上cmd窗口,再以管理员方式运行新的cmd,运行以下命令
mysql -uroot -p
提示输入密码时直接按回车进入,输入
use mysql;
alter user 'root'@'localhost' identified by '123456';
flush privileges;
123456就是要设置的密码,退出MySQL交互环境,再次启动MySQL服务,用设置的密码连接MySQL
详细实现过程:
置空root用户的校验字符串(可以理解为root用户的登陆密码),命令:
update user set authentication_string=‘’ where user=‘root’;
查看root用户的校验字符串是否被置空,root用户的authentication_string字段为空,则说明root校验字符串已被置空,查看命令:
select user, authentication_string from mysql.user;
保存刷新此置空操作,命令:
flush privileges;
关闭刚才的两个cmd命令窗口,管理员模式打开新的cmd命令窗口,启动mysql服务,命令:
net start mysql
root用户登陆mysql,出现的密码输入提示不用管,直接回车,命令:
mysql -uroot -p
进入操作mysql数据库的模式,命令:
use mysql;
设置新密码,命令:
alter user ‘root’@‘localhost’ identified by ‘新密码’;
查看root用户的校验字符串插入是否操作成功,命令:
select user, authentication_string from mysql.user;
刷新保存当前操作,命令:
flush privileges;
退出mysql操作空间,命令:
exit
重启mysql服务,到此为止,root用户密码就重置成功了。可以用工具连接,或新开个cmd命令窗口测试下。