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.

How to retrieve the default MySQL password

In some cases, you may not be prompted to enter a password for the root user. When this happens, you need to be aware of how to access the database so that you can create other users. Below are a few tricks you can use to get access to the MySQL database. Once in, you can create your preferred user.

Option 1:
#Run the following command to access mysql database. Once prompted, enter your sudo password:
> sudo mysql --defaults-file=/etc/mysql/debian.cnf

Option 2:
> sudo cat /etc/mysql/debian.cnf
#This file has some default username and password. Use that to access the database, see example.
mysql -u debian-sys-maint -p #use the password provided.

Option 3:
#Run the mysql as sudo, this doesnt ask for password
> sudo mysql -u root

Option 4:
#Run the following:
> sudo -i
> mysql

Option 5:
> sudo mysql

Once logged in, we can proceed with running 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

Installing phpMyAdmin on Linux

After setting up MySQL on your Linux machine, you may want to manage your databases with a web-based interface. phpMyAdmin is a popular tool that allows you to interact with MySQL through a web browser, making database management easier. Follow be steps below:

  1. Install phpMyAdmin and Dependencies: Install phpMyAdmin and the required PHP extensions:
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl

During the installation:

Select Apache: When prompted to choose a web server, select Apache by pressing the spacebar and then Enter.
Database Configuration: You’ll also be asked if you want to configure phpMyAdmin with dbconfig-common. Select Yes and provide your MySQL root password when prompted.

  1. Enable the PHP mbstring Extension: The mbstring extension is necessary for phpMyAdmin to function. Enable it with the following command:
sudo phpenmod mbstring
  1. Restart Apache: Restart your Apache server to apply the changes:
sudo systemctl restart apache2
  1. Access phpMyAdmin: Once the installation is complete, visit the URL and input your MySQL login details. These are the details set during installation. If you were not prompted to set when installing MySQL, refer to the process above on how to access the default login details.
http://localhost/phpmyadmin

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: