背景
在windows上安装mysql,通过压缩包的方式安装,也就是解压之后,通过配置安装。在安装过程中,会自动产生密码,后期使用时候要修改密码。这种安装方式下,网上搜到的很靠前的解决办法并不能解决问题。
下面是网上一些办法
整理了以下四种在MySQL中修改root密码的方法,可能对大家有所帮助!
方法1: 用SET PASSWORD命令
mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
方法2:用mysqladmin
mysqladmin -u root password "newpass"
如果root已经设置过密码,采用如下方法
mysqladmin -u root password oldpass "newpass"
方法3: 用UPDATE直接编辑user表
mysql -u root
mysql> use mysql;
mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
在丢失root密码的时候,可以这样
mysqld_safe --skip-grant-tables&
mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;
亲手尝试
可以看出mysql安装成功,系统生成了密码。
接下来修改密码
可以看出,上面的操作都失败了。
成功操作
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
by 后面就是要设置的新密码
操作参考
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
D:\>cd "Program Files"
D:\Program Files>cd mysql-8.0.15-winx64
D:\Program Files\mysql-8.0.15-winx64>cd bin
D:\Program Files\mysql-8.0.15-winx64\bin>ls
'ls' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\Program Files\mysql-8.0.15-winx64\bin>dir
驱动器 D 中的卷是 DataDisk
卷的序列号是 B628-73B4
D:\Program Files\mysql-8.0.15-winx64\bin 的目录
2019/01/26 09:29 <DIR> .
2019/01/26 09:29 <DIR> ..
2019/01/26 01:28 19,264 echo.exe
2019/01/26 01:28 617,792 harness-library.dll
2019/01/26 01:28 5,098,304 ibd2sdi.exe
2019/01/26 01:28 5,065,536 innochecksum.exe
2019/01/26 01:28 2,110,272 libeay32.dll
2019/01/26 01:28 1,846,592 libmecab.dll
2019/01/26 01:28 4,958,016 lz4_decompress.exe
2019/01/26 01:28 5,353,280 myisamchk.exe
2019/01/26 01:28 5,184,320 myisamlog.exe
2019/01/26 01:28 5,250,880 myisampack.exe
2019/01/26 01:28 5,228,352 myisam_ftdump.exe
2019/01/26 01:28 5,297,472 mysql.exe
2019/01/26 01:28 5,197,120 mysqladmin.exe
2019/01/26 01:28 5,502,272 mysqlbinlog.exe
2019/01/26 01:28 5,209,408 mysqlcheck.exe
2019/01/26 01:28 44,661,568 mysqld.exe
2019/01/26 09:03 390,524,928 mysqld.pdb
2019/01/26 01:28 5,267,264 mysqldump.exe
2019/01/26 08:20 7,902 mysqldumpslow.pl
2019/01/26 08:20 28,713 mysqld_multi.pl
2019/01/26 01:28 5,189,440 mysqlimport.exe
2019/01/26 01:28 5,671,744 mysqlpump.exe
2019/01/26 01:28 5,716,800 mysqlrouter.exe
2019/01/26 01:28 4,927,808 mysqlrouter_plugin_info.exe
2019/01/26 01:29 5,187,904 mysqlshow.exe
2019/01/26 01:29 5,205,312 mysqlslap.exe
2019/01/26 08:20 7,392 mysql_config.pl
2019/01/26 01:29 4,987,200 mysql_config_editor.exe
2019/01/26 01:29 5,188,928 mysql_secure_installation.exe
2019/01/26 01:29 5,016,896 mysql_ssl_rsa_setup.exe
2019/01/26 01:29 4,912,448 mysql_tzinfo_to_sql.exe
2019/01/26 01:29 11,038,528 mysql_upgrade.exe
2019/01/26 01:29 4,970,304 my_print_defaults.exe
2019/01/26 01:29 5,653,312 perror.exe
2019/01/26 01:29 364,352 ssleay32.dll
2019/01/26 01:29 4,932,416 zlib_decompress.exe
36 个文件 581,400,039 字节
2 个目录 186,679,865,344 可用字节
D:\Program Files\mysql-8.0.15-winx64\bin>mysqld --initialize --console
2019-03-19T01:46:39.318148Z 0 [System] [MY-013169] [Server] D:\Program Files\mys
ql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server in progre
ss as process 5004
2019-03-19T01:46:54.443341Z 5 [Note] [MY-010454] [Server] A temporary password i
s generated for root@localhost: !qzy=6kQLYsh
2019-03-19T01:46:58.474643Z 0 [System] [MY-013170] [Server] D:\Program Files\mys
ql-8.0.15-winx64\bin\mysqld.exe (mysqld 8.0.15) initializing of server has compl
eted
D:\Program Files\mysql-8.0.15-winx64\bin>mysqld install
Service successfully installed.
D:\Program Files\mysql-8.0.15-winx64\bin>net start mysql
MySQL 服务正在启动 ....
MySQL 服务已经启动成功。
D:\Program Files\mysql-8.0.15-winx64\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15
Copyright (c) 2000, 2019, 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> set password for root@localhost = password('root');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'passw
ord('root')' at line 1
mysql> set password for 'root'@'localhost' = password('root');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'passw
ord('root')' at line 1
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement befo
re executing this statement.
mysql> UPDATE user SET Password = PASSWORD('root') WHERE user = 'root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '('roo
t') WHERE user = 'root'' at line 1
mysql> UPDATE user SET Password = 'root' WHERE user = 'root';
ERROR 1046 (3D000): No database selected
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement befo
re executing this statement.
mysql> UPDATE mysql.user SET Password=PASSWORD('root') WHERE User='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '('roo
t') WHERE User='root'' at line 1
mysql> UPDATE mysql.user SET Password=PASSWORD('Huawei@123') WHERE User='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '('Hua
wei@123') WHERE User='root'' at line 1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.04 sec)
mysql>