主鍵:
PRIMARY KEY 主鍵 UNIQUE KEY 惟一鍵
DDL: CREATE DATABASE 建立數據庫 CREATE TABLE 建立表 CREATE USER 建立用戶
DROP DATABASE 刪除數據庫 DROP TABLE 刪除表 DROP USER 刪除用戶
GRANT 受權 REVOKE 取消受權 DML: INSERT 插入數據 DELETE 刪除數據 UPDATE 更新數據 SELECT 查詢數據
管理數據庫: CREATE DATABASE DB_NAME; DROP DATABASE DB_NAME;
SHOW DATABASES;
管理表:
CREATE TABLE [DB_NAME.]TABLE_NAME (COLUMN DEFINATION) COLUMN DEFINATION: (col1_name data_type [修飾符], col2_name data_type [修飾符])
查看錶定義: DESC TB_NAME
DROP TABLE TB_NAME;
管理用戶: CREATE USER USERNAME@HOST [IDENTIFIED BY
'password'
];
HOST表示格式:
ip: 網絡地址:
MySQL的字符通配符: %: 匹配任意長度的任意字符 _: 匹配任意單個字符
DROP USER USERNAME@HOST;
GRANT 權限列表 ON DB_NAME.TB_NAME TO USERNAME@HOST [IDENTIFIED BY
'new_pass'
];
權限列表: ALL PRIVILEGES,可簡寫爲ALL DB_NAME:
*:全部庫
TB_NAME: *: 全部表
刷新受權表,以使得權限當即生效: mysql> FLUSH PRIVILEGES;
REVOKE 權限列表 ON DB_NAME.DB_TABLE FROM USERNAME@HOST;
DML:
插入數據:
INSERT INTO tb_name [(col1, col2,...)] VALUE|VALUES (val1, val2,...)[,(val1, val2,...)];
mysql> INSERT INTO students (Name,Age,Gender,Class) VALUES (
'jerry'
,43,
'm'
,
'class 2'
),(
'Ou Yangfeng'
,77,
'm'
,
'Hamopai'
);
查詢數據:
SELECT 字段列表 FROM 表 WHERE 條件子句 ORDER BY 字段;
組合條件: and or not
BETWEEN start_value AND end_value;
LIKE: 模糊匹配 Name LIKE O%;
RLIKE:模式匹配 Name RLIKE
'^O.*$'
刪除數據: DELETE FROM tb_name WHERE 條件子句 [LIMIT n];
更新數據:
UPDATE tb_name SET col1=new_value1 WHERE 條件子句;
mysql> create database zone; 建立zone數據庫
Query OK, 1 row affected (0.00 sec)node
mysql> show databases; 查看數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| dingchao |
| momo |
| mysql |
| test |
| wpdb |
| zone |
+--------------------+mysql
mysql> drop database zone; 刪除數據庫
Query OK, 1 row affected (0.00 sec)sql
mysql> show databases; 查看數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| dingchao |
| momo |
| mysql |
| test |
| wpdb |
+--------------------+
6 rows in set (0.00 sec)數據庫
mysql> use dingchao; 使用數據庫
Database changedapache
mysql> create table boss ( ID int unsigned not null unique key auto_increment,bash
Name char(10) not null, Age tinyint, Gender enum('N','M') not null, Gongzi char(15));
Query OK, 0 rows affected (0.08 sec) 建立表服務器
mysql> show tables; 查看錶
+--------------------+
| Tables_in_dingchao |
+--------------------+
| Class |
| boss |
| student |
+--------------------+
3 rows in set (0.01 sec)網絡
mysql> desc boss; 查看錶的結構
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | char(10) | NO | | NULL | |
| Age | tinyint(4) | YES | | NULL | |
| Gender | enum('N','M') | NO | | NULL | |
| GongZI | char(15) | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)ide
mysql> insert into boss (Name,Age,Gender,GongZI) VALUE ('sloary','22','N','4400'),函數
('mara','35','M','3600'),('jifu','77','N','5800'); 插入數據
Query OK, 3 rows affected (0.00 sec)
mysql> select * from boss;
+----+--------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+--------+------+--------+--------+
| 1 | sloary | 22 | N | 4400 |
| 2 | mara | 35 | M | 3600 |
| 3 | jifu | 77 | N | 5800 |
| 4 | lili | 22 | N | 3800 |
| 5 | boy | 35 | M | 2600 |
| 6 | tom | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | hellen | 30 | M | 3000 |
+----+--------+------+--------+--------+
9 rows in set (0.00 sec)
mysql> select * from boss where Age > 22 ; 選擇行 查詢年齡大於22歲的人
+----+--------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+--------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jifu | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | tom | 77 | M | 3000 |
| 8 | king | 28 | M | 4600 |
| 9 | hellen | 30 | M | 3000 |
+----+--------+------+--------+--------+
6 rows in set (0.00 sec)
mysql> select Name,Age,GongZI from boss where Age > 30; 查詢年齡大於30歲人的工資
投影列 選擇行 ,記住前面多個字段要加 「,」
+------+------+--------+
| Name | Age | GongZI |
+------+------+--------+
| mara | 35 | 3600 |
| jifu | 77 | 5800 |
| boy | 35 | 2600 |
| tom | 77 | 3000 |
+------+------+--------+
4 rows in set (0.00 sec)
INSERT INTO box (Name,Age,Gender,GongZI) VALUE ('sloary','22','N','4400'),
('mara','35','M','3600'),('jifu','77','N','5800'); 插入數據
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dingchao |
| momo |
| mysql |
| test |
| wpdb |
+--------------------+
mysql> select Name,Age,GongZI from boss where Age > 30 order by Age,GongZI; 升序
+------+------+--------+
| Name | Age | GongZI |
+------+------+--------+
| boy | 35 | 2600 |
| mara | 35 | 3600 |
| tom | 77 | 3000 |
| jifu | 77 | 5800 |
+------+------+--------+
4 rows in set (0.00 sec)
mysql> select Name,Age,GongZI from boss where Age > 30 order by Age,GongZI desc; 降序
+------+------+--------+
| Name | Age | GongZI |
+------+------+--------+
| mara | 35 | 3600 |
| boy | 35 | 2600 |
| jifu | 77 | 5800 |
| tom | 77 | 3000 |
+------+------+--------+
4 rows in set (0.00 sec)
mysql> show tables; 查看錶
+--------------------+
| Tables_in_dingchao |
+--------------------+
| Class |
| boess |
| boss |
| student |
+--------------------+
4 rows in set (0.01 sec)
mysql> delete from boess; 這個命令慎用 一刪除整張表都被刪除啦
Query OK, 0 rows affected (0.00 sec)
mysql> delete from boss where Age =22 ;
Query OK, 2 rows affected (0.00 sec) 通常跟where 條件使用
mysql> select * from boss;
+----+--------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+--------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jini | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | tom | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | hellen | 30 | M | 3000 |
+----+--------+------+--------+--------+
7 rows in set (0.00 sec)
mysql> update boss set Name= 'jini' where GongZI = '3000'; 更新數據
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from boss;
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jini | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | jini | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
7 rows in set (0.00 sec)
mysql> select * from boss;
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jini | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | jini | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
7 rows in set (0.00 sec)
mysql> update boss set Name= 'jimi' where GongZI = '5800'; 更新數據
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from boss;
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jimi | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | jini | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
7 rows in set (0.00 sec)
mysql> select * from boss;
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 3 | jimi | 77 | N | 5800 |
| 5 | boy | 35 | M | 2600 |
| 6 | jini | 77 | M | 3000 |
| 7 | mary | 18 | N | 6000 |
| 8 | king | 28 | M | 4600 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
7 rows in set (0.00 sec)
mysql> select * from boss where GongZI between 2000 and 5000; 查詢工資介於2000-5000的人
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 2 | mara | 35 | M | 3600 |
| 5 | boy | 35 | M | 2600 |
| 6 | jini | 77 | M | 3000 |
| 8 | king | 28 | M | 4600 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
5 rows in set (0.00 sec)
mysql> select * from boss where Name like '%j%'; like 比較消耗資源,儘可能少使用
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 3 | jimi | 77 | N | 5800 |
| 6 | jini | 77 | M | 3000 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
3 rows in set (0.01 sec)
mysql> select * from boss where Name rlike '^j.*$'; 模糊查找基於正則
+----+------+------+--------+--------+
| ID | Name | Age | Gender | GongZI |
+----+------+------+--------+--------+
| 3 | jimi | 77 | N | 5800 |
| 6 | jini | 77 | M | 3000 |
| 9 | jini | 30 | M | 3000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
mysql> show engines; 查看存儲引擎
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)
mysql> show table status\G 查看錶的屬性
*************************** 1. row ***************************
Name: Class
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 1
Avg_row_length: 32
Data_length: 32
Max_data_length: 281474976710655
Index_length: 2048
Data_free: 0
Auto_increment: 2
Create_time: 2014-08-11 04:07:12
Update_time: 2014-08-11 04:14:15
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
mysql> show character set; 查看字符集
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
mysql> show collation; 查看排序規則
+--------------------------+----------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------------+----------+-----+---------+----------+---------+
| big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
| big5_bin | big5 | 84 | | Yes | 1 |
| dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
| dec8_bin | dec8 | 69 | | Yes | 1 |
mysql> show global variables like '%new%'; 查看變量
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| new | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show global status like 'create';
Empty set (0.00 sec)
建立用戶和受權
MariaDB [(none)]> create user dingchao@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> create user tom@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create database dingchao;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on dingchao.* to dingchao@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on dingchao.* to tom@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+----+----------+------+--------+------------+
| ID | Name | Age | Gender | Class |
+----+----------+------+--------+------------+
| 1 | dingchao | 40 | m | 11wanglou1 |
| 2 | tom | 40 | m | 11wanglou2 |
| 3 | peter | 30 | F | wushu |
| 4 | mary | 40 | m | 11wanglou2 |
| 5 | kim | 22 | F | wushu |
| 6 | jeny | 33 | m | 11wanglou2 |
| 7 | lili | 26 | F | wushu |
| 8 | jbod | 28 | m | 11wanglou2 |
| 9 | maki | 55 | F | wushu |
+----+----------+------+--------+------------+
select * from student where Age >30; 選擇行
mysql> select * from student where Age >30;
+----+----------+------+--------+------------+
| ID | Name | Age | Gender | Class |
+----+----------+------+--------+------------+
| 1 | dingchao | 40 | m | 11wanglou1 |
| 2 | tom | 40 | m | 11wanglou2 |
| 4 | mary | 40 | m | 11wanglou2 |
| 6 | jeny | 33 | m | 11wanglou2 |
| 9 | maki | 55 | F | wushu |
+----+----------+------+--------+------------+
select Name,Age from student where Age >30; 投影列選擇行
mysql> select Name,Age from student where Age >30;
+----------+------+
| Name | Age |
+----------+------+
| dingchao | 40 |
| tom | 40 |
| mary | 40 |
| jeny | 33 |
| maki | 55 |
+----------+------+
建立用戶和受權
MariaDB [(none)]> create user dingchao@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.01 sec)
建立tom這個用戶@能夠從192.168.0.0這個網段上以密碼1234 登陸到服務器
MariaDB [(none)]> create user tom@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; 刷新密碼使用戶生效,否則重啓服務器
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create database dingchao;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on dingchao.* to dingchao@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
受權tom這個用戶能夠在dingchao這個數據庫上建立全部的表、刪除全部的表及數據
能夠從192.168.0.0 這個網段以1234密碼登陸
MariaDB [(none)]> grant all on dingchao.* to tom@'192.168.%.%' identified by '1234';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges; 刷新使受權生效
Query OK, 0 rows affected (0.00 sec)
查詢數據庫中受權的用戶狀況
MariaDB [mysql]> select User,Password,Host from user;
+---------+-------------------------------------------+---------------+
| User | Password | Host |
+---------+-------------------------------------------+---------------+
| root | *A4B6157319038724E3560894F7F932C8886EBFCF | localhost |
| root | *A4B6157319038724E3560894F7F932C8886EBFCF | 127.0.0.1 |
| tom | *A4B6157319038724E3560894F7F932C8886EBFCF | 192.168.%.% |
| root | *A4B6157319038724E3560894F7F932C8886EBFCF | 192.168.%.% |
| zbxuser | *A4B6157319038724E3560894F7F932C8886EBFCF | 172.16.%.% |
| zbxuser | *A4B6157319038724E3560894F7F932C8886EBFCF | node1.org.com |
| zbxuser | *24E65C3D3577DA6C2A596788CEAA02923A74B75D | localhost |
+---------+-------------------------------------------+---------------+
update
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
建議使用GRANT語句進行受權,語句以下:
grant all privileges on *.* to root@'%' identified by "root";
---------------------------------------------------
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON maildb.*
TO 'mail'@'localhost'
IDENTIFIED by 'mailPASSWORD ';
建立snort數據庫,建立snort用戶,將snort庫全部表的全部權限賦予用戶snort。
mysql> create database snort;
Query OK, 1 row affected (0.06 sec)
建立數據庫
mysql> use mysql;
進入mysql庫
mysql> insert into user (Host,User,Password) values ("localhost","snort",PASSWORD("112233445566"));
建立用戶,設置初始密碼
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使改動生效; 注:若是不執行該指令,則沒法正常執行後續指令。
mysql> grant all on snort.* to 'snort'@'localhost' ;
Query OK, 0 rows affected (0.00 sec)
將snort庫的全部權限賦予 snort用戶
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使改動生效
mysql> show grants for snort@localhost;
+-------------------------------------------------------------------------------------+
| Grants for snort@localhost |
+-------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'snort'@'localhost' IDENTIFIED BY PASSWORD '1e6b29186dd45e97' |
| GRANT ALL PRIVILEGES ON `snort`.* TO 'snort'@'localhost' |
+-------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MySQL 賦予用戶權限命令的簡單格式可歸納爲:grant 權限 on 數據庫對象 to 用戶
1、grant 普通數據用戶,查詢、插入、更新、刪除 數據庫中全部表數據的權利。
grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to common_user@'%'
2、grant 數據庫開發人員,建立表、索引、視圖、存儲過程、函數。。。等權限。
grant 建立、修改、刪除 MySQL 數據表結構權限。
grant create on testdb.* to developer@'192.168.0.%' ;
grant alter on testdb.* to developer@'192.168.0.%' ;
grant drop on testdb.* to developer@'192.168.0.%' ;
grant 操做 MySQL 外鍵權限。
grant references on testdb.* to developer@'192.168.0.%' ;
grant 操做 MySQL 臨時表權限。
grant create temporary tables on testdb.* to developer@'192.168.0.%' ;
grant 操做 MySQL 索引權限。
grant index on testdb.* to developer@'192.168.0.%' ;
grant 操做 MySQL 視圖、查看視圖源代碼 權限。
grant create view on testdb.* to developer@'192.168.0.%' ;
grant show view on testdb.* to developer@'192.168.0.%' ;
grant 操做 MySQL 存儲過程、函數 權限。
grant create routine on testdb.* to developer@'192.168.0.%' ; -- now, can show procedure status
grant alter routine on testdb.* to developer@'192.168.0.%' ; -- now, you can drop a procedure
grant execute on testdb.* to developer@'192.168.0.%' ;
3、grant 普通 DBA 管理某個 MySQL 數據庫的權限。
grant all privileges on testdb to dba@'localhost'
其中,關鍵字 「privileges」 能夠省略。
4、grant 高級 DBA 管理 MySQL 中全部數據庫的權限。
grant all on *.* to dba@'localhost'
5、MySQL grant 權限,分別能夠做用在多個層次上。
1. grant 做用在整個 MySQL 服務器上:
grant select on *.* to dba@localhost ; -- dba 能夠查詢 MySQL 中全部數據庫中的表。
grant all on *.* to dba@localhost ; -- dba 能夠管理 MySQL 中的全部數據庫
2. grant 做用在單個數據庫上:
grant select on testdb.* to dba@localhost ; -- dba 能夠查詢 testdb 中的表。
3. grant 做用在單個數據表上:
grant select, insert, update, delete on testdb.orders to dba@localhost ;
4. grant 做用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost ;
5. grant 做用在存儲過程、函數上:
grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'
6、查看 MySQL 用戶權限
查看當前用戶(本身)權限:
show grants;
查看其餘 MySQL 用戶權限:
show grants for dba@localhost;
7、撤銷已經賦予給 MySQL 用戶權限的權限。
revoke 跟 grant 的語法差很少,只須要把關鍵字 「to」 換成 「from」 便可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
8、MySQL grant、revoke 用戶權限注意事項
1. grant, revoke 用戶權限後,該用戶只有從新鏈接 MySQL 數據庫,權限才能生效。
2. 若是想讓受權的用戶,也能夠將這些權限 grant 給其餘用戶,須要選項 「grant option「
grant select on testdb.* to dba@localhost with grant option;