Access MySQL from Terminal on Linux

Access and most basic operations to manage MySQL on a Linux system when accessed through the command line terminal.

At .com we published some very interesting notes some time ago for anyone who works with MySQL and wants to . That article was oriented towards people who work with Windows, although it is true that most of the administration statements work the same in Linux, because ultimately what is being done is to communicate with MySQL through the SQL language, which does not differ at all. from one system to another.

However, for people who work with MySQL on GNU/Linux, whether they have Linux installed on their desktop or SSH into dedicated servers, some issues related to accessing the MySQL interface change a bit. In this article we are going to comment on some aspects that may be of interest to those new users who are looking for some basic orientations.

We will start by giving some details on how to access the Linux system command line, either on your local system, or on a remote Linux system that you may have on a dedicated server.

Access to a server by SSH

As a first step we are going to discuss how to make a connection via SSH to a dedicated server, or a virtual server or hosting, that allows the connection via SSH. This is not specifically related to MySQL administration, but it can be used as a guide for people who are wanting to connect to a remote server and then access that database management system.

In this case, you may have several different operating systems and you want to access the Linux server through any of them.

See also  What is the difference between exit and die in PHP?

Windows:

To SSH into a remote server from your Windows desktop I usually use Putty, which is super lightweight and free. You can see information about this software in the article

linux:

The Putty program is also available for Linux, but it is completely unnecessary, since through the terminal of the Linux system itself you have access to the SSH command to connect to any server.

ssh user@1.2.3.4

Of course, you will have to edit your username and the IP of the server you want to connect to. For example, to root the server with IP 280.230.2.67 you would write:

ssh root@280.230.2.67

Mac:

Computers with Mac OS X system have the command line terminal also integrated among the available applications. To access another server via SSH, use the same command as in Linux.

Access to a local MySQL database on Linux systems

If in your case you have Linux installed on your desktop computer and you want to access MySQL by command line, opening a Terminal is enough. That is, here you would not need to connect via SSH to any server.

Note: On my Ubuntu system I open a terminal with the CTRL + ALT + T key combination. I suppose on other Linux distributions this shortcut may change, but it shouldn’t be too difficult to open a terminal. Search for the program among those installed.

Access to the MySQL command line interface

Once on the server where you have the MySQL you want to access, you simply connect with the command:

mysql -h localhost -u root -p

You will have to indicate the MySQL user with which you want to authenticate. You are probably root on your local MySQL installation, but if you are on a production server it might be smarter to connect with the username and password to the MySQL database you want to manage, which are the same ones you use in, for For example, the PHP function mysql_connect() .

See also  How to Conduct Market Analysis for Your Product

Note: The MySQL server to which we connect is localhost, since it is assumed that you are accessing the management system that you have on the server with which you have connected. But you could also access other remote servers by changing localhost to the name or IP of that server you want to access.

In Linux it doesn’t matter in the directory where you are when you execute that command to connect with MySQL, since the command is installed on the server and active from any location.

Once this command is executed, we will be asked for the password of the user that we have used to connect. If we put it right, we will be inside the MySQL command line interface.

If we ever want to exit back to the Linux terminal, we use the command

exit

Typical MySQL commands

Once inside MySQL you can execute commands to work with the database management system in the . The most typical thing is that you perform actions like these:

See the databases that we have created in the system:

show databases;

Note: note that the commands you issue to MySQL must contain a semicolon “;” at the end, if you do not put it, the command will not be executed until you write the “;” and press the Enter key again.

Switch to a database:

use_database;

Where database_de_database is the name of the database to use.

From here you can perform any type of selection, update, insertion, etc. queries. on the database. For this you use the standard query language of which we have an interesting .

See also  General characteristics of Google Maps and Yahoo Maps

To complement this information, you can read the previous article on , where we explained another series of actions available when we attack MySQL from the terminal.

Loading Facebook Comments ...
Loading Disqus Comments ...