MySQL數據庫入門———經常使用基礎命令
mysql
數據——公司的生命線,所以在大多數的互聯網公司,都在使用開源的數據庫產品,MySQL也所以關注度與使用率很是的高,因此作爲運維的屌絲們,掌握它的一些基本操做仍是必要的。sql
一、使用幫助信息數據庫
登錄數據庫就不介紹了哦
運維
好比說想作建立的數據庫的操做,不知道命令,就能夠查看幫助信息ide
mysql> help create;
spa
Many help items for your request exist.orm
To make a more specific request, please type 'help <item>',ci
where <item> is one of the following產品
topics:it
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
二、建立、刪除、查看數據庫
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 |
+----------------------------+
三、鏈接數據庫
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)
四、建立用戶、受權、收回權限
當數據庫建立完成後,就須要建立用戶,以供須要鏈接數據庫的人員使用與操做數據庫,不可能人人使用root登錄,因此權限設置也是很重要的
mysql> grant all on test_gbk.* to 'testuser'@'localhost' identified by '123456'; #建立用戶testuser,並將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
五、建立、刪除表
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)
基礎的操做命令就介紹這麼多,都是一些經常使用的必備命令