Friday, March 29, 2024
HomeTutorialsHow to Install MySQL on Ubuntu Linux

How to Install MySQL on Ubuntu Linux

MySQL is considered the most popular database management system in the world. It is an open-source database management system which is usually installed as part of LAMP stack. For managing the data, it uses a relational database and structured query language (SQL). Here I am going to describe the steps of how to install MySQL on an Ubuntu server.

Requirements


Before going to install MySQL on Ubuntu, it is advised to have the latest version of Ubuntu server with the necessary setup. This setup includes a non-root user having sudo privileges and a firewall.

Installation Process


  • MySQL Installation

In the latest versions of Ubuntu, MySQL is available by default in the APT package repository. For a successful installation process, updating the package index on the server is essential with “apt.”

sudo apt update

After that complete the installation of the default package.

sudo apt-get install mysql-server

By this, MySQL will be installed. But, setting password and configuration modification is not allowed due to security purposes.

  • MySQL Configuring

During a fresh installation, if you run the included security script, it will change some default options of less security for the purposes like sample users and remote root logins. In the latest versions of MySQL, the data directory is initialized automatically whereas older versions needed a manual initiation.

sudo mysql_secure_installation

It will drive you through a sequence where you can bring necessary changes to the security options of MySQL installation. The first prompt will inquire that whether you want to set up any password plugin to examine the strength of MySQL password given by you. The second prompt will ask you to set up a password dedicated to the MySQL root user. Confirm the selected password from here.

Then, pressing “Y” and “ENTER” will accept the defaults for all situations. Anonymous users and test database will be removed by this. This will also disable remote root login and activate the new rules changed by you for immediate action by MySQL.

- -

For initializing the MySQL data directory, use “mysql_install_db” command for previous versions of 5.7.6 and for the versions after that use “mysqld –initialize.” If MySQL is installed from the Debian distribution, data directory is initialized by default. No additional action required.

It is to be mentioned that, setting a password for the root MySQL user does not ensure that the user has authentication with a password for connecting to MySQL shell. If required, following step 3 you can adjust this setting.

  • Adjustment of User Authentication and Privileges (Optional)

No password but a default plugin “auth_socket” is used to authenticate the root MySQL user in Ubuntu systems running the latest versions of MySQL (5.7 and later). Though it permits for some greater security and ease of use, in some cases like allowing an external program to access the user, it may create a complicated scenario.

For using a password to as root to MySQL, it is needed to change the authentication method. This change of authentication is form “auth_socket” to “mysql_native_password.” From the terminal, open up MySQL prompt to perform this action.

sudo mysql

After that, with the following command check the user authentication method of your MySQL user account.

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Adjustment of User Authentication and Privileges

In the above example, it is seen that using the “auth_socket” plugin; the root user complete the authentication process. For configuring the root account to authenticate using a password, the following “ALTER USER” command is to run. Select a strong password and mind it that the root password set up in step 2 will be changed with this command.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run the following command to reload the grant tables and putting new changes into effect.

mysql> FLUSH PRIVILEGES;

Check the authentication methods assigned by each user to be assured that root does not authenticate using “auth_socket” plugin.

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

authentication methods assigned by each user

In this example, you can see that the authentication is done by root MySQL user by using a password. Once confirmed on the server, you can exit the shell.

mysql> exit

Now, is the time to test the MySQL installation.

  • Testing the MySQL Installation

No matter how you have installed MySQL, it should have initiated functioning automatically. With the following command, check its status.

sudo systemctl status mysql.service

Testing the MySQL Installation

You can initiate with:

sudo systemctl start mysql

If MySQL does not run. You can try in another way of connecting to the database utilizing “mysqladmin” tool for an additional check. It is a client which allows run the administrative commands.

sudo mysqladmin -p -u root version

It indicates that MySQL is up and running.

Concluding Words


Now, MySQL is installed on your Ubuntu server. For ease of use, you can now modify it. Besides, adding measures is also possible for enhancing security. Is this tutorial on how to install MySQL on Ubuntu helpful? If you like it, please take a moment to share this tutorial on your social network. And don’t forget to leave your suggestion and experience in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now