Accessing MariaDB or MySQL from the terminal is one of the fundamental tasks when managing databases.
The mysql terminal tool is very powerful and from my point of view much more reliable than any Python or PHP script.
Normally we manage the servers from an SSH connection, so we will access the terminal from
or another similar client. The tutorial is valid for
MariaDB and MySQL
, but to avoid repeating both names continuously we will use one or the other indifferently.
Basic commands to access MariaDB or MySQL from the terminal.
For the examples I will use the following data as a user and password for MariaDB or MySQL:
First we open a session in the terminal and execute the command:
mysql -u root -p
This is the most elementary command and after executing it, it will ask us to enter the password. We can also indicate the password directly indicating it just after the
option and without leaving spaces, as we see in the following command:
mysql -u root -ptGaHdEd
So far we have not specified any host and if we are going to use localhost it should not be necessary, but in case you need this option at some point, the command would be as follows:
mysql -u root -p -h localhost
Problems with the special characters in the user or password when connecting to MySQL or MariaDB.
When we have some special character in the username or password, we will have to type it in single quotes, for example:
mysql -u 'r@@t' -p't@Ga#HdEd'
Once this is done, we will already be inside the mysql client with which to launch queries or queries. As a test, you can launch the
which will show you all the databases that exist in the system.
To exit the mysql console, we have to execute the
. It should be noted that all our queries end in a semicolon
so that the syntax is correct.
Access MySQL or MariaDB indicating the database we want to use.
When we connect to the MySQL server we can also specify the database that we want to use at the time of starting the connection. For this, the command would be as follows:
mysql -u root -D nombre_bd -p
We see that we have used the
option to specify the name of the database that we will use, which in our case is called