TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Install MySQL on Ubuntu and Debian OS
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

Install MySQL on Ubuntu and Debian OS

Last updated: Sep 29, 2023 2:01 pm
By Meenakshi Agarwal
Share
14 Min Read
How to Install MySQL on Ubuntu 16.04 and Debian OS
Install MySQL on Ubuntu and Debian.
SHARE

MySQL is the 2nd most popular database management system commonly used by web applications as a backend. It integrates into Web development frameworks like LAMP (Linux, Apache, MySQL, PHP) to enable database and SQL (Structured Query Language) support. Here we are outlining the steps to install MySQL on Ubuntu 16.04 OS.

Contents
Installing the latest version – MySQL v5.7Installing the older versions – MySQL v5.5 and v5.6Install MySQL 5.5 server and clientInstall MySQL 5.6 server and clientSecuring the MySQL installationEnabling remote database accessInitializing MySQL databaseVerifying MySQL installationEdit MySQL server settingsConnecting to MySQL instanceView Users in MySQLCreate a new MySQL userCreate a new MySQL database.

MySQL support on Ubuntu is unparalleled. It is fast and reliable. Also, Ubuntu 16.04 is an LTS version, which means it would get regular updates until five years from the date of release. And that’s why we recommend an LTS version for installing and managing servers like MySQL and Apache TomCat.

Debian users who want to install MySQL on Debian OS can also follow this tutorial as the commands and syntax are almost the same. However, do let us know of any variation and we’ll address it.

Before installing MySQL on Ubuntu, you’ll have to decide the version of MySQL package for the installation. Its latest version is v5.7, but older versions like v5.5 and v5.6 are also quite popular. In this post, we’ll use all these versions to install MySQL on Ubuntu.

Table of Index.

  • Pre-requisites.
  • How to install MySQL – Ubuntu 16.04
    • Installing the latest version – MySQL v5.7
    • Installing the older versions – MySQL v5.5 and v5.6
    • Install MySQL 5.5 server and client
    • Install MySQL 5.6 server and client
    • Securing the MySQL installation
    • Enabling remote database access
    • Initializing MySQL database
    • Verifying MySQL installation
  • How to configure MySQL on Ubuntu
    • Edit MySQL server settings
  • Managing MySQL database and users
    • Connecting to MySQL instance
    • View Users in MySQL
    • Create a new MySQL user
    • Create a new MySQL database
  • Performance Tuning Your MySQL Server
How to Install MySQL on Ubuntu 16.04 and Debian OS
Install MySQL on Ubuntu and Debian.

Pre-requisites.

  • A Virtual machine or a physical system running Ubuntu 16.04 OS
  • Sudo or Root access to install MySQL
  • Fast and stable internet connection

How to install MySQL on Ubuntu 16.04?

Did you check if the system which you are using to install MySQL has the hostname set? It is a best practice to use a name for accessing the database servers. Make sure the hostname length should not exceed the maximum of 15 characters. You can check the hostname with any of the below commands.

# Display the short host name.
$ hostname

# Display the fully qualified domain name (FQDN).
$ hostname -f

With the commands given here, you can install all versions of MySQL supported by Ubuntu 16.04.

Installing the latest version – MySQL v5.7

To install MySQL v5.7, you can use the version included in Ubuntu’s APT repository. For Ubuntu 16.04, it currently points to the latest MySQL v5.7.

At the onset, we’ll first update the APT repo and upgrade the software already installed on our Ubuntu system.

$ sudo apt-get update
$ sudo apt-get upgrade

Fact – The apt-get update command synchronizes the local package (APT) index with the latest changes whereas the apt-get upgrade command updates the already installed packages on the system.

Next, run the following commands to install both MySQL server and client packages.

$ sudo apt-get install mysql-server mysql-client

During the installation, the system will prompt you to set the database root user password. Make sure the password is hard to guess for others but one that you can easily recall when needed.

Installing the older versions – MySQL v5.5 and v5.6

If you intend to install an older version of MySQL on Ubuntu, then the process is somewhat similar. You need to suffix the version identifier in the package names. Check out the below commands to carry out the installation.

First of all, update the package index and the system.

$ sudo apt-get update
$ sudo apt-get upgrade

Install MySQL 5.5 server and client

$ sudo apt-get install mysql-server-5.5 mysql-client-5.5

Install MySQL 5.6 server and client

$ sudo apt-get install mysql-server-5.6 mysql-client-5.6

Securing the MySQL installation

The installation comes with a security script to control the default settings for root logins and sample users. Run this script with sudo privileges.

$ sudo mysql_secure_installation

Upon execution, the script will prompt you to enter the database root password you set during MySQL installation. Further, it’ll ask some questions which you need to respond with <ENTER> key to accept the default setting. Remember to ignore one question that asks you to modify the root password.

Enabling remote database access

Now, you are all set to start working with MySQL. However, if you like to access the database instance remotely via any client software, then you might run the following commands to allow remote access.

Fact – If you are tired of entering the password for sudo every time a command is run, then use the sudo -s command to save you from any such hassle.

sudo iptables -I INPUT -p tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -I OUTPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT

Initializing MySQL database

To initialize the MySQL version earlier than 5.7, run the following command.

sudo mysql_install_db

For versions, 5.7 and above, you should be using the following command.

mysqld --initialize

In case the, database directory is pre-initialized, the command will issue the following error message.

[ERROR] --initialize specified but the data directory has files in it. Aborting.

Verifying MySQL installation

It is easy to track if the MySQL software is successfully installed or not.

mysql --version

mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper

As the name suggests, the command will give you the version installed.

Now, to check if the MySQL service daemon is running or not, issue the following command.

$ service mysql status

It will return the following status. Please note that the value of process-id will change.

mysql start/running, process 3215

However, if the MySQL server is not running, then you can spawn it by using the below commands.

$ sudo service mysql start

In addition to the above commands, you can test it further by logging into the MySQL database.

$ mysqladmin -p -u root version

mysqladmin Ver 8.42 Distrib 5.7.16, for Linux on x86_64
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version 5.7.16-0ubuntu0.16.04.1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 18 min 25 sec

The above command will verify the successful MySQL installation on Ubuntu.

How to configure MySQL on Ubuntu?

Edit MySQL server settings

The system permits you to alter the default MySQL configuration. If you want to change the default port, IP address, or any other simple setting, then edit the “/etc/mysql/my.cnf“ file.

However, modify the “/etc/mysql/mysql.conf.d/mysqld.cnf" file to update any advanced database server option.

$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

Once you make these changes, you’ll need to restart the MySQL services. You can run any of the below commands.

# Run the following command
$ systemctl restart mysql.service

# Or you can also run the below command.
$ service mysql restart

Managing MySQL database and users

Connecting to MySQL instance

To connect to the installed MySQL instance, you need to run the below command.

$ mysql -u root -p

After entering the database root user password, the above command will open the MySQL command prompt.

At the MySQL shell, you can execute SQL queries.

View Users in MySQL

For viewing the existing database users, run the below query into the MySQL shell.

mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
3 rows in set (0.00 sec)

Create a new MySQL user

You can create standard database users who can connect to the MySQL server and perform database-related tasks.

mysql> CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'test123';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.sys | localhost |
| root | localhost |
| testuser | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

You can also log in with the newly created user.

$ mysql -u testuser -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Create a new MySQL database.

With everything in place, you might now want to create a new database as well. Use the below command to create a fresh MySQL DB.

mysql> CREATE DATABASE techbeamers;
Query OK, 1 row affected (0.00 sec)

You can verify if the new database is available for use. Run the below query.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| techbeamers |
+--------------------+
5 rows in set (0.00 sec)

Next, to update the database, you’ll first of all need to use the database.

mysql> use techbeamers;
Database changed

Let’s now create a dummy table in the newly created database.

mysql> CREATE TABLE state (name VARCHAR(50), capital VARCHAR(256));
Query OK, 0 rows affected (0.01 sec)

You can check if the table gets created successfully or not.

mysql> show tables;
+-----------------------+
| Tables_in_techbeamers |
+-----------------------+
| state |
+-----------------------+
1 row in set (0.00 sec)

Performance Tuning Your MySQL Server.

You can install the MySQL tuner package which comes with a Perl script to optimize the performance and tighten the security of a running MySQL instance.

Run the following commands to install and set up the MySQL tuner.

$ sudo apt-get install mysqltuner

Once you get the MySQL tuner installed, issue the below command to start the performance tuner. But, please make sure that the MySQL server has run for at least 24 hours or longer before you launch the performance tuner.

$ mysqltuner --host <Host name> --user <User Name> --pass <Password>

By default, the tuner will apply the default recommendations which you can change if needed.

Summary – How to Install MySQL on Ubuntu 16.04 OS

We tried to keep all the instructions simple and directed to install MySQL on Ubuntu. And we wish that you would now be able to use them on your systems.

If you still have any open questions about MySQL, then please let us know through your comments. Also, don’t miss to read the below tutorial as well.

MySQL tutorial

Best,
TechBeamers

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article C# Exception Handling Interview Questions. C# Exception Handling – 35 Questions Programmers Should not Miss
Next Article MySQL Tutorials for Beginners Quick Steps to Install MySQL on Windows

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x