mariadb重置自动id_如何更改/重置MySQL或MariaDB根密码

mariadb重置自动id

Recently, I changed my website MySQL root user password. Then I thought what will happen if I forget the MySQL root password?

最近,我更改了我的网站MySQL root用户密码。 然后我想如果忘记MySQL的root密码会怎样?

Is there an easy way to reset the MySQL or MariaDB root password?

是否有一种简单的方法来重置MySQL或MariaDB根密码?

I looked through some online tutorials, but none of them seem to be complete enough to differentiate between changing the password and resetting the password. They seemed lacking the details about the MySQL table where user passwords are stored and in which columns.

我浏览了一些在线教程,但是它们似乎都不完整,无法区分更改密码和重置密码。 他们似乎缺少有关MySQL表的详细信息,该表存储了用户密码以及存储在哪些列中。

In this tutorial, we will learn the following topics.

在本教程中,我们将学习以下主题。

  1. How to Change MySQL/MariaDB root Password

    如何更改MySQL / MariaDB根密码
  2. How to Reset MySQL/MariaDB root Password

    如何重置MySQL / MariaDB根密码

I will try to make it as complete as possible, and hopefully after reading this, you will get through this task easily without the need of any further help.

我将尝试使其尽可能完整,阅读本文后,希望您可以轻松完成此任务,而无需任何其他帮助。

更改和重置密码有什么区别? (What is the difference between changing and resetting the password?)

If you know the root password, you can connect to the database as root user and then change the password very easily. You can change the root password as well as any other user password.

如果您知道root密码,则可以以root用户身份连接到数据库,然后非常容易地更改密码。 您可以更改root密码以及任何其他用户密码。

If you have forgotten the root password, means you can’t connect to the MySQL server as root user. The root user has the highest privileges and you can’t change its password through other accounts. We have to perform some additional steps in this case to reset the MySQL root password.

如果您忘记了root密码,则意味着您无法以root用户身份连接到MySQL服务器。 root用户具有最高的特权,您不能通过其他帐户更改其密码。 在这种情况下,我们必须执行一些其他步骤来重置MySQL根密码。

MySQL和MariaDB的步骤是否相同? (Steps for MySQL and MariaDB are Same?)

MariaDB is built on top of MySQL. It’s very popular for web hosting requirements. In fact, JournalDev and all my websites are using MariaDB database. Any command that works for MySQL will work for MariaDB as well.

MariaDB建立在MySQL之上。 对于Web托管要求,它非常受欢迎。 实际上,JournalDev和我所有的网站都使用MariaDB数据库。 任何适用于MySQL的命令也适用于MariaDB。

The only adjustment in the commands you might have to make is to stop and start of MySQL server. I am using Ubuntu for this tutorial and I use systemctl to start/stop services. You can also use /etc/init.d/mysql to perform the same operations.

您可能必须对命令进行的唯一调整是停止和启动MySQL服务器。 我在本教程中使用Ubuntu,并且使用systemctl启动/停止服务。 您也可以使用/etc/init.d/mysql来执行相同的操作。

If you are on Windows OS, then please use mysqld or mysqladmin from the command prompt to start or stop the MySQL server. They are located in the MySQL installation bin folder.

如果您使用的是Windows操作系统,请从命令提示符处使用mysqldmysqladmin来启动或停止MySQL服务器。 它们位于MySQL安装bin文件夹中。

如何更改MySQL或MariaDB根密码 (How to Change MySQL or MariaDB root Password)

I am using MariaDB database, we can use –version option to find out its version.

我正在使用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 (1. Connect to MySQL as root user)


# 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, 2018, 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值 (2. Change the password and authentication_string value in mysql.user table)

MySQL user passwords are stored in mysql.user table password and authentication_string columns in the encrypted form. We can use the PASSWORD() function to convert a plain-text string to the encrypted value and set these columns.

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:~# 

Let’s understand what’s happening in the above queries.

让我们了解以上查询中发生了什么。

  • First of all, we are changing the database to ‘mysql’

    首先,我们将数据库更改为“ mysql”
  • Then, we are setting ‘authentication_string’ and ‘password’ column values for ‘root’@’localhost’ user with the new password.

    然后,我们使用新密码为'root'@'localhost'用户设置'authentication_string'和'password'列值。
  • Then we are reloading the grant tables using the FLUSH PRIVILEGES command.

    然后,我们使用FLUSH PRIVILEGES命令重新加载授权表。
  • Then quit the MySQL session. The root password has been changed successfully.

    然后退出MySQL会话。 根密码已成功更改。

NOTE: I tried to use ALTER USER command to change the root password, but it didn’t work.

注意 :我尝试使用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用户登录 (3. Verify the root user login using new password)


# 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, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

That’s it. We have successfully changed the MySQL/MariaDB user password.

而已。 我们已经成功更改了MySQL / MariaDB用户密码。

如何重置MySQL / MariaDB根密码 (How to Reset the MySQL/MariaDB root Password)

If you have forgotten the root password, then we need to perform one extra step so that we can login to the MySQL terminal without providing the password.

如果您忘记了root密码,那么我们需要执行一个额外的步骤,以便我们无需提供密码即可登录MySQL终端。

1.停止MySQL服务器 (1. Stop the MySQL Server)


# systemctl stop mysql

You can also run systemctl stop mariadb, the effect will be the same.

您也可以运行systemctl stop mariadb ,效果将相同。

2.在没有权限检查设置的情况下启动MySQL Server (2. Starting the MySQL Server without permission checking setting)

The idea is to start the MySQL server without loading the grant tables information, so that we can login as root user without providing password.

这个想法是在不加载授权表信息的情况下启动MySQL服务器,以便我们可以以root用户身份登录而无需提供密码。

It’s a security risk to run MySQL server like this, so it must be done briefly and shut down immediately after resetting the root password.

像这样运行MySQL服务器会带来安全隐患,因此必须简短地进行此操作,并在重置root密码后立即将其关闭。

We can start the MySQL server in safe mode and pass –skip-grant-tables option to skip loading grant tables that store the user privileges settings.

我们可以在安全模式下启动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
#

It is important to run the command ending with & so that it runs in the background. I am also passing –skip-networking option to skip networking that prevents other clients from connecting to the MySQL server.

重要的是,运行以&结尾的命令,以使其在后台运行。 我还传递了–skip-networking选项以跳过网络连接,该网络阻止其他客户端连接到MySQL服务器。

3.以root用户身份连接到MySQL服务器,而无需传递密码 (3. Connect to MySQL Server as root without passing password)


# 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, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

Notice that we are not providing root password, but still we are able to connect to MySQL server.

注意,我们没有提供root密码,但是我们仍然能够连接到MySQL服务器。

4.重置mysql.user表中的root密码 (4. Reset the root password in mysql.user table)


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 Reset Root Password
Mysql Reset Root Password
Mysql重置根密码

5.停止并启动MySQL服务器 (5. Stop and Start the MySQL server)

First of all, we will kill the running MySQL server. The PID is present in the /var/run/mysqld/mysqld.pid file.

首先,我们将终止正在运行MySQL服务器。 PID位于/var/run/mysqld/mysqld.pid文件中。


# cat /var/run/mysqld/mysqld.pid
11891
# sudo kill 11891
# 

Now, start the MySQL server in the normal mode.

现在,以正常模式启动MySQL服务器。


# systemctl start mysql

6.通过使用新密码以root用户身份登录进行验证 (6. Verify by logging as root user with new password)


# 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, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

If you will try to login as root without password, it will throw “Access denied” error.

如果您尝试以没有密码的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)
# 

结论 (Conclusion)

MySQL root user is just like any other user. However, changing or resetting its password is a bit tricky because it’s the super user and we can’t change root password from another user login.

MySQL根用户与其他任何用户一样。 但是,更改或重置其密码有点棘手,因为它是超级用户,我们无法从其他用户登录名更改root密码。

翻译自: https://www.journaldev.com/39274/change-reset-mysql-mariadb-root-password

mariadb重置自动id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值