{"id":186,"date":"2024-07-12T22:39:32","date_gmt":"2024-07-12T14:39:32","guid":{"rendered":"https:\/\/thereisno.top:4430\/?p=186"},"modified":"2024-07-18T10:31:39","modified_gmt":"2024-07-18T02:31:39","slug":"mysql%e9%87%8d%e7%bd%aeroot%e5%af%86%e7%a0%81","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=186","title":{"rendered":"mysql\u91cd\u7f6eroot\u5bc6\u7801"},"content":{"rendered":"\n<p>Step 1 \u2014 Identifying the Database Version<br>\nMost modern Linux distributions ship with either MySQL or MariaDB, a popular drop-in replacement which is fully compatible with MySQL. Depending on the database used and its version, you\u2019ll need to use different commands to recover the root password.<br>\nYou can check your version with the following command:<\/p>\n\n\n\n<p>mysql &#8211;version<br>\nCopy<br>\nYou\u2019ll see some output like this with MySQL:<\/p>\n\n\n\n<p>MySQL output<br>\nmysql  Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using  EditLine wrapper<br>\nOr output like this for MariaDB:<\/p>\n\n\n\n<p>MariaDB output<br>\nmysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1<br>\nMake note of which database and which version you\u2019re running, as you\u2019ll use them later. Next, you need to stop the database so you can access it manually.<br>\nStep 2 \u2014 Stopping the Database Server<br>\nTo change the root password, you have to shut down the database server beforehand.<br>\nYou can do that for MySQL with:<\/p>\n\n\n\n<p>sudosystemctl stop mysql<br>\nAnd for MariaDB wtih:<\/p>\n\n\n\n<p>sudosystemctl stop mariadb<\/p>\n\n\n\n<p>After the database server is stopped, you\u2019ll access it manually to reset the root password.<br>\nStep 3 \u2014 Restarting the Database Server Without Permission Checking<br>\nIf you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password. This will allow you to gain access to the database without knowing it.<br>\nTo do this, you need to stop the database from loading the grant tables, which store user privilege information. Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.<br>\nStart the database without loading the grant tables or enabling networking:<\/p>\n\n\n\n<p>sudomysqld_safe &#8211;skip-grant-tables &#8211;skip-networking &amp;<\/p>\n\n\n\n<p>The ampersand at the end of this command will make this process run in the background so you can continue to use your terminal.<br>\nNow you can connect to the database as the root user, which should not ask for a password.<\/p>\n\n\n\n<p>mysql -uroot<br>\nCopy<br>\nYou\u2019ll immediately see a database shell prompt instead.<br>\nMySQL prompt<br>\nType &#8216;help;&#8217; or &#8216;\\h&#8217; for help. Type &#8216;\\c&#8217; to clear the current input statement.<br>\nmysql&gt;<br>\nMariaDB prompt<br>\nType &#8216;help;&#8217; or &#8216;\\h&#8217; for help. Type &#8216;\\c&#8217; to clear the current input statement.<br>\nMariaDB [(none)]&gt;<br>\nNow that you have root access, you can change the root password.<br>\nStep 4 \u2014 Changing the Root Password<br>\nOne simple way to change the root password for modern versions of MySQL is using the ALTER USER command. However, this command won\u2019t work right now because the grant tables aren\u2019t loaded.<br>\nLet\u2019s tell the database server to reload the grant tables by issuing the FLUSH PRIVILEGES command.<\/p>\n\n\n\n<p><strong><em>FLUSH PRIVILEGES;\u975e\u5e38\u5173\u952e\uff0c\u5fc5\u987b\u5148\u6267\u884c\u8fd9\u4e2a\uff0c\u7136\u540e\u6267\u884c\u540e\u9762\u7684\u8bed\u53e5\u624d\u6709\u6548<\/em><\/strong><br> Now we can actually change the root password.<br> For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command.<\/p>\n\n\n\n<p>ALTER USER&#8217;root&#8217;@&#8217;localhost&#8217;IDENTIFIED BY &#8216;new_password&#8217;;<br>\nCopy<br>\nFor MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:<\/p>\n\n\n\n<p>SET PASSWORD FOR &#8216;root&#8217;@&#8217;localhost&#8217;=PASSWORD(&#8216;new_password&#8217;);<br>\nCopy<br>\nMake sure to replace new_password with your new password of choice.<br>\nNote: If the ALTER USER command doesn\u2019t work, it\u2019s usually indicative of a bigger problem. However, you can try UPDATE \u2026 SET to reset the root password instead.<\/p>\n\n\n\n<p>UPDATE mysql.user SET authentication_string =PASSWORD(&#8216;new_password&#8217;)WHERE User =&#8217;root&#8217;AND Host =&#8217;localhost&#8217;;<br>\nCopy<br>\nRemember to reload the grant tables after this.<br>\nIn either case, you should see confirmation that the command has been successfully executed.<\/p>\n\n\n\n<p>Output<br>\nQuery OK, 0 rows affected (0.00 sec)<br>\nThe password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.<br>\nStep 5 \u2014 Restart the Database Server Normally<br>\nFirst, stop the instance of the database server that you started manually in Step 3. This command searches for the PID, or process ID, of MySQL or MariaDB process and sends SIGTERM to tell it to exit smoothly after performing clean-up operations. You can learn more in this Linux process management tutorial.<br>\nFor MySQL, use:<\/p>\n\n\n\n<p>sudokill<code>cat\/var\/run\/mysqld\/mysqld.pid<\/code><br>\nCopy<br>\nFor MariaDB, use:<\/p>\n\n\n\n<p>sudokill<code>\/var\/run\/mariadb\/mariadb.pid<\/code><br>\nCopy<br>\nThen, restart the service using systemctl.<br>\nFor MySQL, use:<\/p>\n\n\n\n<p>sudosystemctl start mysql<br>\nCopy<br>\nFor MariaDB, use:<\/p>\n\n\n\n<p>sudosystemctl start mariadb<br>\nCopy<br>\nNow you can confirm that the new password has been applied correctly by running:<\/p>\n\n\n\n<p>mysql -uroot -p<br>\nCopy<br>\nThe command should now prompt for the newly assigned password. Enter it, and you should gain access to the database prompt as expected.<br>\nConclusion<br>\nYou now have administrative access to the MySQL or MariaDB server restored. Make sure the new root password you choose is strong and secure and keep it in safe place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step 1 \u2014 Identifying the Database Version Most modern L &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=186\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cmysql\u91cd\u7f6eroot\u5bc6\u7801\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,37],"tags":[48],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-linux","category-mysql","tag-mysqlroot"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}