Connect to MariaDB

Summary: in this tutorial, you will learn how to connect to the MariaDB server using the mysql command-line program.

To connect to MariaDB, you can use any MariaDB client program with the correct parameters such as hostname, user name, password, and database name.

In the following section, you will learn how to connect to a MariaDB Server using the mysql command-line client.

Connecting to the MariaDB server with a username and password

The following command connects to the MariaDB server on the localhost:

mysql -u [username] -p [password]
Code language: SQL (Structured Query Language) (sql)

In this command:

-u specifies the username
-p specifies the password of the username

Note that the password is followed immediately after the -p option.

For example, this command connects to the MariaDB server on the localhost:

mysql -u root -p S@cure1PassCode language: SQL (Structured Query Language) (sql)

In this command, root is the username and S@cure1Pass is the password of the root user account.

Notice that using the password on the command line can be insecure. Typically, you leave out the password from the command as follows:

mysql -u root -pCode language: SQL (Structured Query Language) (sql)

It will prompt for a password. You type the password to connect to the MariaDB server:

Enter password: ********    Code language: SQL (Structured Query Language) (sql)

Once you are connected, you will see a welcome screen with the following command line:

mysql>Code language: SQL (Structured Query Language) (sql)

Now, you can start using any SQL statement. For example, you can show all databases in the current server using the show databases command as follows:

show databases;Code language: SQL (Structured Query Language) (sql)

Here is the output that shows the default databases:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)
Code language: SQL (Structured Query Language) (sql)

Connecting to the MariaDB server on a specific host

To connect to MariaDB on a specific host, you use the -h option:

mysql -u [username] -p [password] -h [hostname]Code language: SQL (Structured Query Language) (sql)

For example, the following command connects to the MariaDB server with IP 172.16.13.5 using the root account:

mysql -u root -p -h 172.16.13.5Code language: SQL (Structured Query Language) (sql)

It will also prompt for a password:

Enter password: ********    Code language: SQL (Structured Query Language) (sql)

Note that the root account must be enabled for remote access in this case.

Connecting to a specific database on the MariaDB server

To connect to a specific database, you specify the database name after all the options:

mysql -u [username] -p[password] -h [hostname] database_nameCode language: SQL (Structured Query Language) (sql)

The following command connects to the information_schema database of the MariaDB server on the localhost:

mysql -u root -p -h localhost information_schema
Enter password: ********Code language: SQL (Structured Query Language) (sql)

The mysql client command-line default parameters

When you type the mysql command with any option, mysql client will accept the default parameters.

mysqlCode language: SQL (Structured Query Language) (sql)

In this case:

  • The hostname is localhost
  • The username is either the login name on Linux or ODBC on Windows
  • No password is sent
  • The client will connect to the server without any particular database.

In this tutorial, you will learn how to connect to the MariaDB server using the mysql command-line client.

Was this tutorial helpful ?