WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install MySQL on Ubuntu

Edited 2 weeks ago by ExtremeHow Editorial Team

MySQLDatabaseUbuntuServerLinuxInstallationConfigurationOperating SystemsSystemSetup

How to Install MySQL on Ubuntu

This content is available in 7 different language

MySQL is a popular relational database management system. It is widely used due to its speed, reliability, and ease of use. Whether you are setting up a web server or getting involved in PHP and other database-driven applications, MySQL is fundamental. To help you get started with MySQL on your Ubuntu system, here is a comprehensive guide. In this lesson, we will explain in simple terms how you can install MySQL on the Ubuntu operating system. Let's take a look at it step-by-step to ensure clarity and simplicity.

Step 1: Update your system

Before starting the installation process, make sure your Ubuntu system is up-to-date. Run the following command to update the package list for upgrades and other software:

sudo apt update

With the updated package lists, execute the upgrade command to ensure that all installed packages are up to date:

sudo apt upgrade

Updating your system is a necessary step to prevent compatibility and other issues.

Step 2: Install the MySQL package

Ubuntu's APT package repository includes MySQL by default. To install MySQL, use the apt command:

sudo apt install mysql-server

This command will install the core database server, along with the client software and tools you will use to manage the MySQL database. After running the command, the system prompts you to confirm the download and installation. Press Y and then Enter to confirm.

Step 3: Verify the MySQL installation

Once the installation process is complete, make sure that the MySQL service is running without any issues. You can check the status by doing the following:

sudo systemctl status mysql

This command will result in the output that MySQL is up and running.

Step 4: Secure MySQL installation

Out-of-the-box, MySQL has some security flaws, such as default settings that may not be suitable for a production environment. Increase security using mysql_secure_installation script, which allows you to improve the security of your MySQL server:

sudo mysql_secure_installation

During this process, you will be asked several questions related to securing your MySQL installation. It is recommended to answer Yes (Y) to all prompts, especially to set the root password and remove anonymous users.

Step 5: Test the MySQL installation

After securing your installation, log into the MySQL console to make sure everything is working properly. Use the following command:

mysql -u root -p

The system will ask you for the root password you set earlier. If your login is successful, you will find yourself in the MySQL console represented by mysql>. To exit, type:

exit;

Step 6: Create the MySQL database and user

Now that MySQL is working, let's create a sample database and a user account that can access it. First, log back into the MySQL console as the root user:

mysql -u root -p

Once you're logged in, you can create a database:

CREATE DATABASE testdb;

Next, create a new MySQL user account that can access this database. Replace username and 'password' with your preferred username and password:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Now grant the new user all privileges on the new database:

GRANT ALL PRIVILEGES ON testdb.* TO 'username'@'localhost';

Finally, apply the changes:

FLUSH PRIVILEGES;

Your new user username now has full privileges on the database testdb.

Step 7: Configure MySQL remote access (optional)

If you want to connect to your MySQL server from a remote location, you must configure MySQL to allow remote connections. Edit the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Look at this line:

bind-address = 127.0.0.1

Change it to:

bind-address = 0.0.0.0

Save the file and exit. This change allows MySQL to accept connections from any IP address. Restart MySQL to apply the change:

sudo systemctl restart mysql

Be careful when allowing remote access, as this can pose a security risk to your database. Use strong passwords for added security and consider allowed IP address ranges.

Step 8: Customize MySQL (optional)

Performance optimization is also important in database management. To optimize MySQL, you may need to customize settings based on your specific needs and workload. A recommended tool is mysqltuner, which provides suggestions for improving database performance. Install MySQL Tuner with:

sudo apt install mysqltuner

Run it by executing:

mysqltuner

This tool will analyze your MySQL configuration and provide optimization recommendations.

Conclusion

You now have MySQL installed and running on your Ubuntu machine. By following the steps above, you have not only set up the software, but also taken initial steps to secure and optimize it. Maintaining database security and performance is an ongoing task, and regular review of MySQL user accounts and permissions is recommended. As your use case evolves over time, so will your need to adjust configuration settings for MySQL.

With MySQL ready, you can dive deeper into building applications, managing complex databases, or integrating it with many other tools and platforms to meet your project needs. Enjoy your journey with MySQL on Ubuntu!

If you find anything wrong with the article content, you can


Comments