Lệnh mysql trong linux

Bình thường có lẽ những newbie như mình thường sẽ sử dụng các công cụ như workbench, PhpMyadmin,. to thao tác với cơ sở dữ liệu. Nhưng đối với hệ điều hành linux đã hỗ trợ cách dễ hơn, đơn giản hơn, nhanh hơn và màn hình tốt hơn để làm điều đó. Mình sẽ giới thiệu với mọi người câu lệnh để có thể sử dụng mysql trong terminal. Để truy cập vào cơ sở dữ liệu từ thiết bị đầu cuối, ta sử dụng câu lệnh sau

$ mysql -u root -p

Bạn nhập đúng thì terminal sẽ bắt bạn phải điền mk của mysql và kết quả nếu bạn nhập đúng là

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Để thay đổi mk của người dùng root, bạn hãy sử dụng lệnh sau

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

De exit from mysql ta used command

mysql> quit

To show out database in mysql ta used

SHOW DATABASES;

Kết quả thu được có dạng như thế này

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

To create database

mysql> CREATE DATABASE pets;
Query OK, 1 row affected (0.01 sec)

Để tạo các bảng bên trong cơ sở dữ liệu, đầu tiên ta phải lựa chọn cơ sở dữ liệu để sử dụng

mysql> USE pets
Database changed

To create a new table

________số 8

Để xem tất cả các bảng có trong cơ sở dữ liệu

mysql> SHOW TABLES;
+----------------+
| Tables_in_pets |
+----------------+
| cats           |
+----------------+
1 row in set (0.00 sec)

Để xem thông tin các cột trong bảng đấy

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
0

Thêm một bản ghi vào trong bảng

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
1

Xem các bản ghi có trong bảng

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
2

Xóa bàn ghi trong bảng

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
3

Thêm một cột vào trong bảng

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
4

Xóa cột trong bảng

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
5

Như vậy là mình đã giới thiệu cho các bạn thêm một cách để có thể thao tác với cơ sở dữ liệu

Tham khảo tại

https. // nhà phát triển. mysql. com/doc/mysql-bắt đầu-bắt đầu/en/