青山不改,绿水长流。
Linux中MySQL密码修改
- 一、使用 mysqladmin 命令修改 root 账号密码
- 二、MySQL 中使用 update 修改密码
- 三、破解 MySQL 密码
- 总结
一、使用 mysqladmin 命令修改 root 账号密码
-
打开 Linux 终端或命令提示符窗口。
-
使用以下命令以root身份登录MySQL:
sudo mysqladmin -u root -p login
-
输入root用户的密码,然后按Enter键。
-
输入以下命令来修改root账号的密码(假设你要将密码改为"new_password"):
sudo mysqladmin -uroot -p'原密码' password '新密码'
#注意-p后面没有空格哦
-
如果密码修改成功,会显示
"mysqladmin: \[Warning\] Using a password on the command line interface can be insecure."
的警告信息。 -
确认密码已成功修改后,可以退出MySQL:
mysql> exit
二、MySQL 中使用 update 修改密码
在Linux中,你可以使用以下步骤来修改MySQL的密码:
- 使用root用户登录MySQL:
mysql -u root -p
-
输入root用户的密码,然后按Enter进入MySQL控制台。
-
选择要使用的MySQL数据库:
mysql> USE mysql;
- USER 表表结构
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
- 查看当前用户和其对应的加密密码:
mysql> SELECT user, authentication_string FROM user;
- 更新root用户的密码(假设你要将密码改为"new_password"):
mysql> UPDATE user SET authentication_string = PASSWORD('new_password') WHERE user = 'root';
- 刷新权限表以使更改生效:
mysql> FLUSH PRIVILEGES;
- 退出MySQL控制台:
mysql> exit;
- 重新启动MySQL服务器:
sudo service mysql restart
三、破解 MySQL 密码
- 跳过密码加载文件,修改/etc/在配置文件中添加了skip-grant-tables选项,使得MySQL在启动时跳过权限验证。
[root@mysql5_7 ~]# vim /etc/
skip-grant-tables
- 重启MySQL服务,以应用配置文件的更改。
[root@mysql5_7 ~]# systemctl restart mysqld
- 使用
mysql -uroot
命令以root身份登录MySQL
[root@mysql5_7 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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>
- 使用update语句更新了root用户在localhost主机上的认证密码为
'Admin@123'
。
mysql> update mysql.user set authentication_string=password('Admin@123') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 1
- 使用
flush privileges
命令刷新了权限。使用exit命令退出MySQL客户端。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
- 用
#
注释/etc/ 文件中skip-grant-tables
字段,并重启 MySQL 服务
[root@mysql5_7 ~]# vim /etc/
#skip-grant-tables
[root@mysql5_7 ~]# systemctl restart mysqld
- 使用
mysql -uroot -p'Admin@123'
命令重新以root身份登录MySQL,并成功进入MySQL的命令行。
[root@mysql5_7 ~]# mysql -uroot -p'Admin@123'
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 3
Server version: 5.7.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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>
到此密码修改成功
总结
这篇文章主要介绍了如何使用mysqladmin命令
和MySQL update语句来修改MySQL的root账号密码。使用update语句将root账号的密码修改为指定的密码。最后,使用flush privileges命令刷新权限表以使更改生效,然后退出MySQL控制台,重新启动MySQL服务器。这篇文章提供了详细的步骤和注意事项,适合初学者学习。