如何更改/重置MySQL或者MariaDB root密码
最近,我更改了我的MySQL root用户密码。
然后我想如果忘记MySQL的root密码会怎样?
是否有一种简单的方法来重置MySQL或者MariaDB根密码?
我浏览了一些在线教程,但它们似乎都不完整,无法区分更改密码和重置密码。
他们似乎缺少有关MySQL表的详细信息,该表存储了用户密码以及存储在哪些列中。
在本教程中,我们将学习以下主题。
- 如何更改MySQL/MariaDB根密码
- 如何重置MySQL/MariaDB根密码
我将尝试使其尽可能完整,阅读本文后,希望您可以轻松完成此任务,而无需任何其他帮助。
更改和重置密码有什么区别?
如果知道root密码,则可以以root用户身份连接到数据库,然后非常容易地更改密码。
您可以更改root密码以及任何其他用户密码。
如果您忘记了root密码,则意味着您无法以root用户身份连接到MySQL服务器。
root用户具有最高的特权,您无法通过其他帐户更改其密码。
在这种情况下,我们必须执行一些其他步骤来重置MySQL根密码。
MySQL和MariaDB的步骤是否相同?
MariaDB建立在MySQL之上。
它非常适合虚拟主机托管要求。
实际上,theitroad和我所有的都使用MariaDB数据库。
任何适用于MySQL的命令也适用于MariaDB。
您可能需要对命令进行的唯一调整是停止和启动MySQL服务器。
我在本教程中使用Ubuntu,并且使用systemctl
启动/停止服务。
您也可以使用/etc/init.d/mysql执行相同的操作。
如果您使用的是Windows操作系统,请从命令提示符处使用mysqld或者mysqladmin来启动或者停止MySQL服务器。
它们位于MySQL安装bin文件夹中。
如何更改MySQL或者MariaDB根密码
我正在使用MariaDB数据库,我们可以使用–version选项来查找其版本。
# mariadb --version mariadb Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2 #
1.以root用户身份连接到MySQL
# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
2.更改mysql.user表中的password和authentication_string值
MySQL用户密码以加密形式存储在mysql.user表中的password和authentication_string列中。
我们可以使用PASSWORD()函数将纯文本字符串转换为加密值并设置这些列。
MariaDB [(none)]> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> select password, authentication_string from user where User = 'root' AND Host = 'localhost'; +-------------------------------------------+-------------------------------------------+ | password | authentication_string | +-------------------------------------------+-------------------------------------------+ | *E510A8BC6807F8BF4913D893620792C432FCBA5B | *E510A8BC6807F8BF4913D893620792C432FCBA5B | +-------------------------------------------+-------------------------------------------+ 1 row in set (0.00 sec) MariaDB [mysql]> UPDATE user SET authentication_string = PASSWORD('qwerty2021') WHERE User = 'root' AND Host = 'localhost'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> UPDATE user SET password = PASSWORD('qwerty2021') WHERE User = 'root' AND Host = 'localhost'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> select password, authentication_string from user where User = 'root' AND Host = 'localhost'; +-------------------------------------------+-------------------------------------------+ | password | authentication_string | +-------------------------------------------+-------------------------------------------+ | *6F168491676C70E51CB8D0F14D6B581D1322A77A | *6F168491676C70E51CB8D0F14D6B581D1322A77A | +-------------------------------------------+-------------------------------------------+ 1 row in set (0.00 sec) MariaDB [mysql]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> exit Bye root@localhost:~#
让我们了解以上查询中发生的情况。
首先,我们将数据库更改为" mysql"
然后,我们使用新密码为" root" @" localhost"用户设置" authentication_string"和" password"列值。
然后,我们使用FLUSH PRIVILEGES命令重新加载授权表。
然后退出MySQL会话。
根密码已成功更改。
注意:我尝试使用ALTER USER
命令来更改root密码,但是没有用。
MariaDB [mysql]> ALTER USER root@localhost IDENTIFIED BY 'qwerty2022'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USER root@localhost IDENTIFIED BY 'qwerty2022'' at line 1 MariaDB [mysql]>
3.使用新密码验证root用户登录
# mysql -uroot -pqwerty2021 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
我们已经成功更改了MySQL/MariaDB用户密码。
如何重置MySQL/MariaDB根密码
如果您忘记了root密码,那么我们需要执行一个另外的步骤,以便我们无需提供密码即可登录MySQL终端。
1.停止MySQL服务器
# systemctl stop mysql
您也可以运行systemctl stop mariadb
,效果是一样的。
2.在没有权限检查的情况下启动MySQL Server
这个想法是在不加载授权表信息的情况下启动MySQL服务器,以便我们可以以root用户身份登录而无需提供密码。
像这样运行MySQL服务器会带来安全隐患,因此必须简短地进行操作,并在重置root密码后立即将其关闭。
我们可以在安全模式下启动MySQL服务器,并通过–skip-grant-tables选项跳过加载存储用户权限设置的授权表。
# sudo mysqld_safe --skip-grant-tables --skip-networking & [1] 11734 root@localhost:~# 200427 20:05:40 mysqld_safe Logging to syslog. 200427 20:05:40 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql #
重要的是,运行以&结尾的命令,以使其在后台运行。
我还传递了–skip-networking选项以跳过网络连接,以防止其他客户端连接到MySQL服务器。
3.以root用户身份连接到MySQL Server,而无需传递密码
# mysql -u root Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
注意,我们没有提供root密码,但是我们仍然能够连接到MySQL服务器。
4.重置mysql.user表中的root密码
MariaDB [(none)]> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> UPDATE user SET authentication_string = PASSWORD('qwerty2022') WHERE User = 'root' AND Host = 'localhost'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> UPDATE user SET password = PASSWORD('qwerty2022') WHERE User = 'root' AND Host = 'localhost'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [mysql]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> exit Bye #
Mysql重置根密码
5.停止并启动MySQL服务器
首先,我们将终止正在运行的MySQL服务器。
PID位于/var/run/mysqld/mysqld.pid
文件中。
# cat /var/run/mysqld/mysqld.pid 11891 # sudo kill 11891 #
现在,以正常模式启动MySQL服务器。
# systemctl start mysql
6.通过使用新密码以root用户身份登录进行验证
# mysql -uroot -pqwerty2022 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04 Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
如果您尝试以没有密码的root用户身份登录,则会引发"拒绝访问"错误。
# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) # mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) # mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) #