屌炸天實戰 MySQL 系列教程(二) 史上最屌、你不知道的數據庫操作,mysql
此篇寫MySQL中最基礎,也是最重要的操作!
第一篇:屌炸天實戰 MySQL 系列教程(一) 生產標准線上環境安裝配置案例及棘手問題解決
第二篇:屌炸天實戰 MySQL 系列教程(二) 史上最屌、你不知道的數據庫操作
本章內容:
- 查看\創建\使用\刪除 數據庫
- 用戶管理及授權實戰
- 局域網遠程連接法
- 查看\創建\使用\刪除\清空\修改 數據庫表(是否可空,默認值,主鍵,自增,外鍵)
- 表內容的增刪改查
- where條件、通配符_%、限制limit、排序desc\asc、連表join、組合union
- 查看建表語句、查看表結構、查看是否走索引
- 數據類型
- 索引!
一、數據庫操作
1、查看數據庫
SHOW DATABASES;
# 默認數據庫:
mysql - 用戶權限相關數據
test - 用於用戶測試數據
information_schema - MySQL本身架構相關數據
2、創建數據庫
# utf-8 編碼
CREATE DATABASE 數據庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
# gbk 編碼
CREATE DATABASE 數據庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
3、使用數據庫
USE db_name;
# 可以不使用分號
4、用戶管理
# 創建用戶
create user '用戶名'@'IP地址' identified by '密碼';
# 刪除用戶
drop user '用戶名'@'IP地址';
# 修改用戶
rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址';;
# 修改密碼
set password for '用戶名'@'IP地址' = Password('新密碼')
PS:用戶權限相關數據保存在mysql數據庫的user表中,所以也可以直接對其進行操作(不建議)
# 查看當前用戶
select user();
# 查看所有用戶
select host,user from mysql.user;
# 人性化顯示所有用戶
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
# 查看用戶的所有權限
show grants for 'nick'@'%';

![]()
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------+
| query |
+---------------------------+
| User: 'nick'@'%'; |
| User: 'root'@'localhost'; |
+---------------------------+
2 rows in set (0.00 sec)
mysql>
mysql>
mysql>
mysql>
mysql> select host,user from mysql.user;
+-----------+------+
| host | user |
+-----------+------+
| % | nick |
| localhost | root |
+-----------+------+
2 rows in set (0.00 sec)
mysql> show grants for 'nick'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for nick@% |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'nick'@'%' IDENTIFIED BY PASSWORD '*ECE7D02DCD7D4EF7CFE8E3B249FD1D5062A821F7' |
| GRANT ALL PRIVILEGES ON `kaoshi`.* TO 'nick'@'%' |
| GRANT ALL PRIVILEGES ON `xxxxx`.* TO 'nick'@'%' |
| GRANT ALL PRIVILEGES ON `xxxxxx`.`chouti` TO 'nick'@'%' |
+-----------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql>
View Code
5、授權管理
# 查看權限
show grants for '用戶'@'IP地址'
# 授權
grant 權限 on 數據庫.表 to '用戶'@'IP地址'
# 取消權限
revoke 權限 on 數據庫.表 from '用戶'@'IP地址'
常用權限:
all privileges 除grant外的所有權限
select 僅查權限
select,insert 查和插入權限
usage 無訪問權限
對於目標數據庫以及內部其他:
數據庫名.* 數據庫中的所有
數據庫名.表 指定數據庫中的某張表
數據庫名.存儲過程 指定數據庫中的存儲過程
*.* 所有數據庫
對於用戶和IP:
用戶名@IP地址 用戶只能在改IP下才能訪問
用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(通配符%表示任意)
用戶名@% 用戶可以再任意IP下訪問(默認IP地址為%)

![]()
all privileges 除grant外的所有權限
select 僅查權限
select,insert 查和插入權限
...
usage 無訪問權限
alter 使用alter table
alter routine 使用alter procedure和drop procedure
create 使用create table
create routine 使用create procedure
create temporary tables 使用create temporary tables
create user 使用create user、drop user、rename user和revoke all privileges
create view 使用create view
delete 使用delete
drop 使用drop table
execute 使用call和存儲過程
file 使用select into outfile 和 load data infile
grant option 使用grant 和 revoke
index 使用index
insert 使用insert
lock tables 使用lock table
process 使用show full processlist
select 使用select
show databases 使用show databases
show view 使用show view
update 使用update
reload 使用flush
shutdown 使用mysqladmin shutdown(關閉MySQL)
super