Linux Connect to MySQL

Are you curious to learn how to connect to MySQL with Linux? You are at the right stop! This blog unveils the step-by-step procedure to connect to MySQL from Linux systems. You will also learn how to install MySQL in Linux systems and connect to MySQL using SSH, PHP, and command line tools. Ultimately, you will gain good hands-on experience in connecting to MySQL with Linux by going through the step-by-step procedure given in this blog.

Know that MySQL is the most used relational database next to Oracle worldwide. It is a platform-independent database so that you can run it anywhere – whether it is Linux, Windows, or macOS. MySQL is an open-source database that is user-friendly and customizable. Because of these reasons, MySQL is highly preferred by developers and companies everywhere.

As many companies increasingly use MySQL, learning MySQL is the smart way for you to get hired by companies quickly. No wonder the companies like Yahoo, Alcatel, Suzuki, and NASA depend on MySQL for their mission-critical applications.

The important thing is that you must be proficient with managing MySQL to grasp the job opportunities quickly. For example, you must be familiar with connecting to MySQL using various tools and methods.

Okay! To help you out, MindMajix content experts elaborate on connecting MySQL with Linux in this blog. Here, we discuss the various procedures to connect to MySQL from Linux systems. Once you go through the step-by-step procedures in this blog, you will gain hands-on experience connecting to MySQL from Linux systems.

Let’s dive in!

Table of Contents

What is Linux?

We use Linux as an open-source operating system to interact with system hardware directly. The hardware can be memory, CPU, or storage. Linux is one of the widely used open-source operating systems across the world. The Linux operating system generally consists of GNU tools, Kernel, and other software packages.

The important thing is that developers continuously update Linux with additional features, security settings, and many more. This is the reason why Linux is being used everywhere. For example, we use Linux in smartphones, cars, supercomputers, enterprise servers, home appliances, etc. Well! We will also take a glance at command line clients now.

Command line clients are closely associated with Linux. They help to interact with computers directly, that GUIs cannot do. We can use command line clients to query servers, upload files, etc. The great thing about the command line is that it works quicker and consumes less memory.

If you want to enrich your career and become a professional in Linux, then visit Mindmajix - a global online training platform: "Linux Certification Training" This course will help you to achieve excellence in this domain.

What is MySQL?

MySQL is one of the famous open-source databases. It is a relational database management system that stores data in tables in a structured way. It simplifies storing and accessing data. Mainly, MySQL supports popular programming languages such as Python, Ruby, Java, C++, Go, etc. Besides, it supports drivers like ODBC.

Know that big companies such as Facebook, Uber, Twitter, Netflix, etc., use MySQL to manage their data. This is because MySQL is reliable, faster, and easy to use. Also, MySQL supports both client/server mode and embedded systems seamlessly.

MindMajix Youtube ChannelHow to install MySQL on Linux?

In this section, we will go through the steps to install MySQL on Linux systems using the Yum repository.

Let’s get to the steps right away!    

Prerequisites:

  • A user account with sudo privileges.

Step-by-step Procedure:

  • First, add the MySQL yum repository to your repository list. You can use the below command for the same.

sudo rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm

  • Before installing MySQL, disable all the repositories in your repo file. The below command will help you to do so.
sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo
  • Next, install MySQL in your Linux system using the following command.
sudo yum --enablerepo=mysql80-community install mysql-community-server
  • Now, you can start managing MySQL using the below command.
service mysqld start
  • You can use the below command to get the temporary password for the root user. This password is essential to install MySQL.
sudo grep "A temporary password" /var/log/mysqld.log
  • Then, use the below command to secure your MySQL server.
mysql_secure_installation
  • After that, you need to enter the temporary password. You can change your password by entering a new one. Next, you must enter 'Yes' for the questions on the screen.
  • Now, restart mysql using the below command.
service mysqld restart
  • Use the below command to connect to MySQL.
MySQL -u root –p

Alternatively, you can install MySQL on your computer using the link below.

  • Use the following link to install MySQL on your computer.
  • Set a password and complete the installation to access the MySQL database.

Related Article: What is Linux? - A Complete Beginners Tutorial

Connecting to MySQL with Linux using Secure Shell (SSH)

Generally, we use the MySQL program to connect with the MySQL database.

Let’s look at the step-by-step procedure to help you access MySQL through Secure Shell or SSH.

  • First, log into your Linux web server through a secure shell.
  • Then, open the mysql client using the bin directory.
  • Type the below command in the command line prompt. Enter the username in the command.
MySQL - h {hostname} -u username - p{database name}
  • A prompt will appear immediately to enter the password.
  • Once you have entered the correct password, the mysql> prompt will appear on the screen.
  • If you want to view the list of databases, then type the below command at the prompt.
show databases;
  • Suppose you want to access a particular database. Then you must type the command below in the prompt and the database name.
use dbname;
  • Once the database is accessed, then you can run SQL queries and list tables seamlessly.
  • If you want to view the list of MySQL commands, type ‘help’ in the prompt and hit the enter button. As a result, you will get a list of all the MySQL commands.
  • If you want to exit the mysql program, then type the below command in the prompt.
\q

Connecting to MySQL using command line client

This section will go through the step-by-step procedure to connect to MySQL with the help of the command line client.

Let’s see them one by one.

  • First, select the MySQL command line client using the start menu from your computer.

linux connect to mysql step1

  • When you open the command line client, it will open a command prompt. This is where you need to enter the password that you set while installing MySQL on your computer.

linux connect to mysql step2

  • Once you have entered the password, you will get the result as shown below.

linux connect to mysql step3

  • Enter the below-shown command in the command prompt to list the databases of the MySQL server.
show databases; 

linu connect to mysql step4

  • If you want to create a database, use the command below.
create database database name;

linux connect to mysql step5

  • If you want to connect to a specific database, use the command below.
use database database name;

linux connect to mysql step6

  • You can use the below commands if you want to create a table and insert data into the table.
create table table name (….);
insert into table name (….);

linux connect to mysql step7

Connecting to MySQL server with PHP on Linux system

You can connect to MySQL using PHP codes on a Linux system. The below PHP codes will help you to connect with the MySQL server.

Connecting to MySQL using the MySQL Workbench

Below is the procedure to help you connect with MySQL database with MySQL workbench.

Let’s go through the steps.

  • Run MySQL workbench using your start menu
  • Navigate to the database tab and then click ‘connect to database’ from the list.

linux connect to mysql step8

  • A window will open. It will specify the connection name, port, hostname, and user values.

linux connect to mysql step9

  • Then, configure the SSL connection settings on the SSL tab.

linux connect to mysql step10

  • Also, you can configure advanced settings on the advanced menu.

linux connect to mysql step11

  • Now, click OK to ensure all the entered credentials are correct.

linux connect to mysql step12

  • Enter the password to access. Once you get access, you will view the list of databases on the left side of the window. 

FAQs

1. How can you secure MySQL in Linux systems?

You can adopt the following methods to secure MySQL in Linux systems.

  • Setting strong passwords for your root accounts.
  • Not allowing anyone to access root accounts outside of the local host
  • Deleting the test databases once their usage is over.

2. What is the key prerequisite to installing MySQL on Linux?

A user account with sudo privileges is required to install MySQL on Linux systems.

3. How will you configure MySQL in Linux systems?

By setting up a ‘Yum’ repository, you can configure MySQL in Linux.

4. Mention some advantages of using the Linux operating system.

Jotted down are a few advantages of the Linux operating system.

  • High performance
  • Security
  • Multiple distributions
  • Flexibility

5. How will you find the MySQL version in Linux?

You can find the version by running the below command in the command line client.

mysql –V

6. What about the case sensitivity of MySQL in Linux?

MySQL is case-sensitive in Linux systems. However, you can change this feature with the help of collation.

7. How to delete MySQL from your Linux system?

You must execute the below steps to do so.

  • First, stop MySQL service
  • Uninstall MySQL packages
  • Remove MySQL configuration as well as data files
  • Verify the removal

Summary

  • Mysql is an open-source and relational database widely used by developers.
  • MySQL supports all popular programming languages and drivers.
  • Linux is an open-source, easy-to-use operating system with high flexibility and security.
  • The command line client is used to connect with MySQL
  • We can connect with MySQL using MySQL workbench, SSH, and PHP.

Conclusion

At a glance, we hope that this blog might have given you a basic idea about Linux and MySQL. Also, you have gone through step-by-step procedures to connect MySQL with Linux systems using PHP, SSH, and MySQL workbench. Of course! You might have better understood connecting to MySQL through command-line clients.

If you want to learn more about MySQL and Linux, you can register for courses at MindMajix and gain certification. It will help you to get sound exposure to MySQL databases and Linux systems and establish a colorful career in MySQL.

Course Schedule
NameDates
Linux TrainingOct 15 to Oct 30View Details
Linux TrainingOct 19 to Nov 03View Details
Linux TrainingOct 22 to Nov 06View Details
Linux TrainingOct 26 to Nov 10View Details
Last updated: 14 Dec 2023
About Author

As a Senior Writer for Mindmajix, Saikumar has a great understanding of today’s data-driven environment, which includes key aspects such as Business Intelligence and data management. He manages the task of creating great content in the areas of Programming, Microsoft Power BI, Tableau, Oracle BI, Cognos, and Alteryx. Connect with him on LinkedIn and Twitter.

read less