DEV Community

Anthony Gilbert
Anthony Gilbert

Posted on

How to Install and Configure MySQL on Ubuntu

MySQL is one of the most popular relational database management systems. In this post, we’ll walk through a simple process to install and set up MySQL on Ubuntu.

Step 1: Update Your Package List
Before installing any new packages, update your local package index:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Step 2: Install MySQL Server
Install MySQL by running:

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

This command installs the MySQL server package and any required dependencies.

Step 3: Secure Your MySQL Installation
After installation, run the built-in security script to improve your MySQL configuration:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted to configure settings like:

Validating password plugins
Setting a strong root password
Removing anonymous users
Disabling remote root login
Removing the test database
Answer the prompts according to your security needs.

Step 4: Verify MySQL is Running
Check the status of the MySQL service to ensure it’s active:

sudo systemctl status mysql
Enter fullscreen mode Exit fullscreen mode

If MySQL isn’t running, start it with:

sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

Step 5: Log Into MySQL
Access the MySQL shell using the root account:

sudo mysql
Enter fullscreen mode Exit fullscreen mode

From here, you can execute SQL commands to create databases, users, and manage your database server.

Step 6: Create a New Database and User (Optional)
For added security and organization, it’s a good idea to create a dedicated database and user rather than using the root account. Here’s an example:

Create a new database:

CREATE DATABASE my_database;
Enter fullscreen mode Exit fullscreen mode

Create a new user and grant privileges:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON my_database.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Replace my_database, myuser, and mypassword with your desired names and secure password.

Top comments (0)