How to Install MySQL

By

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It uses Structured Query Language (SQL) to manage its data.

This guide will show you how to install a basic MySQL setup on your server.

Prerequisites

This guide was used on an Ubuntu 22.04 server. The following steps should also work on other recent Ubuntu and Debian-based systems.

Install MySQL Package

Update the package index:

sudo apt update

Then install the mysql-server package using the APT package repository:

sudo apt install mysql-server

Configure MySQL Security Options

Run the mysql security script to change some of the less secure default options, such as disallowing remote root logins and removing sample users:

sudo mysql_secure_installation

The script will take you through a series of prompts where you can make some changes to your MySQL security options.

Create MySQL User and Grant Privileges

MySQL creates a root user account which can be used to manage the database. This user has full privileges over the MySQL server, so it’s best to avoid using this account outside of administrative purposes. This step will create a new user account and grant it privileges.

First, access the mysql prompt with the following command:

mysql -u root -p

Next, create a new user with a CREATE USER statement:

mysql> CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password';

Grant the appropriate privileges:

mysql> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'user_name'@'localhost';

Run the FLUSH PRIVILEGES command to free up cached memory:

mysql> FLUSH PRIVILEGES;

You can now exit the MySQL client:

mysql> exit

In the future, to log in as your new MySQL user, use the following command and enter the password for the account when prompted:

mysql -u user_name -p

Conclusion

You now have a basic MySQL setup and can continue with setting up the rest of the LAMP stack.


Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.