You are currently viewing How to Install MySQL Database on Linux

How to Install MySQL Database on Linux

MySQL is a widely used open-source relational database management system. In this article, we will guide you through the steps to install MySQL database on a Linux system with the main focus on the Ubuntu operating system.

Prerequisites

To do the installation, ensure you have access to the following:

  • An Ubuntu system (preferably an updated version).
  • Access to a terminal or command line.
  • Sudo privileges or access to the root user.

How to Install MYSQL Database

Step 1: Update Package Lists

Update the package lists for upgrades and new package installations:

sudo apt update

Step 2: Install MySQL Server

Install the MySQL server package using the following command. During the installation, the system may prompt you to set a root password for MySQL. Set it as required or have it set when setting the security primitives of mysql database.

sudo apt install mysql-server

Step 3: Configure MySQL

To secure your MySQL installation and improve its security, run the MySQL secure installation script:

sudo mysql_secure_installation #If this returns an error, proceed to step 4; then back to step 3

Follow the prompts to complete the security configurations.

Step 4: Start and Enable MySQL Service

Start the MySQL service and enable it to start on boot:

sudo systemctl start mysql  #Start mysql service
sudo systemctl enable mysql #enable the service to start on reboot
OR
#If systemctl is not configured
service mysql start  #Start mysql service
service mysql enable #enable the service to start on reboot

Step 5: Verify MySQL Installation

To verify that MySQL has been successfully installed and is running, execute the following command to access the MySQL shell:

mysql -u root -p  #then enter the root password , set on previous steps.

Once logged in, we can now proceed and run the database queries.

Create a database and switch to it

CREATE DATABASE mydatabase;
USE mydatabase;

Create a User and Set a Password:

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

Grant Privileges to the User on the Database:

GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';

Flush Privileges:

FLUSH PRIVILEGES;

Create a Table and Insert Data:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    age INT
);

INSERT INTO users (name, age) VALUES ('John Doe', 30);
INSERT INTO users (name, age) VALUES ('Alice Smith', 25);
select * from users #returns all data from users table

Make a donation to support us



Web Hosting and email hosting Packages


For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.


Related content: