连接数据库
终端中使用mysql -u root -p连接电脑上的数据库
alicedembp:~ alice$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.15 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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.
查看现有数据库
使用show databases;查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| TL_TEST |
+--------------------+
5 rows in set (0.01 sec)
选择数据库并使用
使用use 数据库名;来选择数据并使用他
mysql> use TL_TEST;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
创建一个表格
使用create table 表名来创建表格,假设名字叫MAJOR
mysql> create table MAJOR(
-> id varchar(100) not null,
-> name varchar(20) not null,
-> mobile int primary key);
Query OK, 0 rows affected (0.01 sec)
查看所有表格
使用show tables; 查看所有表格
mysql> show tables;
+-------------------+
| Tables_in_tl_test |
+-------------------+
| MAJOR |
| USERS |
+-------------------+
2 rows in set (0.00 sec)
查看表结构
使用describe 表名;查看表格的所有列及顺序
mysql> describe USERS;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| mobile | int(11) | NO | PRI | NULL | |
+--------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> describe MAJOR;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | varchar(100) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| mobile | int(11) | NO | PRI | NULL | |
+--------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql>
查看创建的数据表位置
使用show global variables like '%datadir%';查看创建的数据表存储位置,发现默认在:
/usr/local/mysql/data/
mysql> show global variables like '%datadir%';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| datadir | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.00 sec)