下载安装
按照系统选择32/64位,点击下载之后,它会让你登录,不用管点下面的直接下载就好了。
把下载好的压缩包解压到你的某一目录下,然后创建my.ini配置文件,用记事本打开,把下面这些代码粘贴复制进去:
1 | [mysql] |
网上大部分教程中上面的#datadir=D:\mysql-5.7.20-winx64\data这句代码是没有注释掉的,我照着来之后的安装中启动不了服务。
安装服务
进入 bin 目录下,用管理员身份运行命令提示符(注意以管理员身份运行,且一定要进在bin目录)
执行安装命令
1 | 安装:mysqld --install [服务名] |
[服务名]为可选参数,默认不填写则为 MySQL。
初始化data目录
不需要手动创建data文件夹
要初始化数据目录,请使用–initialize或–initialize-insecure选项调用mysqld,具体取决于是否希望服务器为“root”@“localhost”帐户生成随机初始密码。
1 | mysqld --initialize-insecure |
自动生成无密码的root用户
1 | mysqld --initialize |
自动生成带随机密码的root用户,这种方法要进去日志中找到生成的随机密码,日志在data目录中:
成功后使用命令启动服务:
1 | 停止服务:net stop [服务名] |
登录MySQL,修改密码
输入命令,回车,然后输入密码
1 | mysqld --default-file="D:\mysql-5.7.20-winx64\my.ini" --skip-grant-tables |
接下来你想进行一些操作,发现它会提示你,需要先重置密码。
使用命令重置密码:
1 | SET PASSWORD = PASSWORD('新的密码'); |
另外,MySQL官方文档显示在5.7.11版本之前,密码默认一年过期:
Note
Prior to 5.7.11, the default
default_password_lifetime
value is 360 (passwords must be changed approximately once per year). For such versions, be aware that, if you make no changes to thedefault_password_lifetime
variable or to individual user accounts, each user password expires after 360 days and the account starts running in restricted mode. Clients that connect to the server using the account then get an error indicating that the password must be changed:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
However, this is easy to miss for clients that automatically connect to the server, such as connections made from scripts. To avoid having such clients suddenly stop working due to a password expiring, make sure to change the password expiration settings for those clients, like this:
1
2 > ALTER USER 'script'@'localhost' PASSWORD EXPIRE NEVER
>
>
Alternatively, set the
default_password_lifetime
variable to0
, thus disabling automatic password expiration for all users.
这里顺便把密码有效期设置为永久,可以使用命令设置:
1 | ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER |
也可以在配置文件把default_password_lifetime
设置为0,禁用所有用户的自动密码到期。
1 | [mysqld] |
退出重新登录,然后就可以操作了。
参考链接
https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html
https://dev.mysql.com/doc/refman/5.7/en/password-management.html