如何修改Linux服务器中的MySQL数据库密码
通过登录mysql系统,
mysql -uroot -pEnter password: 【输入原来的密码】mysql>use mysql;查看下自己mysql的版本,注意不同的版本修改密码的方式会有所不同
mysql>select version();根据自己的版本从下面的方式中,选择一种进行修改密码即可
5.7以前
mysql>update user set password=password(“123456”) where user=‘root';5.7版本 user表没有了password字段,要用authentication_string代替
mysql>update user set authentication_string=password(“123456”) where user=‘root';8.0以上版本 注意密码要有数字,大小写字母,和特殊符号,要不密码会验证不通过
mysql>ALTER USER ‘root' IDENTIFIED BY ‘123456@Test';刷新权限(必须步骤)
mysql> flush privileges;mysql> exit;修改完后要记得重启下mysql服务,让修改生效
service mysqld restart2.不知道mysql的密码情况下不知道mysql密码的情况下,在安全模式下启动mysql,不用输入密码就可以登录进去,&表示在后台运行,不再后台运行的话,就要再打开一个终端。
mysqld_safe --skip-grant-tables &或者你可以
输入:vi /etc/my.cnf 回车。在这个文件中的最后一行输入:skip-grant-tables
然后重启mysql服务 service mysqld restart 进入到安全模式,修改完密码后记得再删除掉这行数据重启下
mysql -uroot -p然后提示你输入原密码,安全模式下直接回车就可以进入mysql数据库了;
mysql>use mysql;后面的步骤跟上面一样了,根据自己的版本选择修改方式
5.7以前
mysql>update user set password=password(“123456”) where user=‘root';5.7版本 user表没有了password字段,要用authentication_string代替
mysql>update user set authentication_string=password(“123456”) where user=‘root';8.0以上版本 注意密码要有数字,大小写字母,和特殊符号,要不密码会验证不通过
mysql>ALTER USER ‘root' IDENTIFIED BY ‘123456@Test';刷新权限(必须步骤)
mysql> flush privileges;mysql> exit;修改完后要记得重启下mysql服务,让修改生效
service mysqld restartmysql5.7的密码修改错误问题:ERROR 1054 (42S22): Unknown column 'password' in 'field list'的解决本意向修改一个用户的密码,网上搜到的命令为如下
mysql> update user set password=password(“新密码”) where user=”用户名”;执行后报错:
ERROR 1054(42S22) Unknown column 'password' in ‘field list’
错误的原因是 5.7版本下的mysql数据库下已经没有password这个字段了,password字段改成了authentication_string
所以请使用一下命令:
E:\app\mysql\mysql-5.7.22-winx64\bin>mysql -uroot -pmysql123 # 先进入数据库mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; # 使用mysqlDatabase changedmysql> select User from user; # 此处为查询用户命令+---------------+| User |+---------------+| root || root || mysql.session || mysql.sys || root |+---------------+5 rows in set (0.01 sec) mysql> update mysql.user set authentication_string=password('mysql') where user='root'; # 更新密码Query OK, 3 rows affected, 1 warning (0.00 sec)Rows matched: 3 Changed: 3 Warnings: 1 mysql> flush privileges; # 数据刷新Query OK, 0 rows affected (0.00 sec) mysql> quit # 退出Bye E:\app\mysql\mysql-5.7.22-winx64\bin>mysql -uroot -pmysql # 再次进入mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 9Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
跟我这样设置,就可以了,大家自己设置自己的密码就行
总结以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。