How to connect to MySQL from the console?
To connect to MySQL from the console, we must know that there is a wide variety of ways to do it. From the use of specific drivers for each language, through ORMs (Object Relationship Models), to graphical interfaces such as MySQL Workbench or PHPMyAdmin. However, the connection through the terminal is a remarkable option for its simplicity and efficiency. The console is not only powerful, but also allows us to interact directly with the database manager without distractions.
What are the basic steps to connect?
Here is a practical approach:
- Start MySQL from the terminal:
mysql -u root -h 127.0.0.1 -p
-u
specifies the user, in this case root
.
-h
defines the host, which is usually localhost
or 127.0.0.1.
-p
indicates that the password will be requested, instead of passing it openly in the command for more security.
- Enter the secure password: Specify the password when the prompt asks for it. Never send it in the same command for security reasons.
What will you see when you log in?
Once logged in, you will be in the MySQL console, which is indicated at the prompt. Here you can execute various SQL commands to interact directly with the databases.
How to manage the databases from the console?
The console provides us with powerful commands to manage databases efficiently.
How to check existing databases?
Use the following command to list the databases you have access to:
SHOW DATABASES;
This command will list all databases on the server that the current user has permission to access. For example, information_schema
is a key database that contains meta information about other databases.
How to select a database?
To work with a specific database, use the USE
command followed by the name of the desired database:
USE database_name;
For example:
USE TMP;
This sets the TMP
database as the active one and you will see the Database changed
message in the console.
How to list the tables of a database?
Once the database is selected, you can list the tables it contains with:
SHOW TABLES;
This command provides a list of tables within the active database. If, for example, you find a table named temperatures
, it means that this table is available in the database traversed.
How to check the currently selected database?
If you need to remember which database you are currently working in, use:
SELECT DATABASE();
Remember that DATABASE()
is a function, so parentheses are required. This will display the name of the active database.
Why choose the console over graphical interfaces?
Opting to use the console instead of graphical interfaces has several advantages, especially for those seeking more control:
- Speed: The console offers faster response times as it eliminates visual intermediaries.
- Flexibility: You can execute any command compatible with your permissions without depending on whether a specific GUI supports certain functionality.
- Efficient development: Understanding the underlying commands used by GUIs gives you deep understanding and automation possibilities.
With this basic knowledge, the console becomes a powerful ally in database management, allowing a degree of control and efficiency that will undoubtedly complement any graphical interface you decide to use. Don't be afraid of the terminal, it will be your best friend in this data management journey!
Want to see more contributions, questions and answers from the community?