Step-by-Step: Installing MySQL 8.0 on Linux

Step-by-Step: Installing MySQL 8.0 on Linux

If you’re looking to set MySQL database server, this step-by-step tutorial will walk you through installing the latest production release of MySQL 8.0 on a Linux platform from scratch. Installing your own MySQL instance locally is a great way to start learning SQL and database administration.

Let’s get started!

An Overview of MySQL 8.0

MySQL is a popular open source relational database management system that is very fast, scalable, and easy to use. MySQL 8.0 is the most recent major release, bringing with it a number of enhancements:

  • Faster performance via caching and indexing improvements
  • Better security through roles, authentication plugins, and TLS support
  • JSON support for NoSQL-style access
  • Atomic DDL for changed data consistency
  • Enhanced high availability with InnoDB cluster

MySQL is versatile can be used for web/mobile apps, streaming analytics, government databases, financial platforms, and almost any application that requires structured data with transactions, reporting, etc. Given MySQL’s popularity across web-based applications, it’s a must-learn technology for any aspiring software engineer.

Pre-Installation Considerations

Before starting your MySQL install, you should check and update your Linux distro if needed, ensure sufficient disk space exists, and create a special OS account for MySQL to run under for improved security.

First update package info and upgrade all packages to latest stable releases:

sudo apt update
sudo apt upgrade

Then check free disk space (at least 1-2GB recommended for MySQL):

df -h /

Next add a MySQL system user called “mysql” for the MySQL server process to run as:

sudo adduser mysql

Downloading the MySQL Installer for Linux

Now we’re ready to download the latest MySQL 8.0 community edition server installer for Linux systems. MySQL AB makes RPM and Debian packages available through it’s official APT and YUM repositories.

First you’ll need to add the MySQL APT channel to your sources list:

sudo nano /etc/apt/sources.list

Add the following line:

deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-8.0

Then download the MySQL public GPG key and add to apt trusted keys:

wget http://repo.mysql.com/RPM-GPG-KEY-mysql-2022 -O /tmp/RPM-GPG-KEY-mysql-2022
sudo apt-key add /tmp/RPM-GPG-KEY-mysql-2022

Update apt package lists after adding MySQL repo:

sudo apt update

With the MySQL APT channel configured, now install MySQL server and client development libraries:

sudo apt install mysql-community-server mysql-client libmysqlclient-dev

Follow prompts to begin the MySQL server install.

Configuring the MySQL Installation

During the interactive setup process, you’ll be asked to make choices about your database configuration. Let’s go over what each option means.

First you need to create and confirm a root password for managing your MySQL instance. Make this a strong password you won’t forget.

Next, you can choose to skip validating the initial MySQL users table during setup. Validating ensures the system database containing your users and permissions was created properly. But skipping validation speeds up setup when first testing.

You’ll then be asked to configure MySQL networking. The bind address determines the network interfaces MySQL listens on. Choosing 0.0.0.0 allows connecting to MySQL from anywhere, while 127.0.0.1 only allows local connections. Choose networking setup accordingly.

For community edition installs, the decisional component system isn’t required and can be disabled to save memory. But in production environments this should be enabled.

Finally, make a decision on transactional binary logging. Binary logs contain data change events which can be replicated or used in point-in-time restores. Enable this for production systems, but disable when first getting started to reduce overhead.

Setting Up Security Policies

Now your MySQL server is installed and running, but it’s currently wide open without any actual user accounts defined other than root. Below are a few key security policies to put in place:

Enable Validation and Expire Default Root Password

Log into the MySQL shell as root without a password prompt due to no validation (type exit to quit):

mysql -u root

To expire the temporary root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY '' PASSWORD EXPIRE;

Now root password must be reset before access is allowed again.

Setup Password Validation Plugin

Create a password validation policy requiring a minimum of 8 characters, both upper and lowercase letters, a number, and a symbol:

CREATE VALIDATION `StrngPsswrd` FOR CURRENT_USER() 
REQUIRE CONTAINS `Upper`, `Lower`, `Digit`, `Special`, LENGTH 8;

SET GLOBAL validate_password.policy=StrngPsswrd;

Remove Anonymous User Accounts

In older MySQL versions, anonymous users were added by default for local and remote access. These should be removed:

“`sql
DROP USER ”@’localhost’;
DROP USER ”@’127.0.0.1′;

Creating Admin User Accounts
Usually separate admin accounts are setup beyond root access for managing databases, users, backups etc. Here is how you might create an admin user:

**Create Admin and Grant Privileges** 

sql
CREATE USER ‘myadmin’@’localhost’ IDENTIFIED BY ‘securepassword123!’;

GRANT ALL ON . TO ‘myadmin’@’localhost’ WITH GRANT OPTION;

This creates myadmin with a strong password and grants it complete access to all database resources.

**Test Admin Account**

Open a new terminal, connect/validate as myadmin locally:


mysql -u myadmin -p
SHOW DATABASES;
EXIT;

Confirm you can access MySQL without issue before moving on.

Testing Remote MySQL Connectivity 
If MySQL networking is bound to 0.0.0.0 allowing remote connections, test connectivity from another system to validate. 

Here we test from a remote Ubuntu desktop to the server using both IP and hostname:

mysql -u myadmin -p -h 192.168.5.22
mysql -u myadmin -p -h mydbserver.domain.com

Enter the admin password defined earlier. Then run a simple SHOW DATABASES query to confirm connectivity into MySQL from external clients.

Understanding the MySQL File Layout
Now that MySQL is installed and secured, let's briefly discuss the MySQL server file structure to help manage your database setup.

The main folder locations you should be aware of are:

- **/usr/share/mysql**: Default schema files
- **/etc/mysql**: Configuration files like my.cnf
- **/var/lib/mysql**: Database data and log files   
- **/var/log/mysql**: Error and slow query log files  

Knowing the layout ahead of time helps when troubleshooting MySQL or tuning configuration.

Starting and Stopping the MySQL Server  
A key part of learning MySQL basics is understanding how to start and stop the database server properly.

Use the systemd command to safely stop MySQL:

sudo systemctl stop mysql

Then to start it again:

sudo systemctl start mysql

And restart after config changes:

sudo systemctl restart mysql

Get status with:

sudo systemctl status mysql

Setting MySQL to Start at Boot
To ensure your MySQL server starts automatically on system boot (like after a reboot):

sudo systemctl enable mysql

This sets mysql.service to start on boot. Confirm it worked:

sudo systemctl is-enabled mysql
“`

Which should report back “enabled”.

Next Steps for Learning MySQL

You now have MySQL 8.0 installed locally ready start writing SQL queries, build test apps, import datasets, and otherwise learn MySQL development.

Here are some next steps for leveling up your MySQL skills:

  • Review MySQL shell commands
  • Create sample databases and tables
  • Build user accounts and permissions
  • Import/export data between files and databases
  • Query data using SELECT statements with filters and functions
  • Learn how indexes optimize data retrieval
  • Configure memory, storage engine, and security settings
  • Set up binary log replication between two MySQL instances
  • Explore

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate ยป