How to Install MySQL Community Server

MySQL is one of the most popular open-source relational database management systems (RDBMS). It is widely used for web application development and is a central component of the LAMP (Linux, Apache, MySQL, PHP) stack.

In this article, we will guide you step by step on how to install MySQL Community Server on a Linux machine.

Step 1: Check for Existing MySQL Installation

Before installing MySQL, we need to check if it’s already installed on our system. Open the terminal and type the following command:

“`

$ mysql –version

“`

If MySQL is already installed, the command will display the version number. If not, it will show an error message.

Step 2: Add MySQL Repository

To install the latest version of MySQL, we need to add the MySQL repository to our system. To do this, follow the instructions below:

1. Download the MySQL repository configuration package:

“`

$ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

“`

Note: The above command is for CentOS 7. If you are using a different version of Linux, you can find the appropriate download link on the MySQL website.

2. Install the repository package:

“`

$ sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm

“`

Step 3: Install MySQL Community Server

Once the repository is added, we can install MySQL Community Server by running the following command:

“`

$ sudo yum install mysql-community-server

“`

This command will download and install MySQL Community Server on our system.

Step 4: Start MySQL Service

After installing MySQL, we need to start the MySQL service to make it available for use. To start the service, run the following command:

“`

$ sudo systemctl start mysqld

“`

Step 5: Secure MySQL Installation

By default, MySQL installation is not secure. We can secure it by running the following command:

“`

$ sudo mysql_secure_installation

“`

This command will ask a series of questions to secure the MySQL installation. Follow the instructions below to answer the questions:

1. Enter the root password for MySQL. If you haven’t set up a root password yet, press Enter.

2. Set a root password for MySQL.

3. Remove anonymous users.

4. Disallow root login remotely.

5. Remove test database and access to it.

6. Reload privilege tables.

Step 6: Connect to MySQL

To connect to MySQL, run the following command:

“`

$ mysql -u root -p

“`

This command will prompt you to enter the root password for MySQL. Once you enter the password, you will be connected to the MySQL command-line interface.

Step 7: Create a MySQL User

By default, MySQL comes with a root user that has full privileges. It’s not recommended to use the root user for all tasks. Instead, we should create a separate user with limited privileges.

To create a new MySQL user, follow the instructions below:

1. Log in to MySQL with the root user:

“`

$ mysql -u root -p

“`

2. Create a new user:

“`

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

“`

Replace ‘username’ and ‘password’ with the desired username and password for the new user.

3. Grant privileges to the new user:

“`

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

“`

Replace ‘username’ with the name of the new user.

4. Reload the privilege tables:

“`

mysql> FLUSH PRIVILEGES;

“`

Step 8: Exit MySQL

To exit MySQL, type the following command:

“`

mysql> exit

“`

This will exit the MySQL command-line interface.

In this article, we have covered the step-by-step process of installing MySQL Community Server on a Linux machine. We have also shown how to secure the installation, connect to MySQL, and create a new user. With this knowledge, you can easily set up and manage your own MySQL database server for your web applications.