How To Connect MySQL/MariaDB Server From Command Line On Linux and Windows? – POFTUT

How To Connect MySQL/MariaDB Server From Command Line On Linux and Windows?

MySQL or MariaDB database servers can be managed in different ways. Generally, GUI tools are provided because of their easiness. But in some cases command line connection may be required to connect and management MySQL and MariaDB database server.

Install MySQL/MariaDB Client For Ubuntu, Debian, Kali, Mint

We will start by installing MySQL/MariaDB command line client in the deb based distributions like Ubuntu, Debian, Kali, Mint etc. The package is named as mysql-client which we will install with the apt command like below.

Install MySQL/MariaDB Client For Ubuntu, Debian, Kali, Mint

Install MySQL/MariaDB Client For Ubuntu, Debian, Kali, Mint

Install MySQL/MariaDB Client For Fedora, CentOS, RedHat

In the rpm based distributions like Fedora, CentOS, RedHat can be installed with the yum command like below. Also alternatively dnf can be used like below.

OR

Install MySQL/MariaDB Client For Fedora, CentOS, RedHat

Install MySQL/MariaDB Client For Fedora, CentOS, RedHat

Connect MySQL/MariaDB Database From Command Line

If MySQL/MariaDB database is installed on the local system we can use mysql tool to connect. We can use mysql command with the sudo command for Linux systems like Ubuntu, Debian, Fedora, Mint, CentOS etc.

OR for Windows

Connect MySQL/MariaDB Database From Command Line

Connect MySQL/MariaDB Database From Command Line

Connect Specifying Username MySQL/MariaDB Database From Command Line

Default behavior of the mysql command is using the current user name. We can provide the user name explicitly with the -u option and the user name. In this example, we will connect with the user name root

Connect Specifying Username MySQL/MariaDB Database From Command Line

Connect Specifying Username MySQL/MariaDB Database From Command Line

Connect Specifying Username and Password MySQL/MariaDB Database From Command Line

Up to now, we have connected to the local MySQL/MariaDB database server without explicitly specifying the password. If we want to connect a remote database server we have to specify the password for the given user. We can also specify the remote server hostname or IP address with the -h option like below.

LEARN MORE  What Is MySQL Database and Product?

Run SQL Without Connecting Remote MySQL/MariaDB Database

mysql command provides different features like running SQL statements remotely without using the mysql shell. We can run the sql clause with the -e option like below.

Run SQL Without Connecting Remote MySQL/MariaDB Database

Run SQL Without Connecting Remote MySQL/MariaDB Database

Show Databases From Command Line

If we want to list databases stored on the MySQL/MariaDB database without connecting them explicitly and using mysql shell we can use show databases; command like below.

 

Run SQL Without Connecting Remote MySQL/MariaDB Database

Run SQL Without Connecting Remote MySQL/MariaDB Database

 

Specify The Default Database From Command Line

As we use command line tool mysql to connect and manage MySQL/MariaDB databases we need to specify the default database name in order to run SQL queries. We will use use statement with the database name in order to specify the default database the SQL queries run.

How To Connect MySQL/MariaDB Server From Command Line On Linux and Windows?

How To Connect MySQL/MariaDB Server From Command Line On Linux and Windows?

Run SQL Script or SQL File From Command Line On Remote MySQL/MariaDB Database Server

SQL scripts and statements can be stored in the SQL script file. We can run this script files without copy and paste just providing the file and redirecting the content of the file. We have the following SQL file content which is sqlscript.sql .

We have to also specify the database name as the last parameter to the mysql command which is sys in this case.

LEARN MORE  How To List MySQL / MariaDB Database?

Run SQL Script or SQL File From Command Line On Remote MySQL/MariaDB Database Server

Run SQL Script or SQL File From Command Line On Remote MySQL/MariaDB Database Server

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)