MySQL数据库基础篇之入门基础命令

34次阅读

共计 5154 个字符,预计需要花费 13 分钟才能阅读完成。

数据——公司的生命线,因此在大多数的互联网公司,都在使用开源的数据库产品,MySQL 也因此关注度与使用率非常的高,所以做为运维的屌丝们,掌握它的一些基本操作还是必要的,那么今天就来侃一侃一些常用的基础命令,走过的、路过的,就不要再错过了,掌握的、没掌握的,也不要再扔民工哥鸡蛋了哦,呵呵 ………

从此篇文章开始将要给大家带来另一个系列的文章:Mysql 数据库系列,总共分为以下四个大类:
1、基础篇
2、中级篇
3、优化篇
4、企业架构篇

1、使用帮助信息

登陆数据库就不介绍了哦,比如说想做创建的数据库的操作,不知道命令,就可以查看帮助信息

mysql> help create;
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   CREATE DATABASE   #最简单的创建命令
   CREATE EVENT
   CREATE FUNCTION
   CREATE FUNCTION UDF
   CREATE INDEX
   CREATE LOGFILE GROUP
   CREATE PROCEDURE
   CREATE SERVER
   CREATE TABLE
   CREATE TABLESPACE
   CREATE TRIGGER
   CREATE USER
   CREATE VIEW
   SHOW
   SHOW CREATE DATABASE
   SHOW CREATE EVENT
   SHOW CREATE FUNCTION
   SHOW CREATE PROCEDURE
   SHOW CREATE TABLE
   SPATIAL

2、创建、删除、查看数据库

mysql> create database test_data;
#创建默认字符集的数据库(默认是拉丁字符集)Query OK, 1 row affected (0.02 sec)
mysql> show databases like "test%";
+------------------+
| Database (test%) |
+------------------+
| test_data        |
+------------------+
1 rows in set (0.00 sec)

创建 gbk 字符集的数据库

mysql> create database test_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 1 row affected (0.04 sec)
mysql> show create database test_gbk; 
#查看创建数据库的语句
+----------+----------------------------------------------+
| Database | Create Database                              |
+----------+------------------------------------------------------------------+
| test_gbk | CREATE DATABASE `test_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+----------------------------------------------+
1 row in set (0.00 sec)

删除数据库

mysql> drop database test_data;
Query OK, 0 rows affected (0.07 sec)
mysql> show databases;
+----------------------------+
| Database                   |
+----------------------------+
| information_schema |
| test_gbk                    |
+----------------------------+

3、连接数据库

mysql> use test_gbk;
#相当于 cd 命令,切换到数据库进行操作
Database changed
mysql> select database();
#查看当前连接的数据库,相当于 pwd
+------------+
| database() |
+------------+
| test_gbk   |
+------------+
1 row in set (0.00 sec)
mysql> select user();

#查看当前连接数据库的用户, 相当于 whoami
+--------------------+
| user()               |
+-------------------+
| root@localhost |
+--------------------+
1 row in set (0.00 sec)

4、创建用户、授权、收回权限

当数据库创建完成后,就需要创建用户,以供需要连接数据库的人员使用与操作数据库,不可能人人使用 root 登陆,所以权限设置也是很重要的

mysql> grant all on test_gbk.* to 'testuser'@'localhost' identified by '123456';
#创建用户并 all 权限给在 test_gbk 库所有表,密码‘123456’Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
#刷新权限,使权限生效
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'testuser'@'localhost';
#查看用户有哪些权限
+-----------------------------------------------------------------------------------------------------------------+
| Grants for testuser@localhost                           |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `test_gbk`.* TO 'testuser'@'localhost'                                  |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

收回权限

mysql> revoke insert,update,select,delete on test_gbk.* from 'testuser'@'localhost';
#将以上权限收回
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'testuser'@'localhost';             
+----------------------------------------------------------------------------------------------------+
| Grants for testuser@localhost                           |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'           |
| GRANT CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `test_gbk`.* TO 'testuser'@'localhost' |
+------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

注:原来不知道 all 权限到底是哪些权限,采用这种方法之后,应该就清楚了

SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER

5、创建、删除表

mysql> create table test(id int(4)not null,name char(20)not null);
#建表,并且建立两个字段
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;# 查看表
+--------------------+
| Tables_in_test_gbk |
+--------------------+
| test                     |
+--------------------+
1 row in set (0.00 sec)
mysql> desc test; #查看表结构
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | NO   |     | NULL    |       |
| name  | char(20) | NO   |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> create table test1(id int(4)not null,name char(20)not null);
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------------+
| Tables_in_test_gbk |
+------------------------+
| test                        |
| test1                     |
+--------------------+
2 rows in set (0.00 sec)

删除表

mysql> drop tables test;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+--------------------+
| Tables_in_test_gbk |
+--------------------+
| test1                 |
+--------------------+
1 row in set (0.00 sec)

查看建表

mysql> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (`id` int(4) NOT NULL,
  `name` char(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

基础的操作命令就介绍这么多,都是一些常用的必备命令

关注 民工哥技术之路 微信公众号对话框回复关键字:1024 可以获取一份最新整理的技术干货:包括系统运维、数据库、redis、MogoDB、电子书、Java 基础课程、Java 实战项目、架构师综合教程、架构师实战项目、大数据、Docker 容器、ELK Stack、机器学习、BAT 面试精讲视频等。

正文完
 0