DEV Community

S3CloudHub
S3CloudHub

Posted on

How to Install MySQL on Ubuntu

Image description

If you’re looking to set up MySQL on your Ubuntu machine, you’re in the right place! MySQL is one of the most popular relational database management systems, and installing it on Ubuntu is a straightforward process. Whether you’re developing locally or setting up a production server, follow these simple steps to get MySQL up and running on your Ubuntu system.

Step 1: Update Your System

Before installing MySQL, it’s always a good idea to update your system to ensure all the packages are up to date. Open your terminal and run the following command:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

This command will refresh the package list from the repositories. You can also upgrade the existing packages:

sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

Step 2: Install MySQL Server

Once your system is updated, you can install the MySQL server package. Run the following command:

sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

This command installs the latest version of MySQL from Ubuntu’s official repository. During installation, it may prompt you for your sudo password, so be sure to enter it.

Step 3: Secure MySQL Installation

After MySQL is installed, it’s crucial to secure your installation by running a built-in script. This script will help remove insecure default settings. To run the script, use the command:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

You’ll be asked a series of questions:

Set a root password (if not already set during installation).
Remove insecure default settings (e.g., sample users, test databases).
Disable remote root login.
Remove the test database.
It’s highly recommended to answer “Y” to most of the questions to improve your MySQL installation’s security.

Step 4: Check MySQL Status

Once the installation is complete, you can check whether MySQL is running with the following command:

sudo systemctl status mysql
Enter fullscreen mode Exit fullscreen mode

You should see output indicating that the MySQL service is active and running. If it’s not running, you can start it with:

sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

You can also enable MySQL to start on boot by using:

sudo systemctl enable mysql

Step 5: Log in to MySQL

To access the MySQL shell, use the following command:

sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Enter the root password you set during the installation. Once logged in, you’ll be presented with the MySQL command prompt, where you can start creating databases, tables, and managing users.

Step 6: Create a New Database (Optional)

Now that you’re logged in, let’s create a new database for your projects. Run the following SQL commands:

CREATE DATABASE mydatabase;
Enter fullscreen mode Exit fullscreen mode

To verify the database was created, use the command:

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

You should see your new database listed.

Conclusion

Congratulations! You’ve successfully installed MySQL on your Ubuntu machine. You’re now ready to start using MySQL for your applications or projects. Always remember to secure your database and back up your data regularly to avoid any unforeseen issues. If you need to learn more about MySQL, there are plenty of resources available to dive deeper into advanced database management techniques.

Top comments (0)