ini.py文件:
[client] default-character-set=utf8 [mysqld] port = 3306 basedir=D:\mysql-8.0.22-winx64 # datadir=D:\mysql-8.0.22-winx64\data max_connections=20 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8MB4 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB
注:文件里面不要放“skip-grant-tables”这句话,这是跳过密码登入的语句,如果加了会使电脑打不开MySQL服务器(一启动就关闭,亲身经历过!!!)
d: # 根目录切换不需要用 cd cd D:\mysql-8.0.22-winx64 # 切换到MySQL下载的根目录
注:我这里配置环境变量(即把 bin 目录的绝对路径加到系统变量的 path 变量下)没有用,只能切换目录。你们可以试一下!
mysqld --initialize --console # 初始化数据库,--console 可以查看日志文件,如果出错也可以翻译下,然后按它的要求解决,最好加上--console好排查问题。
这是初始化成功的标志:
2021-03-26T05:39:51.494723Z 0 [System] [MY-013169] [Server] D:\mysql-8.0.22-winx64\bin\mysqld.exe (mysqld 8.0.22) initializing of server in progress as process 14960 2021-03-26T05:39:51.512804Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-03-26T05:39:52.562462Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-03-26T05:39:55.092666Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: +PjKg9gljJ?y
这时产生一个随机十二位数密码(复制密码,下面要用。):
A temporary password is generated for root@localhost: APWCY5ws&hjQ
mysqld install # 安装MySQL
启动服务器:
net start mysql
登入:
mysql -u root -P3306 -p
注:
然后提示输入密码:Enter password:
输入之前的随机密码,回车,如果不出意外就进入到mysqld的交互模式(mysql>)了,这个时候就能输入MySQL语句以分号结束进行数据库操作了。
我想跳过密码进去,但是没用:
D:\mysql-8.0.22-winx64\bin>mysql --defaults-file='D:\mysql-8.0.22-winx64\my.ini' --console --skip-grant-tales mysql: [ERROR] Failed to open required defaults file: 'D:\mysql-8.0.22-winx64\my.ini' mysql: [ERROR] Fatal error in defaults handling. Program aborted!
说产生了一个致命错误。。。MySQL密码是加密的,储存密码的文件你没法访问。
经过大量阅读相关文章及几天时间的沉淀之后,我又尝试了一下:
mysqld -remove # 删除服务
注:这句话表示删除所有配置,打回安装前的状态,不过同时还要删除根目录下的data目录里面所有的文件。
然后按照上面的步骤在来一次,终于,成功了:
D:\mysql-8.0.22-winx64\bin>mysql -u root -P3306 -p Enter password: ************ Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.22 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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>
看到mysql交互窗口,我舒心地笑了。。。
这个时候还不能得意,因为还没有设置新密码,你操作不了数据库:
mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> select version(); ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
而且,最重要的是,如果不设置的话,下次登入就会因为密码不对报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
所以,还有最后一步:修改密码!!!
我尝试了很多修改密码的语句:
UPDATE mysql.user SET authentication_string=PASSWORD('j2021') WHERE USER='root'; set password=PASSWORD('root'); update user set authentication_string='root';
全都报错了:
your MySQL server version for the right syntax to use near '('root') where user='root'' at line 1
最后,在网上搜索:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.找到了答案。
修改密码正确姿势:
alter user 'root'@'localhost' identified by 'root';
注:by后面填的就是密码。
成功:
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.22 | +-----------+