How to Set Up MySQL

MySQL is a popular open-source relational database management system, that is widely used for web applications. It is easy to install and configure on various platforms, including Linux, Windows, and macOS. In this article, we will discuss how to set up MySQL on a Linux operating system.

Step 1: Installing MySQL

The first step is to install MySQL. The latest version of MySQL is available in the default repositories of most Linux distributions. To install MySQL on Ubuntu or Debian-based systems, run the following command:

Sudo apt-get install mysql-server

For CentOS or Fedora-based systems, run the following command:

Sudo yum install mysql-server

This command will install the MySQL server and client packages. After the installation is complete, start the MySQL service using the following command:

Sudo systemctl start mysqld

Step 2: Securing MySQL

By default, MySQL is not secured, and anyone can connect to the server and access the databases. To secure MySQL, we need to run the following command:

Sudo mysql_secure_installation

This command will guide us through the process of securing MySQL. It will prompt us to set a root password, remove anonymous users, disallow remote root login, and remove the test database.

Step 3: Creating a MySQL User

After securing MySQL, we need to create a MySQL user that can access the databases. To create a new MySQL user, we need to log in to the MySQL shell with the root user:

Mysql -u root -p

Enter the root password that you set during the MySQL secure installation process. Once logged in, run the following command to create a new user:

CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;

Replace ‘username’ and ‘password’ with your desired username and password. This will create a new MySQL user with the username and password you specified.

Step 4: Creating a MySQL Database

After creating a MySQL user, we need to create a MySQL database. To create a new MySQL database, run the following command:

CREATE DATABASE database_name;

Replace ‘database_name’ with your desired database name. This will create a new MySQL database with the name you specified.

Step 5: Granting Permissions to the User

After creating a MySQL user and database, we need to grant permissions to the user to access the database. To grant permissions to the user, run the following command:

GRANT ALL PRIVILEGES ON database_name.* TO ‘username’@’localhost’;

Replace ‘database_name’ and ‘username’ with your desired database name and username. This will grant all privileges to the user to access the specified database.

Step 6: Testing the MySQL Connection

After setting up MySQL, we need to test the MySQL connection to ensure that everything is working correctly. To test the MySQL connection, run the following command:

Mysql -u username -p database_name

Enter the username and password that you created earlier, followed by the database name. If the connection is successful, you will see the MySQL prompt, where you can run MySQL commands.

In this article, we discussed how to set up MySQL on a Linux operating system. We covered the installation process, securing MySQL, creating a MySQL user, creating a MySQL database, granting permissions to the user, and testing the MySQL connection. By following these steps, you can set up MySQL on your Linux system and start using it for your web applications.