{"id":590,"date":"2023-12-01T07:57:01","date_gmt":"2023-12-01T07:57:01","guid":{"rendered":"https:\/\/blog.hostinger.io\/support\/2023\/12\/01\/8661850-how-to-secure-mysql-on-ubuntu-22-04-with-lamp-lemp-stack-templates\/"},"modified":"2026-08-06T07:40:54","modified_gmt":"2026-08-06T07:40:54","slug":"8661850-how-to-secure-mysql-on-ubuntu-22-04-with-lamp-lemp-stack-templates","status":"publish","type":"post","link":"https:\/\/www.hostinger.com\/support\/8661850-how-to-secure-mysql-on-ubuntu-22-04-with-lamp-lemp-stack-templates\/","title":{"rendered":"How to secure MySQL on Ubuntu 22.04 with LAMP\/LEMP Stack templates"},"content":{"rendered":"<p class=\"no-margin\">A <b><a class=\"intercom-content-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/LAMP_(software_bundle)\" target=\"_blank\" rel=\"noopener\">LAMP<\/a><\/b> stack is a<b> bundle of open-source software <\/b>commonly installed together, transforming a server into a hub for hosting dynamic websites and PHP-based web applications. The acronym breaks down into the <b>Linux <\/b>operating system and the <b>Apache <\/b>web server; site data finds its home in a<b> MySQL<\/b> database, while<b> PHP<\/b> takes the lead in processing dynamic content.<\/p><h2 id=\"h-change-the-root-authentication-method\">Change the root authentication method<\/h2><p class=\"no-margin\">To <b>authenticate to MySQL with a root user using a password<\/b>, follow these steps &#128071;<\/p><p class=\"no-margin\">First, open up the <b>MySQL prompt<\/b>:<\/p><pre><code>sudo mysql<\/code><\/pre><p class=\"no-margin\">Then, run the following <code>ALTER USER<\/code> command to <b>change the root authentication method<\/b>. The following example changes the authentication method to <code>mysql_native_password<\/code>:<\/p><pre><code>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';<\/code><\/pre><p class=\"no-margin\">Finally, exit the MySQL prompt:<\/p><pre><code>exit<\/code><\/pre><h2 id=\"h-run-mysql_secure_installation\">Run mysql_secure_installation<\/h2><p class=\"no-margin\">Following that, we recommend that you <b>run a script <\/b>that comes pre-installed with MySQL <b>to configure the access and security<\/b> of MySQL as you like. It is an<b> interactive script<\/b> that requires you to answer <code>Yes<\/code> or <code>No<\/code> to the provided conditions.<\/p><p class=\"no-margin\">Start the script by running the following command:<\/p><pre><code>sudo mysql_secure_installation<\/code><\/pre><p class=\"no-margin\">This will ask if you want to configure <code>VALIDATE_PASSWORD_PLUGIN<\/code>:<\/p><pre><code>VALIDATE PASSWORD PLUGIN can be used to test passwords\r\nand improve security. It checks the strength of password\r\nand allows the users to set only those passwords which are\r\nsecure enough. Would you like to set up VALIDATE PASSWORD plugin?\r\n\r\nPress y|Y for Yes, any other key for No:<\/code><\/pre><p class=\"no-margin\">Note that if enabled, <b>passwords that don&rsquo;t match the specified criteria<\/b> <b>will be rejected<\/b> by MySQL with an error.<\/p><pre><code>There are three levels of password validation policy:\r\n\r\nLOW    Length &gt;= 8\r\nMEDIUM Length &gt;= 8, numeric, mixed case, and special characters\r\nSTRONG Length &gt;= 8, numeric, mixed case, special characters and dictionary              file\r\n\r\nPlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: <\/code><\/pre><p class=\"no-margin\">You will also be asked about the following:<\/p><div class=\"intercom-interblocks-table-container\">\n<table role=\"presentation\">\n<tbody>\n<tr>\n<td style=\"background-color: #e3e7fa80;width: 244px\">\n<p class=\"intercom-align-center no-margin\"><b>Prompt<\/b><\/p>\n<\/td>\n<td style=\"background-color: #e3e7fa80\">\n<p class=\"intercom-align-center no-margin\"><b>Explanation<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 244px\">\n<p class=\"no-margin\"><b>Remove Anonymous Users<\/b><\/p>\n<\/td>\n<td>\n<p class=\"no-margin\">The script will ask if you want to remove anonymous users:<\/p>\n<ul>\n<li>\n<p class=\"no-margin\">Press <b>Y<\/b> to remove (recommended), or <b>N<\/b> to keep them<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">It&rsquo;s generally a good idea to remove anonymous users for security reasons<\/p>\n<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 244px\">\n<p class=\"no-margin\"><b>Disallow Root Login Remotely<\/b><\/p>\n<\/td>\n<td>\n<p class=\"no-margin\">You&rsquo;ll be asked if you want to disallow root login remotely:<\/p>\n<ul>\n<li>\n<p class=\"no-margin\">Press <b>Y<\/b> to disallow it (recommended), or <b>N<\/b> if you need to log in remotely as the root user<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">For security reasons, it&rsquo;s recommended to disallow remote root login<\/p>\n<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 244px\">\n<p class=\"no-margin\"><b>Remove the Test Database<\/b><\/p>\n<\/td>\n<td>\n<p class=\"no-margin\">The script will ask if you want to remove the test database:<\/p>\n<ul>\n<li>\n<p class=\"no-margin\">Press <b>Y<\/b> to remove (recommended), or <b>N<\/b> to keep it<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">It&rsquo;s advisable to remove the test database, especially in a production environment<\/p>\n<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 244px\">\n<p class=\"no-margin\"><b>Reload Privilege Tables<\/b><\/p>\n<\/td>\n<td>\n<p class=\"no-margin\">The script will reload the privilege tables to apply the changes you made. Press <b>Y<\/b> to reload the tables.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div><p class=\"no-margin\">And that&rsquo;s it! You&rsquo;ve successfully secured <b>MySQL <\/b>on an<b> <a class=\"intercom-content-link\" href=\"https:\/\/www.hostinger.com\/support\/9901438-how-to-set-up-ubuntu-remote-desktop-hostinger-vps\/\" target=\"_blank\" rel=\"noopener\">Ubuntu 22.04 Lamp\/Lemp Stack VPS<\/a><\/b> provided by Hostinger.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enhancing Database Security: A Comprehensive Guide to Safeguarding MySQL on Ubuntu 22.04 Using Lamp\/Lemp Stack Templates<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"include_on_kodee":true,"footnotes":""},"categories":[199],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-vps-management"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/support\/8661850-how-to-secure-mysql-on-ubuntu-22-04-with-lamp-lemp-stack-templates\/","default":1},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/support\/8661850-como-proteger-mysql-en-ubuntu-22-04-con-plantillas-lamp-lemp-stack-en-hostinger\/","default":0}],"include_on_kodee":true,"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":2,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":14666,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/590\/revisions\/14666"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}