In this guide, we will show you how to install MySQL on AlmaLinux. There are two separate packages for this on AlmaLinux, depending on what you need to do. You can either install the MySQL client package, which is used to connect to MySQL servers, or install the MySQL server software, which can be used to host your own databases. We'll cover both below.

After installing a MySQL server, we'll also go over the first steps you'll need to get started with hosting a database, which includes making a username and password, creating a database, and granting user permissions on that database. AlmaLinux is a good Linux distro for hosting MySQL, whether you've freshly installed AlmaLinux or migrated from CentOS to AlmaLinux.

In this tutorial you will learn:
  • Install MySQL Client on AlmaLinux
  • Install MySQL Server on AlmaLinux
  • How to setup a MySQL database
Setting up a MySQL database and user on AlmaLinux
Setting up a MySQL database and user on AlmaLinux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System AlmaLinux
Software MySQL Client, MySQL Server
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Install MySQL Client on AlmaLinux

Installing the MySQL client on your AlmaLinux system will enable you to use the mysql shell command. With that command, you'll be able to log in to remote MySQL servers, but you won't actually be running MySQL on your local system.

  1. To get started installing it, open a terminal and type the following command:
    # dnf install mysql
    
  2. You can confirm that the MySQL client was installed successfully and see what version your system is running with this command:
    # mysql -V
    mysql  Ver 8.0.21 for Linux on x86_64 (Source distribution)
    
  3. Now you are able to connect to a remote MySQL server by using the following command syntax. You'll be prompted to enter your password when connecting.
    # mysql -u USERNAME -p -h HOST-OR-SERVER-IP
    

SUBSCRIBE NEWSLETTER & RSS
Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Install MySQL Server on AlmaLinux

If you want to host a MySQL database (or multiple) on your AlmaLinux system, then you'll need to install the MySQL Server package. You'll be able to access the database from the local machine, or from remote clients that are using the MySQL Client to connect.

  1. To install MySQL Server, execute the following command in terminal:
    # dnf install mysql-server
    
  2. After it's installed, use the following systemctl commands to start the MySQL server, and optionally enable the service to start automatically at system boot.
    # systemctl start mysqld
    # systemctl enable mysqld
    
  3. The first thing you'll want to configure after installing your MySQL server is secure it via the following command:
    # mysql_secure_installation
    
    You'll be prompted with a few setup questions now. How you answer them will depend on the environment in which you're deploying this MySQL server. If in doubt, select the most secure settings and choose a strong root password. It's also best practice to disable remote root logins when asked.
  4. Going through the mysql secure installation prompts
    Going through the mysql secure installation prompts
  5. By default, MySQL listens for connections on port 3306. You can confirm that your MySQL service is listening for incoming connections on all interfaces with this command:
    # ss -ltn
    
  6. We can see that MySQL is listening for remote connections on port 3306
    We can see that MySQL is listening for remote connections on port 3306
  7. Finally, the only thing left to do is add an exception in the system firewall. This is only applicable if you have firewalld enabled on AlmaLinux, which it is by default. You can issue the following command to open port 3306 in firewalld, allowing incoming MySQL connections.
    # firewall-cmd --zone=public --add-service=mysql --permanent
    # firewall-cmd --reload
    


How to setup a MySQL database

If you've been following along with us so far, you should have a fully functional MySQL server installed on AlmaLinux.

To get started utilizing your new MySQL server installation, you'll need to create a database. MySQL databases require user permissions (unless you just use the root account to access your database, which isn't recommended) in order to store data. We'll get you going with the basics of a database setup in the following steps.

For this example, we're going to show the steps to setup a new database and user account, allow the account to be logged in from any remote host, and grant that user full permissions (the ability to create, insert, drop, etc) on our database.

  1. First, login to your MySQL server with the root account.
    # mysql -u root -p
    
  2. To get much further, you'll inevitably have to learn some MySQL commands and syntax, but we've got you covered with the bare basics to help you start. Create a new database like so (replacing my_database with the desired name of your database):
    mysql> CREATE DATABASE my_database;
    
  3. Next, we need to create a new user account that will have privileges to the database we just created. The syntax for doing this is as follows:
    mysql> CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password';
    


    The above command will create a new user named my_user with a password of my_password. This user will be able to connect from anywhere on the internet, as we've specified a wildcard % in the command. If we wanted to restrict where it can connect from, we could specify an IP address instead, or allow only local connections with localhost.
  4. Next, we need to grant this new user some permissions on our database. We will give the user all permissions on our database with the following command:
    mysql> GRANT ALL PRIVILEGES ON my_database.* to my_user@'%';
    
  5. Lastly, save all the changes with this command, and then use the exit command to close the MySQL terminal.
    mysql> FLUSH PRIVILEGES;
    mysql> exit
    
Setting up a database and user account in MySQL
Setting up a database and user account in MySQL

You now have a usable MySQL database and a user account that can connect to and edit it. Using the credentials you've created, remote systems can login to your MySQL server to store data in the database, as well as retrieve it.

Closing Thoughts

In this guide, we saw how to install MySQL Client and MySQL Server on AlmaLinux. Whether you need to connect to a remote database or host a MySQL database yourself, following the steps we've outlined should do the job.

We also learned the basics of logging into MySQL and creating a new database and user, and then granting privileges to that user. This is all you need to get started storing and retrieving data in your database from local and remote hosts.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.