{"id":3729,"date":"2023-04-04T18:13:48","date_gmt":"2023-04-04T17:13:48","guid":{"rendered":"https:\/\/www.dpscomputing.com\/blog\/?p=3729"},"modified":"2023-04-04T18:19:19","modified_gmt":"2023-04-04T17:19:19","slug":"access-denied-for-user-rootlocalhost-mysql-error","status":"publish","type":"post","link":"https:\/\/www.dpscomputing.com\/blog\/2023\/04\/04\/access-denied-for-user-rootlocalhost-mysql-error\/","title":{"rendered":"Access denied for user &#8216;root&#8217;@&#8217;localhost&#8217; &#8211; MySQL Error"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"alignright size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3730\" data-permalink=\"https:\/\/www.dpscomputing.com\/blog\/2023\/04\/04\/access-denied-for-user-rootlocalhost-mysql-error\/image-9-11\/\" data-orig-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9.png\" data-orig-size=\"2500,1733\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-9\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-300x208.png\" data-large-file=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-1024x710.png\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-1024x710.png\" alt=\"\" class=\"wp-image-3730\" width=\"256\" height=\"178\" srcset=\"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-1024x710.png 1024w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-300x208.png 300w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-150x104.png 150w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-768x532.png 768w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-1536x1065.png 1536w, https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9-2048x1420.png 2048w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/figure><\/div>\n\n\n<p>You&#8217;ve landed here in frustration whilst developing your latest database-driven creation and want an answer to the question &#8211; how to fix &#8220;Access denied for user &#8216;root&#8217;@&#8217;localhost'&#8221;.  Well, you&#8217;ve come to the right place.  <\/p>\n\n\n\n<p>As we&#8217;re sure you are aware, this error is triggered by your <a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL database<\/a> and unfortunately, as is the case for any developer, errors are a common way of life.  This error can be uber-frustrating due to the fact it&#8217;s likely brought down your ENTIRE app &#8211; after all, the error literally means that your application or website cannot connect successfully to your database.  <\/p>\n\n\n\n<p>Let&#8217;s fix it.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check your creds!<\/h2>\n\n\n\n<p>Firstly, before we move on &#8211; and we know it&#8217;s the standard level one tech support answer (but it&#8217;s this because it&#8217;s so often right!), check your credentials &#8211; i.e. your username and password.  Now, you&#8217;ve either forgotten your password (you should be using a <a href=\"https:\/\/www.lastpass.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">good password manager<\/a> by this point!) or you&#8217;ve never set one and aren&#8217;t sure.  <\/p>\n\n\n\n<p>If this is a production environment, check with your dev lead.  If that&#8217;s you, berate yourself for forgetting.  <\/p>\n\n\n\n<p>If it&#8217;s a development environment and you&#8217;re thinking &#8220;but I&#8217;ve never set a ***** password&#8221; then fortunately, we are likely to know it ;).  If you&#8217;re using AMPPS or similar software for local development then there&#8217;s every chance that your username and password are:<\/p>\n\n\n\n<p>Username &#8211; root<\/p>\n\n\n\n<p>Password &#8211; password<\/p>\n\n\n\n<p>Go check it out &#8211; and hopefully that&#8217;s the end of your nightmare with this error!<\/p>\n\n\n\n<p>For a bit of background, all MySQL databases come with a root password.  This can be used to access any database or table on your setup.  NOTE: this is NOT recommended for use in a production environment &#8211; set up a user specifically for the database\/table that your application needs to access.  <\/p>\n\n\n\n<p>If you are in a local development and you have changed the root users password &#8211; that&#8217;s the one that you&#8217;ll need to use.  If you can&#8217;t remember it, we&#8217;re going to tell you how to reset it further on so keep reading&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL Privileges<\/h2>\n\n\n\n<p>If it&#8217;s not the password (90% of the time it is!) then next stop is to check your MySQL privileges.  Just like a child, a MySQL privilege provides permission to each MySQL user (including root users) to do something.  Including logging in.  <\/p>\n\n\n\n<p>The error message Access denied for user &#8216;root&#8217;@&#8217;localhost&#8217; can appear if your MySQL user account is trying to do something (i.e. have sufficient privileges) to access the database.  This is a curious one with the root user &#8211; as by default it has privileges to do anything &#8211; but if you&#8217;ve been tinkering around in your setup it IS possible that you&#8217;ve modified the privileges to lock yourself out.  <\/p>\n\n\n\n<p>You can easily check the privileges of any user in the MySQL console.  Run the following command on your MySQL server to check the root user privileges.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW GRANTS FOR 'root'@'localhost';\r\n\r<\/code><\/pre>\n\n\n\n<p>See &#8216;grants&#8217; as like the keys to the city that your user possesses.  This will show you all privileges associated with the root user account.  Want to check out a different account?  Just replace root with the name of your user.  For example, if our user was called &#8216;test&#8217; we would use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW GRANTS FOR 'test'@'localhost';<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Granting Privileges<\/h2>\n\n\n\n<p>So, you&#8217;ve worked out you don&#8217;t have the privileges you thought you had?  Or you&#8217;re on a development environment and your really not sure?  Well, we can fix that using a super-special command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password';<\/code><\/pre>\n\n\n\n<p>In the above code choose a password and place it where it says &#8216;password&#8217;.  This should be a secure, auto-generated, long and complex password UNLESS you&#8217;re on a development environment &#8211; in which case it&#8217;s fine to use &#8216;password&#8217; if security is not a concern and you value your sanity which is slowly ebbing away whilst trying to fix this error!  <\/p>\n\n\n\n<p>The other side effect that this command will have is it will grant ALL privileges to the root account &#8211; which is really the way it should be and the way it was configured by default.  BEWARE: the root account should be locked down if this is a Production environment!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check your MySQL Config<\/h2>\n\n\n\n<p>OK, so if it&#8217;s not the password nor the privileges it&#8217;s time for ever developers nightmare &#8211; diving into the MySQL config.  <\/p>\n\n\n\n<p>Open up the MySQL config file in your favourite text editor.  Sometimes the pesky &#8220;Access denied for user &#8216;root&#8217;@&#8217;localhost'&#8221; can be caused because your MySQL config is totally messed up.  <\/p>\n\n\n\n<p>Did you make changes recently?  You took a backup of course so simply replace the current file with your backup when it last worked?  Out of luck?  Well, your options are to download a default MySQL config file or try to fix the swamp of a mess you MAY have made whilst trying to tinker with it.  <\/p>\n\n\n\n<p>Either way, we need to check that the MySQL configuration file has the correct settings.  <\/p>\n\n\n\n<p>&#8220;Where is the MySQL configuration file?&#8221; I hear you ask&#8230; In the blind panic since the error appeared you&#8217;ve totally forgotten where it is.  Fear not and look under the MySQL directions (\/etc\/my.cnf or \/etc\/mysql\/) and look for the file called my.cnf.  <\/p>\n\n\n\n<p>Once open, we&#8217;re going to get down to business and search for this line of config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\r\nskip-grant-tables<\/code><\/pre>\n\n\n\n<p>Not there?  Add it in.  <\/p>\n\n\n\n<p>WARNING!!!!!  This config option will SKIP the password check &#8211; so if it&#8217;s in production, it is not recommended.  At least lock down your website using protected folders or similar otherwise ANYONE will be able to access your MySQL database without a password.  <\/p>\n\n\n\n<p>The line above skips the password check which allows you access and therefore enables you to change the root users password to something more memorable.  <\/p>\n\n\n\n<p>Once you&#8217;ve done this &#8211; don&#8217;t forget to actually delete the lines again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nskip-grant-tables<\/code><\/pre>\n\n\n\n<p>Write yourself a reminder on a post it so that you don&#8217;t forget (please!)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reset the MySQL Root Password<\/h2>\n\n\n\n<p>If you&#8217;ve got to this point, you&#8217;re now desperate to fix the error.  So, as in all times of desperation, let&#8217;s just start hacking away at MySQL.  <\/p>\n\n\n\n<p>All has failed, all is hopeless UNLESS we reset the root users password.  to do this, you&#8217;re going to need to kill (stop) the MySQL server.  Then you can either make the config change above OR start the server with the operator &#8211;skip-grant-tables option which will have the same effect.  <\/p>\n\n\n\n<p>Now from the command line you&#8217;ll be able to login to MySQL without a password.  Again, please DON&#8217;T do this on a production environment!  Once you&#8217;ve &#8216;logged in&#8217;, run this command and you can update the root users password to something better:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql> USE mysql;\r\nmysql> UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';\r\nmysql> FLUSH PRIVILEGES;<\/code><\/pre>\n\n\n\n<p>Replace &#8216;new_password&#8217; in the PASSWORD() function with something awesome and hard to guess.  In development environments that are otherwise locked down you can of course use the favourite &#8216;password&#8217;.  The above code essentially manually updates the user table and updates password (with appropriate hashing\/salting) to the thing that you specify.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>If you&#8217;re Access denied for user &#8216;root&#8217;@&#8217;localhost&#8217; error hasn&#8217;t been solved by now, you&#8217;ve missed something or not done it correct.  Following the above steps is a fool-proof way to fix the access denied error no matter kind of craziness is going on in your setup.  <\/p>\n\n\n\n<p>If all else fails, you can always re-install your MySQL server and build it again from the ground up &#8211; but going back through and following the instructions carefully is likely to take a lot less time and cause a lot less pain!<\/p>\n\n\n\n<p>Let us know if the above steps have helped (or if you&#8217;ve spotted a typo) and feel free to ask us any questions below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve landed here in frustration whilst developing your latest database-driven creation and want an answer to the question &#8211; how to fix &#8220;Access denied for user &#8216;root&#8217;@&#8217;localhost&#8217;&#8221;. Well, you&#8217;ve come to the right place.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3730,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[55,2138,2139],"tags":[3733,3747,2925,3734,3735,3739,3740,3741,2453,3736,3737,3744,3745,3738,3743],"class_list":["post-3729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-development","category-development-issues","tag-access-denied-for-user-rootlocalhost","tag-cannot-login-to-mysql","tag-mysql","tag-mysql-access-denied","tag-mysql-access-denied-error","tag-mysql-access-denied-for-user","tag-mysql-access-denied-for-user-root","tag-mysql-access-denied-for-user-rootlocalhost","tag-mysql-error","tag-mysql-errors","tag-mysql-password","tag-mysql-root-password","tag-mysql-root-password-error","tag-mysql-root-user","tag-rootlocalhost-password"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.dpscomputing.com\/blog\/wp-content\/uploads\/2023\/04\/image-9.png","jetpack_shortlink":"https:\/\/wp.me\/p3nsfA-Y9","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3729"}],"collection":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/comments?post=3729"}],"version-history":[{"count":2,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3729\/revisions"}],"predecessor-version":[{"id":3732,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/posts\/3729\/revisions\/3732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media\/3730"}],"wp:attachment":[{"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/media?parent=3729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/categories?post=3729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dpscomputing.com\/blog\/wp-json\/wp\/v2\/tags?post=3729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}