长时间使用非关系型数据库MongoDB近期遇到突然想使用mysql却发现mysql密码忘记了。。。就我遇到的情况记录一下解决方案
1、常见的一种解决方案是修改配置文件my.ini文件skip-grant-tables 然后重启忽略登录验证的一种方式
2、但我发现现在下载的mysql都是这种目录的
安装之后没有my.ini文件
所以记录一个安装的mysql服务又没有my.ini配置文件忘记密码了该怎么办
步骤如下:
先删除mysql服务
以管理员身份打开cmd进去mysql bin(如果没有设置环境变量)执行
sc delete Mysql(名称是自己的服务器名称)
然后自己新建一个my.ini文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = C:\Program Files\MySQL\MySQL Server 5.7
datadir = C:\Program Files\MySQL\MySQL Server 5.7\Data
port = 3306
# server_id = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 16M
read_rnd_buffer_size = 16M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
注意修改basedir以及datadir
最后重新初始化mysql服务同时指定新建的my.ini作为服务默认的配置文件
mysqld --initialize-insecure --user=mysql
等待该命令结束之后安装mysql服务绑定my.ini配置文件
mysqld --install "MySql" --defaults-file="my.ini url"
重新安装好服务之后启动net start MySql首次进入不需要密码
mysql -u root -p
回车进入mysql并设置密码即可(根据版本选择命令)
update mysql.user set authentication_string=password("你的密码") where user="root";
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
完成之后推出完事
exit