2025年2月14日 星期五 甲辰(龙)年 腊月十四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 软件应用 > 数据库 > MySQL

mysql设置密码,修改密码,破解密码

时间:07-22来源:作者:点击数:29

一、数据库设置密码:

1.命令行模式:

1.1 命令行终端输入:

  • mysqladmin -uroot -p password 密码

回车,提示输入密码,但是mysql初始化时没有密码,所以直接又是回车

1.2 验证mysql密码是否设置成功:

  • mysql -uroot -p

回车,输入密码,进入mysql页面

2.进入数据库设置密码:

2.1 在mysql命令行输入以下sql命令(alter开头的命令):

  • mysql> alter user root@主机 identified by 密码;

2.2 exit退出mysql后,再使用mysql -hlocalhost -uroot -p密码,回车进入数据库

  • 安装mysql-server后:
  • [root@localhost ~]# systemctl enable mysqld --now
  • [root@localhost ~]# mysql
  • 回车后进入mysql命令行:
  • Welcome to the MySQL monitor. Commands end with ; or \g.
  • Your MySQL connection id is 8
  • Server version: 8.0.26 Source distribution
  • Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  • 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> alter user root@"localhost" identified by "123456";
  • Query OK, 0 rows affected (0.00 sec)
  • mysql> exit
  • 推出mysql后使用:
  • mysql -hlocalhost -uroot -p123456
  • 重新进入mysql,成功进入mysql就说明设置密码成功

二、数据库修改密码(需要知道旧密码):

1.命令行模式:

  • mysqladmin  -uroot -p password 新密码

回车,输入旧密码

验证mysql密码是否修改成功:

  • mysql -uroot -p

回车,输入新密码

2.进入数据库修改密码:

  • mysql> alter user root@"localhost" identified by "1234";

三、破解密码(不记得密码)

第1步:让数据库服务不验证数据库运行

  • 1)vim /etc/my.cnf.d/mysql-server.cnf
  • 加一行:
  • skip-grant-tables
  • 其他不变
  • 2)systemctl restart mysqld

第2步:修改mysqld配置文件,使用“mysql”命令无密进入mysql

此时不会验证密码,方便无密码进入mysql,清空authentication_string

  • [root@localhost ~]# mysqladmin -uroot -p password "1234"
  • Enter password:
  • mysqladmin: [Warning] Using a password on the command line interface can be insecure.
  • Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
  • [root@localhost ~]# mysql -uroot -p
  • Enter password:
  • #清空authentication_string
  • mysql> update mysql.user set authentication_string='' where host='localhost' and user='root';
  • #查看root的authentication_string的值是否清空了
  • mysql> select user,authentication_string from mysql.user;
  • #重新加载授权表,使新的授权规则立即生效
  • mysql> flush privileges;

exit推出,将mysqld的配置改回去并重启mysqld:

  • 1)vim /etc/my.cnf.d/mysql-server.cnf
  • 注释掉 skip-grant-tables
  • 其他不变
  • 2)systemctl restart mysqld

第3步:重置密码

再使用“mysql”命令就可以直接进入数据库;

exit退出数据库,再走一遍设置密码的流程就行了(请查看“一、数据库设置密码”)。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐