{"id":9839,"date":"2026-03-25T17:33:25","date_gmt":"2026-03-25T12:03:25","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=9839"},"modified":"2026-03-25T17:34:28","modified_gmt":"2026-03-25T12:04:28","slug":"how-to-disable-lock-a-mysql-user-account-from-cli","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-disable-lock-a-mysql-user-account-from-cli\/","title":{"rendered":"How to Disable\/Lock a MySQL User Account from CLI?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In database administration, there may be situations where you need to temporarily prevent a user from accessing a MySQL server without deleting the account. This can be useful during security incidents, suspicious activity, employee offboarding, or maintenance tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of removing the user completely, MySQL allows administrators to <strong>lock a user account<\/strong>, which immediately blocks login access while preserving the user\u2019s privileges and configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Root or administrative MySQL access<\/li>\n\n\n\n<li>MySQL 5.7.6+ or MySQL 8.x (ACCOUNT LOCK feature supported)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Log in to MySQL with root credentials.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$ mysql -u root -p<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To lock a MySQL user account:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$ ALTER USER &#8216;username&#8217;@&#8217;hostname&#8217; ACCOUNT LOCK;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$ ALTER USER &#8216;appuser&#8217;@&#8217;localhost&#8217; ACCOUNT LOCK;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-2.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"780\" height=\"109\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-2.png\" alt=\"\" class=\"wp-image-9854\" style=\"width:530px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-2.png 780w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-2-300x42.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-2-768x107.png 768w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>If the user connects remotely:<\/em><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$ ALTER USER &#8216;appuser&#8217;@&#8217;%&#8217; ACCOUNT LOCK;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After executing this command, the user will not be able to log in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step3: <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can verify whether the account is locked using<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>SELECT user, host, account_locked<br>FROM mysql.user<br>WHERE user=&#8217;appuser&#8217;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If the account is locked, the <em><code>account_locked<\/code> <\/em>column will show &#8220;Y&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3.png\"><img decoding=\"async\" width=\"1024\" height=\"215\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3-1024x215.png\" alt=\"\" class=\"wp-image-9855\" style=\"aspect-ratio:4.763001683352529;width:424px;height:auto\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3-1024x215.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3-300x63.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3-768x161.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3-850x178.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/03\/image-3.png 1112w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important Notes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Locking a user does not delete the account.<\/li>\n\n\n\n<li>Existing active sessions will continue until disconnected.<\/li>\n\n\n\n<li>This method is safer than deleting the user during investigations.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Locking a MySQL user via CLI is a secure and efficient way to immediately restrict database access without permanently removing the account. It is particularly useful during security incidents, maintenance windows, or access reviews.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In database administration, there may be situations where you need to temporarily prevent a user from accessing a MySQL [&hellip;]<\/p>\n","protected":false},"author":496,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-9839","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2yH","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/496"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=9839"}],"version-history":[{"count":3,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9839\/revisions"}],"predecessor-version":[{"id":9856,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9839\/revisions\/9856"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=9839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=9839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=9839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}