MySQL is a free, open-source, Relational Database Management System used to develop web-based software applications. MySQL is a scalable, intuitive, high-performance, and feature-rich database management system compared to other databases. It is known for its stability and reliability and is considered simple and easy to use.

This post will show you how to install MySQL on Arch Linux.

Step 1 – Configure Repository

By default, the default repository is outdated in Arch Linux, so you will need to modify the default mirror list. You can do it by editing the mirrorlist configuration file:

nano  /etc/pacman.d/mirrorlist

Remove all lines and add the following lines:

## Score: 0.7, United States
Server = http://mirror.us.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.8, United States
Server = http://lug.mtu.edu/archlinux/$repo/os/$arch
Server = http://mirror.nl.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.9, United Kingdom
Server = http://mirror.bytemark.co.uk/archlinux/$repo/os/$arch
## Score: 1.5, United Kingdom
Server = http://mirrors.manchester.m247.com/arch-linux/$repo/os/$arch
Server = http://archlinux.dcc.fc.up.pt/$repo/os/$arch
## Score: 6.6, United States
Server = http://mirror.cs.pitt.edu/archlinux/$repo/os/$arch
## Score: 6.7, United States
Server = http://mirrors.acm.wpi.edu/archlinux/$repo/os/$arch
## Score: 6.8, United States
Server = http://ftp.osuosl.org/pub/archlinux/$repo/os/$arch
## Score: 7.1, India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
## Score: 10.1, United States
Server = http://mirrors.xmission.com/archlinux/$repo/os/$arch

Save and close the file, then update all the package indexes with the following command:

pacman -Syu

Step 2 – Install MySQL Server

The MySQL package is included in the Arch Linux default repository by default. You can install it by running the following command:

pacman -S mysql

Once MySQL is installed, you can verify the MySQL version with the following command:

mysqld --version

You should see the following output:

mysqld  Ver 10.9.3-MariaDB for Linux on x86_64 (Arch Linux)

Next, initialize the MySQL database with the following command:

mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Step 3 – Manage MySQL Service

By default, the MySQL service is managed by systemd. You can control it easily using the systemctl command line utility.

To start the MySQL service, run the following command:

systemctl start mysqld

To enable the MySQL service so it starts at system reboot with the following command:

systemctl enable mysqld

You can verify the active status of MySQL using the following command:

systemctl status mysqld

You will get the following output:

● mariadb.service - MariaDB 10.9.3 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; preset: disabled)
     Active: active (running) since Tue 2022-09-27 04:52:59 UTC; 6h ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 54978 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 8 (limit: 2362)
     Memory: 0B
     CGroup: /system.slice/mariadb.service
             └─54978 /usr/bin/mariadbd

Sep 27 04:52:59 archlinux systemd[1]: Started MariaDB 10.9.3 database server.

Step 4 – Secure the MySQL Installation

By default, the MySQL installation is not secured. You can run the mysql_secure_installation script to secure the MySQL:

mysql_secure_installation

You will be asked to provide the current MySQL root password:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 

Just press the Enter key. You will be asked to switch the Unix socket authentication:

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y

Type Y and press the Enter key. You will be asked to set the root password:

Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Once the root password is set, you will be asked to remove the anonymous user:

Remove anonymous users? [Y/n] Y

Type Y and press the Enter key. You will be asked to disallow remote root login:

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y

Type Y and press the Enter key. You will be asked to remove the test database:

 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y

Type Y and press the Enter key. You will be asked to reload the privilege tables:

 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 5 – Create a User in MySQL

First, log into the MySQL shell with the following command:

mysql -u root -p

Once you log in to MySQL, create a user and set a password:

CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';

Next, grant all privileges on all databases with the following command:

GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'localhost' WITH GRANT OPTION;

Next, flush the privileges and exit from the MySQL shell with the following command:

FLUSH PRIVILEGES;
EXIT;

Conclusion

In this post, we learned how to install the MySQL server on Arch Linux. We also learned how to secure MySQL and create a user. You can now use MySQL as a database backend with your application. Try to deploy and use MySQL on dedicated hosting from Atlantic.Net!