{"id":121552,"date":"2025-01-20T13:57:43","date_gmt":"2025-01-20T13:57:43","guid":{"rendered":"\/tutorials\/?p=121552"},"modified":"2025-01-24T14:28:59","modified_gmt":"2025-01-24T14:28:59","slug":"how-to-install-mysql-ubuntu","status":"publish","type":"post","link":"\/in\/tutorials\/how-to-install-mysql-ubuntu","title":{"rendered":"How to install MySQL on Ubuntu 24.04"},"content":{"rendered":"<p>Hosting an application or dynamic website requires you to set up a database on the backend to store user&rsquo;s information. A popular option is MySQL, which is widely used for various projects with different web stacks.<\/p><p>To help you get started quicker, this tutorial will guide you through the steps to install MySQL on <strong>Ubuntu 24.04<\/strong>. We will also cover basic MySQL configuration to help you prepare your database for deployment.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites-for-installing-mysql-on-ubuntu\">Prerequisites for installing MySQL on Ubuntu<\/h2><p>Before installing MySQL, make sure you have a virtual private server (VPS) to host the database. It can be a dedicated machine or the same system that will run your web application so long as it has sufficient CPU cores, RAM, and storage.<\/p><p>MySQL doesn&rsquo;t have a strict minimum hardware requirement. This will greatly depend on the amount of traffic and data you expect to handle. However, a good starting point would be a system with a <strong>single-core<\/strong> CPU, <strong>2 GB<\/strong> of RAM, and <strong>10 GB <\/strong>of storage.<\/p><p>If you don&rsquo;t have a host machine, we recommend <a href=\"\/in\/vps-hosting\">Hostinger&rsquo;s Linux VPS solution<\/a>, which starts at <strong>\u20b9599.00\/month<\/strong>. In addition to reliable hardware, our VPS features like<strong> Kodee AI assistant <\/strong>and <strong>Browser Terminal<\/strong> can actually help simplify the MySQL installation process for beginners.<\/p><p>You can connect to your VPS as root directly from your browser using <strong>Browser Terminal <\/strong>and ask<strong> Kodee<\/strong> for guidance on managing your database. You will see these tools in action in the later sections.<\/p><p>In addition, make sure your system runs <strong>Ubuntu 24.04<\/strong> to avoid compatibility issues and security risks that are often present in older versions. Hostinger users can easily install this version of Ubuntu without commands by <a href=\"https:\/\/support.hostinger.com\/en\/articles\/4965922-how-to-change-the-operating-system-of-your-vps\" target=\"_blank\" rel=\"noopener\">using the operating system template<\/a>.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Need simpler installation process?<\/h4>\n                    <p>Hostinger users can easily install MySQL on their VPS without commands by simply setting up the operating system template. However, this database is only available as a part of pre-configured web stacks and runs on Ubuntu 22.04.<\/p>\n                <\/div>\n\n\n<?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-installing-mysql-on-a-ubuntu-24-04-vps\">Installing MySQL on a Ubuntu 24.04 VPS<\/h2><p>Once you&rsquo;re sure you meet the prerequisites, follow the steps below to install MySQL on Ubuntu 24.04.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-connect-to-your-server\">1. Connect to your server<\/h3><p>Since installing MySQL requires remotely running commands on your VPS, we must connect to it via SSH <a href=\"\/in\/tutorials\/how-to-use-putty-ssh\">using PuTTY<\/a> or Terminal.<\/p><p>If you use Terminal, run the following command to connect to your remote server. Remember to replace <strong>185.185.185.185<\/strong> with your VPS&rsquo; actual IP address:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ssh root@185.185.185.185<\/pre><p>Alternatively, Hostinger users can use the built-in <strong>Browser terminal <\/strong>feature.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df153e2f35d\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"362\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/01\/the-browser-terminal-button-on-vps-management-page-1024x362.png\" alt=\"Browser terminal button on the VPS management page\" class=\"wp-image-121558\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/the-browser-terminal-button-on-vps-management-page-1024x362.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/the-browser-terminal-button-on-vps-management-page-300x106.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/the-browser-terminal-button-on-vps-management-page-150x53.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/the-browser-terminal-button-on-vps-management-page-768x271.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/the-browser-terminal-button-on-vps-management-page.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Simply open <strong>hPanel<\/strong> &rarr; <strong>sidebar<\/strong> &rarr; <strong>VPS<\/strong> &rarr; <strong>Manage<\/strong> and click the <strong>Browser terminal<\/strong> button on the top right. This tool automatically logs you in as root, making the process more efficient.<\/p><h3 class=\"wp-block-heading\" id=\"h-2-update-package-index\">2. Update package index<\/h3><p>Once logged in as <strong>root<\/strong>, let&rsquo;s update your system&rsquo;s package repository to ensure you get the latest version of MySQL. To do so, run the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update<\/pre><p>While not mandatory, we also recommend upgrading installed packages to the latest version using this command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt upgrade<\/pre><p>Updating all packages before installing MySQL helps prevent incompatibility issues that might arise when using older software.<\/p><h3 class=\"wp-block-heading\" id=\"h-3-install-mysql\">3. Install MySQL<\/h3><p>Now that your system is ready, let&rsquo;s install the MySQL server package by running the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install mysql-server<\/pre><p>Press <strong>Y<\/strong> if your command line asks for confirmation. Wait until the installation finishes, which should take around 15 minutes, depending on your internet connection speed.<\/p><p>After the installation is complete, run the following command to verify that MySQL is properly configured:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysql -V<\/pre><p>If your command line returns the installed MySQL version number, the database server is installed. Otherwise, you will see the &ldquo;<strong>Command not found error<\/strong>,&rdquo; which means you should rerun the installation command.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df153e301e7\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"53\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/01\/terminal-returns-installed-mysql-version-1024x53.png\" alt=\"Terminal returns installed MySQL version\" class=\"wp-image-121564\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/terminal-returns-installed-mysql-version-1024x53.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/terminal-returns-installed-mysql-version-300x15.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/terminal-returns-installed-mysql-version-150x8.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/terminal-returns-installed-mysql-version-768x40.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/terminal-returns-installed-mysql-version.png 1282w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>\n\n<div><p class=\"important\"><strong>Important!<\/strong> Note the uppercase V option. Using lowercase can return an error since <a href=\"\/in\/tutorials\/linux-commands\">Linux commands<\/a> are case-sensitive.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-4-secure-mysql-installation\">4. Secure MySQL installation<\/h3><p>MySQL will use the default configuration out of the box, which can be insecure. For example, it doesn&rsquo;t set up a password for the root database user, allowing unauthorized parties to access the account.<\/p><p>To minimize the security risks, run the MySQL secure installation wizard by entering the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mysql_secure_installation<\/pre><p>During the wizard, you can adjust several security configurations by answering the prompted questions. Here is what you can do during the process:<\/p><ul class=\"wp-block-list\">\n<li>Validate password strength to prevent users from using weak credentials.<\/li>\n\n\n\n<li>Disallow root login remotely, allowing database access only from localhost.<\/li>\n\n\n\n<li>Delete anonymous user accounts, which enable all users to log in to your database.&nbsp;<\/li>\n\n\n\n<li>Remove the default test database that is accessible to all users.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-5-verify-mysql-functionality\">5. Verify MySQL functionality<\/h3><p>After installing MySQL, let&rsquo;s verify whether it works properly. Start by checking whether it is running using this command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl status mysql<\/pre><p>You should see MySQL service status as <strong>Active (Running)<\/strong>. Otherwise, you can start it manually with this command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl start mysql<\/pre><p>Alternatively, Hostinger users can quickly check the MySQL service status by asking <strong>Kodee AI assistant<\/strong>. Open <strong>hPanel <\/strong>and click <strong>Manage<\/strong> on your VPS.<strong> <\/strong>Navigate to the sidebar and select <strong>Kodee<\/strong>. Then, ask your question like so.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df153e31117\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"309\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/01\/kodee-checks-service-status-of-a-vps-1024x309.png\" alt=\"Kodee checks a service status of a VPS\" class=\"wp-image-121566\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/kodee-checks-service-status-of-a-vps-1024x309.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/kodee-checks-service-status-of-a-vps-300x90.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/kodee-checks-service-status-of-a-vps-150x45.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/kodee-checks-service-status-of-a-vps-768x231.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/kodee-checks-service-status-of-a-vps.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Now, let&rsquo;s enter the MySQL shell. This is where you will run SQL statements to interact with your database, including creating a new table for fetching data. Here&rsquo;s the command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo mysql -u root<\/pre><p>Enter the root password, and your command line should change to <strong>MySQL&gt;<\/strong>. It means MySQL works properly, and you can start managing the database.<\/p><h2 class=\"wp-block-heading\" id=\"h-basic-mysql-configuration\">Basic MySQL configuration<\/h2><p>You might need to change several basic MySQL configurations to ensure your database functions based on your requirements.<\/p><p><strong>&#8203;&#8203;Changing the data directory<\/strong><\/p><p>As your database gets populated with more data, it will need more storage space and resources for more intensive I\/O operations. If the current hosting environment doesn&rsquo;t have enough hardware allocation, your application might crash.<\/p><p>Changing the data directory resolves this issue by moving your database storage location elsewhere. For a fresh MySQL installation, doing so lets you separate the database from your server&rsquo;s system files, preventing drive overload.<\/p><p>Here&rsquo;s how to move your database directory:<\/p><ol class=\"wp-block-list\">\n<li>Log in to the MySQL root user account:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysql -u root -p<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Run the following statement to check your active data directory location, which is <strong>\/var\/lib\/mysql\/<\/strong> by default:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT @@datadir;<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Type<strong> exit <\/strong>to quit the MySQL shell and return to the main command line.<\/li>\n\n\n\n<li>Stop your MySQL service temporarily to avoid data changes when you migrate it:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl stop mysql<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Move the data directory to the new location using <a href=\"\/in\/tutorials\/how-to-use-rsync\">rsync<\/a>. Remember to replace the destination placeholder with the actual value:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo rsync -av \/var\/lib\/mysql \/path\/destination<\/pre><ol start=\"6\" class=\"wp-block-list\">\n<li>Reconfigure MySQL to locate the data in the new directory. Start by opening the configuration file <a href=\"\/in\/tutorials\/how-to-install-and-use-nano-text-editor\">using a text editor like Nano<\/a>:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/pre><ol start=\"7\" class=\"wp-block-list\">\n<li>Enter your new data directory into the <strong>datadir <\/strong>variable like so:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">datadir=\/path\/destination<\/pre><ol start=\"8\" class=\"wp-block-list\">\n<li>Change Ubuntu&rsquo;s <strong>AppArmor<\/strong> configuration to allow MySQL to modify data in the new directory. Here&rsquo;s the command:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"alias \/var\/lib\/mysql\/ -&gt; \/your\/new\/datadir\/,\" &gt;&gt; \/etc\/apparmor.d\/tunables\/alias<\/pre><ol start=\"9\" class=\"wp-block-list\">\n<li>Reload AppArmor to apply the changes:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo \/etc\/init.d\/apparmor reload<\/pre><ol start=\"10\" class=\"wp-block-list\">\n<li>Restart the MySQL service using the following command:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl start mysql<\/pre><p>The steps to move the data directory to another server are similar, except you use a remote destination with <strong>rsync<\/strong>. You should also edit the server&rsquo;s MySQL configuration file to point to the new storage.<\/p><p><strong>Enabling remote access<\/strong><\/p><p>By default, you can only access your database from the host server. MySQL blocks remote connections from external systems to prevent unauthorized access that might compromise security.<\/p><p>However, this behavior prevents you from integrating third-party programs, managing the database from another machine, or connecting applications hosted on another system, which might cause functionality issues.<\/p><p>To allow other systems to access your database, add their IP addresses to your MySQL configuration file. To learn more about the steps, read our tutorial on <a href=\"\/in\/tutorials\/mysql\/how-to-grant-remote-access-mysql-vps\">granting MySQL remote access<\/a>.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Since remote access on your MySQL database can expose your server to security risks, only enable it if needed.<br>\n<br>\nIf you need remote access, make sure your MySQL and firewall configuration only allow trusted IP addresses. Also, use secure passwords for all remote MySQL user accounts and avoid granting them all privileges unless necessary.<br>\n<br>\nWe also recommend <a href=\"https:\/\/www.geeksforgeeks.org\/how-to-enable-mysql-query-log\/\" target=\"_blank\" rel=\"noopener\">enabling MySQL logging<\/a> by editing the configuration file. This setting helps you identify malicious activities in your database more easily.<\/p><\/div>\n\n\n\n<\/p><p>Once remote access is enabled, make sure you have <a href=\"\/in\/tutorials\/mysql\/how-create-mysql-user-and-grant-permissions-command-line\">created a MySQL user<\/a> with the privilege to connect to the database server.<\/p><p>Log in to MySQL remotely by running the following command on the permitted machine. Replace <strong>username <\/strong>with the actual account name and enter your password:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysql -u username -h your_vps_ip -p<\/pre><p>To allow another system to access an existing database, add its IP address as the host. After logging in to the root MySQL user, enter the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE mysql.db SET Host='your_remote_system_ip' WHERE Db=ExistingDatabaseName;<\/pre><p><strong>Adjusting the MySQL configuration file<\/strong><\/p><p>You can change various MySQL settings, including network and logging, by editing the configuration files.<\/p><p>Note that MySQL has several configuration files, and you must find out which one your database server is using by running the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysql --help | grep -A1 'Default options are read from the following files in the given order'<\/pre><p>You should see several paths that indicate the global or user-specific configuration files. Check out the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/option-files.html\" target=\"_blank\" rel=\"noopener\">MySQL option file documentation<\/a> to learn more about their differences.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df153e323dd\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"67\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/01\/mysql-shows-active-configuration-file-1024x67.png\" alt=\"MySQL shows active configuration files\" class=\"wp-image-121569\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/mysql-shows-active-configuration-file-1024x67.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/mysql-shows-active-configuration-file-300x20.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/mysql-shows-active-configuration-file-150x10.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/mysql-shows-active-configuration-file-768x50.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2025\/01\/mysql-shows-active-configuration-file.png 1252w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>For this tutorial, let&rsquo;s edit the global configuration file, which is <strong>\/etc\/mysql\/my.cnf<\/strong>. To do so, open it using a text editor:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nano \/etc\/mysql\/my.cnf<\/pre><p>Since <strong>my.cnf<\/strong> has no configuration by default, you must manually add the settings. This file takes the following parameters:<\/p><ul class=\"wp-block-list\">\n<li><strong>#commen<\/strong>t or <strong>;comment<\/strong> &ndash; optional notes to explain your configuration. MySQL won&rsquo;t read these comments.&nbsp;<\/li>\n\n\n\n<li><strong>[group]<\/strong> &ndash; a group or program to which you want to apply the configuration. A group setting ends on the next <strong>[group] <\/strong>line.&nbsp;<\/li>\n\n\n\n<li><strong>opt_name=value<\/strong> &ndash; your MySQL configuration parameter in a key-value pair.&nbsp;<\/li>\n<\/ul><p>To check the available settings, run the following command. Under the <strong>Variables <\/strong>line, you will see the configuration parameters and their values:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mysql --help<\/pre><p>Your MySQL database configuration might look like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[client]\nport=3306\nsocket=\/tmp\/mysql.sock\n\n[mysqld]\nport=3306\nsocket=\/tmp\/mysql.sock\nkey_buffer_size=16M\nmax_allowed_packet=128M\n\n[mysqldump]\nquick<\/pre><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Hosting a dynamic website or application requires setting up a database like MySQL on your server. To help you get started faster, we have explained the steps for installing it on <strong>Ubuntu 24.04<\/strong>. Here&rsquo;s the recap:<\/p><ol class=\"wp-block-list\">\n<li>Connect to your server as <strong>root <\/strong>via SSH using PuTTY, Terminal, or Hostinger&rsquo;s Browser terminal.<\/li>\n\n\n\n<li>Run the <strong>apt update<\/strong> command to renew your system&rsquo;s repository, ensuring you get the newer MySQL version.<\/li>\n\n\n\n<li>Download and install the MySQL package using the <strong>apt install <\/strong>command.<\/li>\n\n\n\n<li>Run MySQL&rsquo;s secure installation script to adjust your database security configuration.<\/li>\n\n\n\n<li>Verify MySQL functionality by checking the service status and entering the database shell.&nbsp;<\/li>\n<\/ol><p>Optionally, you can modify the <strong>mysqld.cnf <\/strong>or <strong>my.cnf<\/strong> configuration files to adjust your MySQL settings. For example, you can change the default data directory, enable a remote connection, or open a different port for your database.<\/p><p>After installing MySQL, <a href=\"\/in\/tutorials\/how-to-install-and-setup-phpmyadmin-on-ubuntu\">configure phpMyAdmin on your Ubuntu server<\/a> so you can easily manage your database using a graphical interface.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-mysql-on-ubuntu-faq\">How to install MySQL on Ubuntu FAQ<\/h2><details class=\"wp-block-details has-large-font-size is-layout-flow wp-block-details-is-layout-flow\" open><summary>What versions of Ubuntu are compatible with MySQL installation?<\/summary>\n<p class=\"has-medium-font-size\"> \n<br>\nTechnically, you can install MySQL on any version of Ubuntu, including the older ones like <strong>16.04<\/strong>. However, we don&rsquo;t recommend doing so for the following reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"has-medium-font-size\"><strong>Incompatibility issues<\/strong>. Using a very old version of Ubuntu with newer MySQL releases might cause some features or functionality to not work properly.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Security risks<\/strong>. Old software commonly has unpatched safety flaws that cybercriminals might exploit.<\/li>\n\n\n\n<li class=\"has-medium-font-size\"><strong>Bugs<\/strong>. Legacy Ubuntu and MySQL might have bugs that can affect functionality.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\">To avoid these issues, we recommend always using the latest version of both software. For instance, it is best to use Ubuntu <strong>24.04<\/strong>, <strong>22.04<\/strong>, or<strong> 20.04<\/strong> with the newest MySQL.<\/p>\n<\/details><details class=\"wp-block-details has-large-font-size is-layout-flow wp-block-details-is-layout-flow\" open><summary>How do I uninstall MySQL?<\/summary>\n<p class=\"has-medium-font-size\">\n<br>\nTo uninstall MySQL on Ubuntu, start by stopping the database service using the following command:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo systemctl stop mysql<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Then, use APT to uninstall MySQL:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo apt remove mysql-client mysql-server -y<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Finish the process by deleting leftover data like unnecessary packages and caches by running these commands sequentially:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo apt autoremove -y&nbsp;<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo apt autoclean -y<\/code><\/p>\n<\/details><details class=\"wp-block-details has-large-font-size is-layout-flow wp-block-details-is-layout-flow\" open><summary>Can I install MySQL Workbench alongside MySQL Server?<\/summary>\n<p class=\"has-medium-font-size\">\n<br>\nYes, you can. MySQL Workbench will only work if your system has the MySQL server. To set it up, download the installer using the following command:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo curl https:\/\/dev.mysql.com\/get\/mysql-apt-config_0.8.33-1_all.deb&nbsp;<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Unpack the installer by running this command:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb&nbsp;<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Finish the package setup wizard by choosing your preferred configurations. Once finished, update your package manager:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo apt update<\/code><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Install MySQL workbench from APT by running this command:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo apt install mysql-workbench-community -y<\/code><\/p>\n<\/details>\n","protected":false},"excerpt":{"rendered":"<p>Hosting an application or dynamic website requires you to set up a database on the backend to store user&rsquo;s information. A popular option is MySQL, which is widely used for various projects with different web stacks. To help you get started quicker, this tutorial will guide you through the steps to install MySQL on Ubuntu [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/how-to-install-mysql-ubuntu\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[22642,22640],"tags":[],"class_list":["post-121552","post","type-post","status-publish","format-standard","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-install-mysql-ubuntu","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-install-mysql-ubuntu","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/121552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=121552"}],"version-history":[{"count":14,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/121552\/revisions"}],"predecessor-version":[{"id":121763,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/121552\/revisions\/121763"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=121552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=121552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=121552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}