加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

修改Linux服务器中的MySQL数据库密码

发布时间:2023-09-16 15:08:46 所属栏目:MySql教程 来源:
导读:修改Linux服务器中的MySQL数据库密码

1.知道mysql的密码情况下

通过登录mysql系统,

mysql -uroot -p

Enter password: 【输入原来的密码】

mysql>use mysql;

查看下自己mysql的版本,注意不
修改Linux服务器中的MySQL数据库密码
 
1.知道mysql的密码情况下
 
通过登录mysql系统,
 
mysql -uroot -p
 
Enter password: 【输入原来的密码】
 
mysql>use mysql;
 
查看下自己mysql的版本,注意不同的版本修改密码的方式会有所不同
 
1
 
mysql>select version();
 
根据自己的版本从下面的方式中,选择一种进行修改密码即可
 
5.7以前
 
1
 
mysql>update user set password=password(“123456”) where user=‘root';
 
5.7版本 user表没有了password字段,要用authentication_string代替
 
1
 
mysql>update user set authentication_string=password(“123456”) where user=‘root';
 
8.0以上版本 注意密码要有数字,大小写字母,和特殊符号,要不密码会验证不通过
 
1
 
mysql>ALTER USER ‘root' IDENTIFIED BY ‘123456@Test';
 
刷新权限(必须步骤)
 
1
 
2
 
mysql> flush privileges;
 
mysql> exit;
 
修改完后要记得重启下mysql服务,让修改生效
 
1
 
service mysqld restart
 
2.不知道mysql的密码情况下
 
不知道mysql密码的情况下,在安全模式下启动mysql,不用输入密码就可以登录进去,&表示在后台运行,不再后台运行的话,就要再打开一个终端。
 
1
 
mysqld_safe --skip-grant-tables &
 
或者你可以
 
输入:vi /etc/my.cnf 回车。在这个文件中的最后一行输入:skip-grant-tables
 
然后重启mysql服务 service mysqld restart 进入到安全模式,修改完密码后记得再删除掉这行数据重启下
 
1
 
mysql -uroot -p
 
然后提示你输入原密码,安全模式下直接回车就可以进入mysql数据库了;
 
1
 
mysql>use mysql;
 
后面的步骤跟上面一样了,根据自己的版本选择修改方式
 
5.7以前
 
1
 
mysql>update user set password=password(“123456”) where user=‘root';
 
5.7版本 user表没有了password字段,要用authentication_string代替
 
1
 
mysql>update user set authentication_string=password(“123456”) where user=‘root';
 
8.0以上版本 注意密码要有数字,大小写字母,和特殊符号,要不密码会验证不通过
 
1
 
mysql>ALTER USER ‘root' IDENTIFIED BY ‘123456@Test';
 
刷新权限(必须步骤)
 
mysql> flush privileges;
 
mysql> exit;
 
修改完后要记得重启下mysql服务,让修改生效
 
1
 
service mysqld restart
 
mysql5.7的密码修改错误问题:ERROR 1054 (42S22): Unknown column 'password' in 'field list'的解决
 
本意向修改一个用户的密码,网上搜到的命令为如下
 
1
 
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 8
 
Server 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 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> use mysql;  # 使用mysql
 
Database changed
 
mysql> 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 9
 
Server 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 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>
 
跟我这样设置,就可以了,大家自己设置自己的密码就行
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章