Tuesday 2 July 2024

mysql commands to learn basic sql commands on terminal

 

On your mac terminal to open your mysql use the below command

firstname.lastname@MRMAcNAME ~ % mysql -u root -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 1277

Server version: 10.4.33-MariaDB Homebrew

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)]> 

=======================================================

To list databases

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| database1   |

| master       |

| information_schema |          |

| mysql              |            |

+--------------------+

7 rows in set (0.004 sec)

MariaDB [(none)]> 

============================================

To create a new database

MariaDB [(none)]> create database testdemo;

Query OK, 1 row affected (0.003 sec)

============================================

To use the database 

MariaDB [(none)]> use testdemo;

Database changed

============================================

To list the tables

MariaDB [testdemo]> show tables;

Empty set (0.001 sec)

============================================

To delete a database

MariaDB [(none)]> drop database testdemo;

Query OK, 0 rows affected (0.022 sec)

============================================

To exit from the Mysql server

MariaDB [testdemo]> exit;

Bye

firstname.lastname@MRMAcNAME ~ % 

Can't connect to local MySQL server through socket '/tmp/mysql.sock'

 If you see this error message while trying to connect to a local MySQL Server:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

It means either the MySQL server is not installed/running, or the file mysql.sock doesn’t exist in /var/lib/mysql/.

There are a couple of solutions for this error. 

On your command prompt or terminal try to stop the server first by running below command

Mysql.server stop

And after successfully stopped

Run below command to start the mysql server

Mysql.server start