1、下载mysql8.0.20安装包
下载地址: https://dev.mysql.com/downloads/mysql/.
找到安装包后下载。(官网为英文,如果看不懂的小伙伴可以将网站复制到谷歌进行翻译)
点击跳过登录,直接下载到本地。
安装mysql
1、下载下来之后是一个zip的压缩包文件
将其解压。
2、解压之后,接下来设置环境变量
右击我的电脑===>点击属性===>点击高级系统设置===>环境变量===>系统变量path===>编辑===>
新建(放上安装mysq的路径)
点击高级系统设置
点击环境变量
在系统变量中找到path点击编辑
点击新建,把安装mysql的bin路径放上去
我的是D:\JAVA\mysql8.0(Windows)\mysql-8.0.20-winx64\mysql-8.0.20-winx64\bin,所以环境变量中写的就是这个路径。配置好环境变量后点击确定。
3、配置mysql
因为在最新版本中的zip中是没有my.ini文件,需要自己新建一个my.ini文件。(my.ini是一些配置,比如端口、连接数等等)
记得检查位置,别弄错了喔!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
4、初始化、启动mysql
1)这里需要注意有的教程可能会让你在mysql-8.0.20-winx64根目录下如果没有data文件夹就新建一个data文件夹,这是有问题,等会用命令行安装Mysql的时候你会找不到初始的登入密码,其实在你输入mysqld --initialize 命令初始化mysql的时候会自动新建一个data文件夹。
如果没有记得手动建一个。
2)
在bin目录下输入cmd,以管理员省份打开命令。
输入cmd后直接回车
还可以直接以管理员身份打开cmd命令,然后用cd 命令切换到bin目录下
然后运行
mysqld --initialize --console
命令。执行过后找到A temporary password is generated for root@localhost: 这句,localhost后面就是自己的初始化密码。(看不到密码请看文末解决方案)
启动服务输入
net start mysql
输入
mysql -u root -p
进行登录数据库,这时提示需要密码,然后就是用你上面的密码登录
修改密码语句:
ALTER USER root@localhost IDENTIFIED BY ‘123456’;
修改密码为:123456
若要删除mysql,可执行命令
mysqld --remove mysql
值得注意的是,当我们打出命令mysqld --initialize --console查看信息的时候,并没有看到默认账号root的密码,那么怎么看到密码呢?请继续看下面
问题:在安装mysql时,打开DESKTOP-8UNK26K.err未发现密码,发现:
[Warning] root@localhost is created with an empty password ! Please
consider switching off the --initialize-insecure option.
而非:
[Warning] A temporary password is generated for root@localhost:
**********
原因:
mysql官网已经给出解决方案及原因:
The server creates a 'root'@'localhost' superuser account and other reserved accounts (see Reserved Accounts). Some reserved accounts are locked and cannot be used by clients, but 'root'@'localhost' is intended for administrative use and you should assign it a password.
Server actions with respect to a password for the 'root'@'localhost' account depend on how you invoke it:
With --initialize but not --initialize-insecure, the server generates a random password, marks it as expired, and writes a message displaying the password:
[Warning] A temporary password is generated for root@localhost:
iTag*AfrH5ej
With --initialize-insecure, (either with or without --initialize because --initialize-insecure implies --initialize), the server does not generate a password or mark it expired, and writes a warning message:
[Warning] root@localhost is created with an empty password ! Please
consider switching off the --initialize-insecure option.
For instructions on assigning a new 'root'@'localhost' password, see Post-Initialization root Password Assignment.
大概意思就是,如果之前初始化数据库时使用的是–initialize-insecure 命令,那么就不会出现密码。如果以–initialize初始化数据库,就会出现密码。initialize意为“不安全的,不可靠的”,这也就解释了为什么加上他就没有出现密码,及root的密码为空。
解决:
1、删除自己创建的data文件夹
2、以管理员身份cmd进入bin目录,关闭mysqld服务
D:\Program ~\MySQL\bin>mysqld -remove MySQL
Service successfully removed.
3、在bin目录执行mysqld --initialize,程序会自动创建刚才删掉的那个文件夹
4、在bin目录执行mysqld --install,重新安装mysql服务
5、在bin目录运行net start mysal,启动mysql服务
6、进入程序自动创建的那个文件夹,打开DESKTOP-8UNK26K.err,即可发现密码,问题解决。
————————————————
原文链接:https://blog.csdn.net/qq_30039127/article/details/104457068