En este tutorial veremos algunos comandos básicos desde la consola y si ya se que se preguntan por que conectarnos por la consola de comandos en lugar de un GUI como puede ser Workbench HeidiSQL etc, es muy necesario sobre todo si eres programador ya que solo se usan comandos y créeme no es que vallas a usar la consola todo el tiempo pero si aprender los comandos en lugar de crear las tablas desde GUI se te va a dificultar mas, de hecho conforme pasa el tiempo y van creciendo las tablas en lo referente a columnas por la misma experiencia se les hará mas fácil manejarse con comandos.
Bien para conectarnos nos posicionamos en el directorio de instalación de mariaDB que es C:\Program Files\MariaDB 10.6\bin y para conectarnos usaremos el siguiente comando
mariadb -u root -p
Nos pedirá ingresar la contraseña del root la cual asignamos cuando hicimos la instalación, se la ponemos y nos arrojara la siguiente salida
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.6.7-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Nos da el mensaje de bienvenida, nos dice como debemos rematar una ; o \g para ejecutar la instrucción, es el equivalente para un sinaloense rematar una frase con fierro pariente o jalados, nos da también el id de conexión que en este caso es 8, nos muestra la versión del server y todo lo que ven allí,
Para ver los comandos disponibles solo tecleamos \h y nos saldrá lo siguiente
General information about MariaDB can be found at
MariaDB Foundation
List of all client commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to MariaDB server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to MariaDB server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
For server side help, type 'help contents'
Y bien ya empezamos lo primero es las base de datos existentes para ello solo usamos el comando show databases y nos mostrara lo siguiente
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.001 sec)
Para posicionarnos en una base de datos y ver que tablas, procedimientos, vistas y triggers tiene por que eso es lo que contienen las bases de datos, vamos usar el comando use “database“, como ejemplo nos meteremos a a la base de datos mysql entonces escribimos use mysql; nos arrojara el siguiente resultado
MariaDB [(none)]> use mysql
Database changed
MariaDB [mysql]>
Bien ahora para ver que tablas tienes esa base de datos escribimos show tables; y nos arrojara el siguiente resultado
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| column_stats |
| columns_priv |
| db |
| event |
| func |
| general_log |
| global_priv |
| gtid_slave_pos |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| index_stats |
| innodb_index_stats |
| innodb_table_stats |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| roles_mapping |
| servers |
| slow_log |
| table_stats |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| transaction_registry |
| user |
+---------------------------+
31 rows in set (0.001 sec)
Bien ahora veremos que columnas y de que tipo es la tabla “user” para ello escribimos describe user; y dará el siguiente resultado
MariaDB [mysql]> describe user
-> ;
+------------------------+---------------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------------------+------+-----+----------+-------+
| Host | char(255) | NO | | | |
| User | char(128) | NO | | | |
| Password | longtext | YES | | NULL | |
| Select_priv | varchar(1) | YES | | NULL | |
| Insert_priv | varchar(1) | YES | | NULL | |
| Update_priv | varchar(1) | YES | | NULL | |
| Delete_priv | varchar(1) | YES | | NULL | |
| Create_priv | varchar(1) | YES | | NULL | |
| Drop_priv | varchar(1) | YES | | NULL | |
| Reload_priv | varchar(1) | YES | | NULL | |
| Shutdown_priv | varchar(1) | YES | | NULL | |
| Process_priv | varchar(1) | YES | | NULL | |
| File_priv | varchar(1) | YES | | NULL | |
| Grant_priv | varchar(1) | YES | | NULL | |
| References_priv | varchar(1) | YES | | NULL | |
| Index_priv | varchar(1) | YES | | NULL | |
| Alter_priv | varchar(1) | YES | | NULL | |
| Show_db_priv | varchar(1) | YES | | NULL | |
| Super_priv | varchar(1) | YES | | NULL | |
| Create_tmp_table_priv | varchar(1) | YES | | NULL | |
| Lock_tables_priv | varchar(1) | YES | | NULL | |
| Execute_priv | varchar(1) | YES | | NULL | |
| Repl_slave_priv | varchar(1) | YES | | NULL | |
| Repl_client_priv | varchar(1) | YES | | NULL | |
| Create_view_priv | varchar(1) | YES | | NULL | |
| Show_view_priv | varchar(1) | YES | | NULL | |
| Create_routine_priv | varchar(1) | YES | | NULL | |
| Alter_routine_priv | varchar(1) | YES | | NULL | |
| Create_user_priv | varchar(1) | YES | | NULL | |
| Event_priv | varchar(1) | YES | | NULL | |
| Trigger_priv | varchar(1) | YES | | NULL | |
| Create_tablespace_priv | varchar(1) | YES | | NULL | |
| Delete_history_priv | varchar(1) | YES | | NULL | |
| ssl_type | varchar(9) | YES | | NULL | |
| ssl_cipher | longtext | NO | | | |
| x509_issuer | longtext | NO | | | |
| x509_subject | longtext | NO | | | |
| max_questions | bigint(20) unsigned | NO | | 0 | |
| max_updates | bigint(20) unsigned | NO | | 0 | |
| max_connections | bigint(20) unsigned | NO | | 0 | |
| max_user_connections | bigint(21) | NO | | 0 | |
| plugin | longtext | NO | | | |
| authentication_string | longtext | NO | | | |
| password_expired | varchar(1) | NO | | | |
| is_role | varchar(1) | YES | | NULL | |
| default_role | longtext | NO | | | |
| max_statement_time | decimal(12,6) | NO | | 0.000000 | |
+------------------------+---------------------+------+-----+----------+-------+
47 rows in set (0.022 sec)
Nos arroja todas las columnas que tiene esa tabla y el tipo de dato, si es, primaria, índice valores por default etc
Ahora para ver que información tiene esa tabla usamos el comando select [columnas] from tabla, si queremos ver todas las columnas usamos * por ejemplo vamos a ver la información de user solo escribimos select * from user; y dara el siguiente resultado
MariaDB [mysql]> select * from user;
+-----------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+---------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+---------+--------------+--------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | Delete_history_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | is_role | default_role | max_statement_time |
+-----------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+---------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+---------+--------------+--------------------+
| localhost | mariadb.sys | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 | mysql_native_password | | Y | N | | 0.000000 |
| localhost | root | *8721276FB8AF50582299864B6C43E27301DACA58 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *8721276FB8AF50582299864B6C43E27301DACA58 | N | N | | 0.000000 |
| desktop-8pu5o44 | root | *8721276FB8AF50582299864B6C43E27301DACA58 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *8721276FB8AF50582299864B6C43E27301DACA58 | N | N | | 0.000000 |
| 127.0.0.1 | root | *8721276FB8AF50582299864B6C43E27301DACA58 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *8721276FB8AF50582299864B6C43E27301DACA58 | N | N | | 0.000000 |
| ::1 | root | *8721276FB8AF50582299864B6C43E27301DACA58 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *8721276FB8AF50582299864B6C43E27301DACA58 | N | N | | 0.000000 |
+-----------------+-------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+---------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+---------+--------------+--------------------+
5 rows in set (0.002 sec)
Si ya se que tremendo cochinero que trajo, pero para traerlo mas ordenado traermos solamente 3 columnas para escribimos lo siguiente select host, user from user; nos dara el siguiente resultado.
MariaDB [mysql]> select host, user from user;
+-----------------+-------------+
| Host | User |
+-----------------+-------------+
| 127.0.0.1 | root |
| ::1 | root |
| desktop-8pu5o44 | root |
| localhost | mariadb.sys |
| localhost | root |
+-----------------+-------------+
5 rows in set (0.001 sec)
ahora si queremos filtrar usaremos el where por ejemplo, solo se quiere que nos regrese el usuario Mariadb.sys entonces corremos el siguiente comando select host, user from user where user = ‘mariadb.sys’; nos arrojara el siguiente resultado
MariaDB [mysql]> select host, user from user where user = 'mariadb.sys'
-> ;
+-----------+-------------+
| Host | User |
+-----------+-------------+
| localhost | mariadb.sys |
+-----------+-------------+
1 row in set (0.005 sec)
1 pingback