MySQL5.7安装

下载安装

MySQL下载地址

按照系统选择32/64位,点击下载之后,它会让你登录,不用管点下面的直接下载就好了。

把下载好的压缩包解压到你的某一目录下,然后创建my.ini配置文件,用记事本打开,把下面这些代码粘贴复制进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\mysql-5.7.20-winx64
# 设置mysql数据库的数据的存放目录
#datadir=D:\mysql-5.7.20-winx64\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

网上大部分教程中上面的#datadir=D:\mysql-5.7.20-winx64\data这句代码是没有注释掉的,我照着来之后的安装中启动不了服务。

mark

安装服务

进入 bin 目录下,用管理员身份运行命令提示符(注意以管理员身份运行,且一定要进在bin目录)

执行安装命令

1
2
安装:mysqld --install [服务名]
移除:mysqld --remove [服务名]

[服务名]为可选参数,默认不填写则为 MySQL。

初始化data目录

不需要手动创建data文件夹

要初始化数据目录,请使用–initialize或–initialize-insecure选项调用mysqld,具体取决于是否希望服务器为“root”@“localhost”帐户生成随机初始密码。

1
mysqld --initialize-insecure

自动生成无密码的root用户

1
mysqld --initialize

自动生成带随机密码的root用户,这种方法要进去日志中找到生成的随机密码,日志在data目录中:

mark

mark

成功后使用命令启动服务:

1
2
停止服务:net stop [服务名]
启动服务:net start [服务名]

mark

登录MySQL,修改密码

输入命令,回车,然后输入密码

1
2
mysqld --default-file="D:\mysql-5.7.20-winx64\my.ini" --skip-grant-tables
mysql -u root -p

接下来你想进行一些操作,发现它会提示你,需要先重置密码。

mark

使用命令重置密码:

ll
1
SET PASSWORD = PASSWORD('新的密码');

mark

另外,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 to 0, thus disabling automatic password expiration for all users.

这里顺便把密码有效期设置为永久,可以使用命令设置:

1
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER

也可以在配置文件把default_password_lifetime设置为0,禁用所有用户的自动密码到期。

1
2
[mysqld]
default_password_lifetime=0

退出重新登录,然后就可以操作了。

mark

参考链接

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