mysql--常用基础命令

MySQL数据库入门———常用基础命令




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


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)


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

 

posted @ 2018-08-04 14:53  禾子、  阅读(226)  评论(0编辑  收藏  举报